map容器:键值和实值是分开的,排序规则按照键值排序
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<map> #include<string> using namespace std; template<class T> void printMap(T &m){ for (map<int, string>::iterator it = m.begin(); it != m.end(); ++it){ cout << "key:" << it->first << " value:" << it->second << endl; } } //注意:map的排序规则,传入的参数不是pair类型,而是key类型 struct mycompare{ bool operator()(int key1, int key2){ return key1 > key2; } }; //1. map容器的使用 void test01(){ //map是否支持随机访问?不支持随机访问,双向迭代器 //创建map容器 map<int, string, mycompare> mymap; //1. 第一种插入方式 mymap.insert(pair<int,string>(3,"aaa")); //2. 第二种插入方式 mymap.insert(make_pair(6, "bbb")); //3. 第三种插入方式 mymap.insert(map<int,string>::value_type(2,"ccc")); //4. 第四种插入方式 mymap[4] = "ddd"; mymap[4] = "eee"; mymap.erase(2); printMap(mymap); //cout << "size:" << mymap.size() << endl; //如果使用[]号这种方式输出一个不存在的key的值,那么编译器会以指定key增加新的数据 //如果使用[]号方式插入一条key存在的数据,那么已有数据会被修改 //cout << mymap[100] << endl; //cout << "size:" << mymap.size() << endl; } //3. map查找操作 /* find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end(); count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。 lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。 upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。 equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。 */ void test02(){ map<int, string> mymap; mymap.insert(make_pair(1, "aaa")); mymap.insert(make_pair(2, "bbb")); mymap.insert(make_pair(3, "ccc")); mymap.insert(make_pair(4, "ddd")); mymap.insert(make_pair(5, "eee")); map<int, string>::iterator it = mymap.find(30); if (it == mymap.end()){ cout << "查找失败!" << endl; } else{ cout << "Key:" << it->first << " Value:" << it->second << endl; } it = mymap.lower_bound(3); if (it == mymap.end()){ cout << "查找失败!" << endl; } else{ cout << "Key:" << it->first << " Value:" << it->second << endl; } it = mymap.upper_bound(3); if (it == mymap.end()){ cout << "查找失败!" << endl; } else{ cout << "Key:" << it->first << " Value:" << it->second << endl; } pair<map<int, string>::iterator, map<int, string>::iterator> ret = mymap.equal_range(3); if (ret.first != mymap.end()){ cout << "Key:" << ret.first->first << " Value:" << ret.first->second << endl; } if (ret.second != mymap.end()){ cout << "Key:" << ret.second->first << " Value:" << ret.second->second << endl; } } int main(){ //test01(); test02(); system("pause"); return EXIT_SUCCESS; }
map容器案例:员工分组案例
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<string> #include<map> #include<ctime> using namespace std; //公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作 //人员信息有: 姓名 年龄 电话 工资等组成 //通过Multimap进行信息的插入 保存 显示 //分部门显示员工信息 显示全部员工信息 #define SALE_DEPATMENT 1 //销售部门 #define DEVELOP_DEPATMENT 2 //研发部门 #define FINACIAL_DEPATMENT 3 //财务部门 class Woker{ public: string mName; int mAge; int mSalary; }; //创建员工 void CreateWorkers(vector<Woker> &vWorkers){ srand((unsigned int)time(NULL)); string nameSeed = "ABCDE"; for (int i = 0; i < 5;i ++){ Woker worker; worker.mName = "员工"; worker.mName += nameSeed[i]; worker.mAge = rand() % 30 + 30; worker.mSalary = rand() % 10000 + 10000; vWorkers.push_back(worker); } } //员工分组 void WokerByGroup(vector<Woker> &vWorkers, multimap<int, Woker> &mWokers){ //遍历员工 for (vector<Woker>::iterator it = vWorkers.begin(); it != vWorkers.end(); ++it){ int departmentID = rand() % 3 + 1; //员工保存在分组之后容器中 mWokers.insert(make_pair(departmentID,*it)); } } void Group(multimap<int, Woker> &mWokers, int depID){ multimap<int, Woker>::iterator it = mWokers.find(depID); int mcount = mWokers.count(depID); int index = 0; for (; it != mWokers.end() && index < mcount; ++it, ++index){ cout << "Name:" << it->second.mName << " Age:" << it->second.mAge << " Salary:" << it->second.mSalary << "美刀!" << endl; } } //打印各组员工信息 void PrintWorkers(multimap<int, Woker> &mWokers){ cout << "财务部门员工信息如下:" << endl; Group(mWokers, FINACIAL_DEPATMENT); cout << "研发部门员工信息如下:" << endl; Group(mWokers, DEVELOP_DEPATMENT); cout << "销售部门员工信息如下:" << endl; Group(mWokers, SALE_DEPATMENT); } int main(){ vector<Woker> vWorkers;// 保存未分组的员工信息 multimap<int, Woker> mWokers; //保存分组后的员工信息 CreateWorkers(vWorkers); WokerByGroup(vWorkers, mWokers); PrintWorkers(mWokers); system("pause"); return EXIT_SUCCESS; }