题目描述
- 公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作
- 人员信息有:姓名年龄电话工资等组成
- 通过multimap进行信息的插入保存显示
- 分部门显示员工信息显示全部员工信息
代码
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <map>
using namespace std;
enum {RENLI,MEISHU,YANFA};
class Person
{
public:
Person(string name, int money):m_Name(name),m_money(money) {}
public:
string m_Name;
int m_money;
};
void createWorker(vector<Person> &v)
{
string nameSeed = "ABCDE";
for (int i = 0;i<5;i++)
{
string name = "员工";
name += nameSeed[i];
int money = rand() % 10001 + 10000;
Person p(name,money);
v.push_back(p);
}
}
void setGroup(vector<Person>&v,multimap<int,Person>&m)
{
for (vector<Person>::iterator it = v.begin();it != v.end();it++)
{
//随机产生部门编号
int departId = rand() % 3;
//将员工分到mutimap容器中
m.insert(make_pair(departId,*it));
}
}
void showGroup(multimap<int,Person>&m)
{
cout << "RENLI:" << endl;
multimap<int, Person>::iterator pos = m.find(RENLI);
int index = 0;
int num = m.count(RENLI);
for (;pos != m.end(),index<num;pos++,index++)
{
cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
}
cout << "MEISHU:" << endl;
pos = m.find(MEISHU);
index = 0;
num = m.count(MEISHU);
for (; pos != m.end(), index < num; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
}
cout << "YANFA:" << endl;
pos = m.find(YANFA);
index = 0;
num = m.count(YANFA);
for (; pos != m.end(), index < num; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
}
}
int main()
{
//创建随机数种子
srand((unsigned int)time(NULL));
//创建5个员工
vector<Person>v;
createWorker(v);
//员工创建测试
/*for (vector<Person>::iterator it = v.begin(); it != v.end();it++)
{
cout << "name:" << it->m_Name << " " << "money:" << it->m_money << endl;
}*/
//设置分组,分组的multimap容器
multimap<int, Person>m;
setGroup(v,m);
//分部门显示员工
showGroup(m);
return 0;
}