STL——案例-员工分组

公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作员工信息有: 姓名 工资组成;部门分为:策划、美术、研发随机给10名员工分配部门和工资通过multimap进行信息的插入 key(部门编号) value(员工)分部门显示员工信息

实现步骤

  1. 创建10名员工,放到vector中

  2. 遍历vector容器,取出每个员工,进行随机分组

  3. 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中

  4. 分部门显示员工信息

#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
class worker
{
public:
string name;
int salary;
};
void CreatePlayer(vector<worker> &a)
{
char b[] = "ABCDEFGHIJ";
int i = 0;
for(;i<10;i++)
{
worker WORKER;
WORKER.name = b[i];
WORKER.salary = rand() % 5000 + 5000;
a.push_back(WORKER);
}
}
void MapWorker(vector<worker>&vec,multimap<int,worker>&mp)
{
int ID;
for (int i = 0; i < 10; i++)
{
pair<int, worker> temp;
ID = rand() % 3;
temp.first = ID;
temp.second = vec[i];
mp.insert(temp);
}

}
void ShowWork(multimap<int, worker>& mp)
{
//先输出编号为0的员工信息
for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 0)
{
cout << "策划部" << "姓名" << begin->second.name << "工资" << begin->second.salary << endl;
}
}
for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 1)
{
cout << "美术部" << "姓名" << begin->second.name << "工资" << begin->second.salary << endl;
}
} for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 2)
{
cout << "研发部" << "姓名" << begin->second.name << "工资" << begin->second.salary << endl;
}
}
}
void test()
{
//创建员工
vector<worker> WORKERS;
CreatePlayer(WORKERS);

//员工分组
multimap<int, worker> mWORKERS;
MapWorker(WORKERS,mWORKERS);

//分组显示员工
ShowWork(mWORKERS);
}
int main()
{
test();
return 0;
}