1 ``` 2 #include<iostream> 3 #include<algorithm> 4 #include<vector> 5 #include<map> 6 #include<cstring> 7 #include<cstdlib> 8 #include<ctime> 9 using namespace std; 10 11 #define SALE_DEPATMENT 1//销售部门 12 #define DEVELOP_DEPATMENT 2//研发部门 13 #define FINACIAL_DEPATMENT 3//财务部门 14 15 /*multimap案例 16 公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作 17 人员信息有:姓名 年龄 电话 工资等组成 18 通过multimap 进行信息的插入 保存 显示 19 分部门显示员工信息 显示全部员工信息*/ 20 21 class Worker{ 22 public: 23 string mName; 24 string mTele; 25 int mAge; 26 int mSalary; 27 }; 28 29 void Creat_Worker(vector<Worker>& vWorker){ 30 string seedName="ABCDE"; 31 for( int i=0; i<5; i++ ){ 32 Worker worker; 33 worker.mName="员工"; 34 worker.mName+=seedName[i]; 35 36 worker.mAge=rand()%10+20; 37 worker.mTele="010-8888888"; 38 worker.mSalary=rand()%10000+10000; 39 //保存员工信息 40 vWorker.push_back(worker); 41 42 } 43 } 44 45 //员工分组 46 void WorkerByGroup(vector<Worker>& vWorker,multimap<int,Worker>& WorkerGroup){ 47 //把员工随机分配到不同的部门 48 srand(time(NULL)); 49 for(vector<Worker>::iterator it=vWorker.begin(); it!=vWorker.end(); it++ ){ 50 int departID=rand()%3+1; 51 switch(departID){ 52 case SALE_DEPATMENT: 53 WorkerGroup.insert(make_pair(SALE_DEPATMENT,*it)); 54 break; 55 case DEVELOP_DEPATMENT: 56 WorkerGroup.insert(make_pair(DEVELOP_DEPATMENT,*it)); 57 break; 58 case FINACIAL_DEPATMENT: 59 WorkerGroup.insert(make_pair(FINACIAL_DEPATMENT,*it)); 60 break; 61 default: 62 break; 63 } 64 } 65 66 } 67 68 void ShowGroupWorkers(multimap<int,Worker>& WorkerGroup,int departID){ 69 multimap<int,Worker>::iterator it=WorkerGroup.find(departID); 70 int DepartCount=WorkerGroup.count(departID); 71 int num=0; 72 for( ;num<DepartCount&&it!=WorkerGroup.end(); it++,num++ ){ 73 cout<<"姓名:"<<it->second.mName<<" 年龄:"<<it->second.mAge<<" 电话:" 74 <<it->second.mTele<<" 工资:"<<it->second.mSalary<<endl; 75 } 76 } 77 78 //打印每一部门的员工信息 79 void PrintWorkerByGroup(multimap<int,Worker>& WorkerGroup){ 80 //打印销售部员工信息 81 cout<<"销售部门"<<endl; 82 ShowGroupWorkers(WorkerGroup,SALE_DEPATMENT); 83 //打印研发部员工信息 84 cout<<"研发部门"<<endl; 85 ShowGroupWorkers(WorkerGroup,DEVELOP_DEPATMENT); 86 //打印财务部员工信息 87 cout<<"财务部门"<<endl; 88 ShowGroupWorkers(WorkerGroup,FINACIAL_DEPATMENT); 89 90 } 91 92 93 int main(){ 94 //存放新员工的信息 95 vector<Worker> vWorker; 96 //multimap保存分组信息 97 multimap<int,Worker> WorkerGroup; 98 //创建员工 99 Creat_Worker(vWorker); 100 //员工分组 101 WorkerByGroup(vWorker,WorkerGroup); 102 //打印每一部门的员工信息 103 PrintWorkerByGroup(WorkerGroup); 104 105 return 0; 106 } 107 108 ```