根据C++学习视频写的一个小案例,巩固multimap容器的学习。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
#define SALE_DEPARTMENT 1 //销售部门
#define DEVELOP_DEPARTMEN 2 //研发部门
#define FINACIAL_DEPARTMENT 3 //财务部门
class Worker{
public:
public:
string mName;
string mTele;
int mAge;
int mSalary;
};
//创建员工
void Create_Worker(vector<Worker>& vWorker){
string seedName = "ABCDE";
for (int i = 0; i < 5; i++){
Worker worker;
worker.mName = "员工";
worker.mName += seedName[i];
worker.mAge = rand() % 10 + 20;
worker.mTele = "13227848860";
worker.mSalary = rand() % 10000 + 10000;
//保存员工信息
vWorker.push_back(worker);
}
}
//员工分组
void WorkerByGroup(vector<Worker>& vWorker, multimap<int, Worker>& WorkerGroup){
//把员工随机分配到不同部门
srand(time(NULL));
for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++){
int departID = rand() % 3 + 1;
switch (departID){
case SALE_DEPARTMENT:
WorkerGroup.insert(make_pair(SALE_DEPARTMENT, *it));
break;
case DEVELOP_DEPARTMEN:
WorkerGroup.insert(make_pair(DEVELOP_DEPARTMEN, *it));
break;
case FINACIAL_DEPARTMENT:
WorkerGroup.insert(make_pair(FINACIAL_DEPARTMENT, *it));
break;
default:
break;
}
}
}
void ShowGroupWorkers(multimap<int, Worker>& WorkerGroup, int departID){
multimap<int, Worker>::iterator it = WorkerGroup.find(departID);
int DepartCount = WorkerGroup.count(departID);
int num = 0;
for (multimap<int, Worker>::iterator pos = it; pos != WorkerGroup.end() && num < DepartCount; pos++, num++){
cout << "姓名:" << pos->second.mName
<< " 年龄:" << pos->second.mAge
<< " 电话:" << pos->second.mTele
<< " 薪水:" << pos->second.mSalary
<< endl;
}
}
//打印每一部门的员工信息
void PrintWorkerByGroup(multimap<int, Worker>& WorkerGroup){
cout << "销售部门员工:" << endl;
ShowGroupWorkers(WorkerGroup, SALE_DEPARTMENT);
cout << "研发部门员工:" << endl;
ShowGroupWorkers(WorkerGroup, DEVELOP_DEPARTMEN);
cout << "财务部门员工:" << endl;
ShowGroupWorkers(WorkerGroup, FINACIAL_DEPARTMENT);
}
int main(void){
//存放新员工信息
vector<Worker> vWorker;
//multimap保存分组信息
multimap<int, Worker> WorkerGroup;
//创建员工
Create_Worker(vWorker);
//员工分组
WorkerByGroup(vWorker, WorkerGroup);
//打印每一部门的员工信息
PrintWorkerByGroup(WorkerGroup);
return 0;
}