案例要求:
//multimap 案例
//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作
//人员信息有: 姓名 年龄 电话 工资等组成
//通过 Multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息
程序源码:
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<time.h>
#include<stdlib.h>
using namespace std; //multimap 案例
//公司招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作
//人员信息有: 姓名 年龄 电话 工资等组成
//通过 Multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息 #define SALE_DEPARTMENT 1 //销售部
#define DEVELOP_DEPARTMENT 2 //研发部
#define FINACIAL_DEPARTMENT 3 //财务部 class Worker
{
public:
string mName;
int mAge;
string mTele;
double mSalary;
};
//创建员工
void CreatWorker(vector<Worker>&vWorker)
{
string nameSeed = "ABCDE";
for (int i = ; i < ; i++)
{
Worker worker;
worker.mName = "员工";
worker.mName += nameSeed[i]; worker.mAge = rand() % +;
worker.mSalary = rand() % + ;
worker.mTele = "+86-88888888";
//保存员工信息
vWorker.push_back(worker);
}
}
//员工分组
void WorkerGroup(vector<Worker>& vWorker, multimap<int, Worker>& workerGroup)
{
srand(time(NULL));
//把员工随机分配到不同部门
for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
int DepartID = rand() % + ;
switch (DepartID)
{
case SALE_DEPARTMENT:
workerGroup.insert(make_pair(SALE_DEPARTMENT, *it));
break;
case DEVELOP_DEPARTMENT:
workerGroup.insert(make_pair(DEVELOP_DEPARTMENT, *it));
break;
case FINACIAL_DEPARTMENT:
workerGroup.insert(make_pair(FINACIAL_DEPARTMENT, *it));
break;
default:
break;
}
}
} void ShowWorkerGroup(multimap<int, Worker>& workerGroup,int DepartID)
{
multimap<int, Worker>::iterator it = workerGroup.find(DepartID);
//找当前部门总人数
int workerCount = workerGroup.count(DepartID);
int num = ;
for (multimap<int, Worker>::iterator dit = it; dit != workerGroup.end() && num < workerCount; dit++, num++)
{
cout << "姓名:" << dit->second.mName << " 年龄:" << dit->second.mAge << " 电话:" << dit->second.mTele << " 工资:" << dit->second.mSalary << endl;
}
}
//打印每一分组员工信息
void PrintWorkerGroup(multimap<int, Worker>& workerGroup)
{
//打印销售部门员工的信息
cout << "销售部门:" << endl;
ShowWorkerGroup(workerGroup, SALE_DEPARTMENT);
//打印研发部门员工信息
cout << "研发部门:" << endl;
ShowWorkerGroup(workerGroup, DEVELOP_DEPARTMENT);
//打印财务部门员工信息:
cout << "财务部门:" << endl;
ShowWorkerGroup(workerGroup, FINACIAL_DEPARTMENT); } int main()
{
//存放新员工信息
vector<Worker> vWorker;
//multimap保存分组信息
multimap<int, Worker> workerGroup;
//创建员工
CreatWorker(vWorker);
//员工分组
WorkerGroup(vWorker, workerGroup);
//打印每一分组员工信息
PrintWorkerGroup(workerGroup);
return ;
}
运行结果: