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