map容器:键值和实值是分开的,排序规则按照键值排序
#define _CRT_SECURE_NO_WARNINGS#include #include
map容器案例:员工分组案例
#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include 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 &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 &vWorkers, multimap &mWokers){ //遍历员工 for (vector ::iterator it = vWorkers.begin(); it != vWorkers.end(); ++it){ int departmentID = rand() % 3 + 1; //员工保存在分组之后容器中 mWokers.insert(make_pair(departmentID,*it)); }}void Group(multimap &mWokers, int depID){ multimap ::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 &mWokers){ cout << "财务部门员工信息如下:" << endl; Group(mWokers, FINACIAL_DEPATMENT); cout << "研发部门员工信息如下:" << endl; Group(mWokers, DEVELOP_DEPATMENT); cout << "销售部门员工信息如下:" << endl; Group(mWokers, SALE_DEPATMENT);}int main(){ vector vWorkers;// 保存未分组的员工信息 multimap mWokers; //保存分组后的员工信息 CreateWorkers(vWorkers); WokerByGroup(vWorkers, mWokers); PrintWorkers(mWokers); system("pause"); return EXIT_SUCCESS;}