map/multimap容器
map/multimap容器相对于set的区别,map具有键值和实值,所有元素根据键值自动排序。pair的第一元素称为键值,第二元素称为实值。map也是以红黑树为底层实现机制
map的key值不可以重复,multimap的key值可以重复
#include
#include
using namespace std;
void PrintMap(map<int,int> m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key值:" << (*it).first << " value值:" << (*it).second << endl;
}
}
void MapTest1() {
map<int, int> map1;
map1.insert(pair<int, int>(1, 56));
map1.insert(make_pair(4, 888));
map1.insert(map<int,int>::value_type(2,455));
map1[9] = 233;
PrintMap(map1);
}
void MapTest2() {
map<int, int> map1;
/*测试方法一:方法一在插入时会返回一个pair对组,
第一个元素是元素迭代器,第二个元素是布尔值反应插入结果*/
pair<map<int, int>::iterator, bool> ret = map1.insert(pair<int, int>(10, 255));
if (ret.second == false) {
cout << "第一次插入失败!" << endl;
}
else {
cout << "第一次插入成功!" << endl;
}
ret = map1.insert(pair<int, int>(10, 199));
if (ret.second == false) {
cout << "第二次插入失败!" << endl;
}
else {
cout << "第二次插入成功!" << endl;
}
cout << "------------------------" << endl;
map1[40] = 90;
PrintMap(map1);
map1[40] = 88;
cout << "------------------------" << endl;
PrintMap(map1);
cout << "------------------------" << endl;
cout << "map1[60]=" << map1[60] << endl;
cout << "------------------------" << endl;
PrintMap(map1);
}
int main() {
MapTest2();
return 0;
}
实践案例
#include
#include
#include
#include
#include
#define SALE_DEPARTMENT 1
#define DEVELOP_DEPARTMENT 2
#define FINANCIAL_DEPARTMENT 3
using namespace std;
class Staff {
public:
string mName;
string mTele;
int mAge;
int mSalary;
Staff(){};
Staff(string name,string tele, int age,int salary):mName(name),mAge(age),mTele(tele),mSalary(salary){}
};
void CreateStaff(vector<Staff>& vStaff) {
string seedName = "ABCDE";
for (int i = 0; i < 5; i++) {
Staff staff;
staff.mName = "职员";
staff.mName += seedName[i];
staff.mAge = rand() % 10 + 20;
staff.mTele = "010-88888888";
staff.mSalary = rand() % 10000 + 10000;
vStaff.push_back(staff);
}
}
void StaffByGroup(vector<Staff>& vStaff, multimap<int,Staff>& mStaff) {
srand(time(NULL));
for (vector<Staff>::iterator it = vStaff.begin(); it != vStaff.end(); it++) {
int department = rand() % 3 + 1;
switch (department)
{
case SALE_DEPARTMENT:
mStaff.insert(make_pair(SALE_DEPARTMENT, (*it)));
break;
case DEVELOP_DEPARTMENT:
mStaff.insert(make_pair(DEVELOP_DEPARTMENT, (*it)));
break;
case FINANCIAL_DEPARTMENT:
mStaff.insert(make_pair(FINANCIAL_DEPARTMENT, (*it)));
break;
default:
break;
}
}
}
void PrintStaff(multimap<int, Staff>& mStaff, int departement,string departmentName) {
multimap<int, Staff>::iterator it = mStaff.find(departement);
int mCount = mStaff.count(departement);
cout << "-------------------------------------------" << endl;
cout << departmentName << endl;
int num = 0;
for (it; it != mStaff.end() && num < mCount; it++, num++) {
cout << "姓名:" << ((*it).second).mName << " 年龄:" << ((*it).second).mAge << " 工资:" << ((*it).second).mSalary << " 电话:" << ((*it).second).mTele << endl;
}
}
void PrintStaffByGroup(multimap<int,Staff>& mStaff) {
PrintStaff(mStaff, SALE_DEPARTMENT, "销售部门员工表:");
PrintStaff(mStaff, DEVELOP_DEPARTMENT, "研发部门员工表:");
PrintStaff(mStaff, FINANCIAL_DEPARTMENT, "财务部门员工表:");
}
int main() {
vector<Staff> vStaff;
multimap<int, Staff> mStaff;
CreateStaff(vStaff);
StaffByGroup(vStaff, mStaff);
PrintStaffByGroup(mStaff);
return 0;
}
#include
#include
#include
using namespace std;
class MyKey {
public:
int mIndex;
MyKey(int index) :mIndex(index) {}
};
class MyComapare {
public:
bool operator()(MyKey key1, MyKey key2) const
{
return key1.mIndex < key2.mIndex;
}
};
void PrintMap2(map<MyKey, int,MyComapare> myMap) {
for (map<MyKey, int>::iterator it = myMap.begin(); it != myMap.end(); it++) {
cout << "Key : " << ((*it).first).mIndex << " value : " << (*it).second << endl;
}
}
void MultimapTest1() {
map<MyKey, int,MyComapare> myMap;
myMap.insert(make_pair(MyKey(15), 59));
myMap.insert(make_pair(MyKey(45), 449));
myMap.insert(make_pair(MyKey(4), 233));
myMap.insert(make_pair(MyKey(1), 88));
PrintMap2(myMap);
}
int main() {
MultimapTest1();
return 0;
}