案例描述
公司今天招聘了10个员工,10名员工进入公司之后,需要指派员工在那个部门工作
员工信息有:姓名 工资组成;部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息的插入 key(部门编号)value(员工)
分部门显示员工信息
实现步骤
创建10名员工,放到vector中
遍历vector容器,取出每个员工,进行随机分组
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
分部门显示员工信息
代码实现
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;
#define cehua 0
#define meishu 1
#define yanfa 2
class Worker
{
public:
string m_Name;//员工姓名
int m_Salary;//员工工资
};
void CreateWorker(vector<Worker>&v)
{
string name = "ABCDEFGHIJ";
for(int i = 0;i<10;i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name +=name[i];//姓名
worker.m_Salary = rand() % 10000 + 10000;//10000 ~ 19999
//将员工放入到容器中
v.push_back(worker);
}
}
void group(vector<Worker>&v,multimap<int,Worker>&m)
{
//遍历员工
for(vector<Worker>::iterator it = v.begin();it != v.end();it++)
{
//产生随机部门编号
int n = rand() % 3;
//将编号和员工容器放到m中
m.insert(make_pair(n,*it));
}
}
void show(multimap<int,Worker>&v)
{
cout << "策划部门: "<< endl;
multimap<int,Worker>::iterator pos = v.find(cehua);
int c = v.count(cehua);
int i = 0;
for(;pos!= v.end() && i < c;i++,pos++)
{
cout << "姓名: "<< pos->second.m_Name<<"工资: "<<pos->second.m_Salary<<endl;
}
cout << "-------------------" << endl;
cout << "美术部门: "<< endl;
pos = v.find(meishu);
c = v.count(meishu);
i = 0;
for(;pos!= v.end() && i < c;i++,pos++)
{
cout << "姓名: "<< pos->second.m_Name<<"工资: "<<pos->second.m_Salary<<endl;
}
cout << "-------------------" << endl;
cout << "研发部门: "<< endl;
pos = v.find(yanfa);
c = v.count(yanfa);
i = 0;
for(;pos!= v.end() && i < c;i++,pos++)
{
cout << "姓名: "<< pos->second.m_Name<<"工资: "<<pos->second.m_Salary<<endl;
}
}
int main()
{
//创建员工
vector<Worker>vworker;
CreateWorker(vworker);
//分组操作
multimap<int,Worker>m;
group(vworker,m);
//分组显示员工
show(m);
return 0;
}
编译运行