//
// Created by shexuan on 2021/1/23.
//
/* 案例-员工分组
* 案例描述:
* - 公司招聘10个员工(ABCDEFGHIJ),10名员工进入公司后,需要指派员工在哪个部门工作;
* - 员工信息有:姓名,工资;部门有:策划、美术、研发;
* - 随机给10名员工分配部门和工资;
* - 通过multimap进行信息的插入,key(部门编号) value(员工);
* - 分部门显示信息.
*/
/* 实现步骤:
* (1)创建10名员工,存入vector容器中;
* (2)遍历vector容器,取出每个员工,随机进行分组;
* (3)分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中;
* (4)分部门显示信息;
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctime>
using namespace std;
class Worker{
public:
Worker(string name, int salary){
this->m_name = name;
this->m_salary = salary;
}
string m_name;
int m_salary;
};
void CreateWorkers(vector<Worker> &v){
srand((unsigned)time(NULL));
string names = "ABCDEFGHIJ";
for (string::iterator it=names.begin(); it!=names.end(); it++){
string name = "姓名";
name += *it;
int salary = rand()%10000 + 10000;
Worker w(name, salary);
v.push_back(w);
}
}
void SetGroup(const vector<Worker> &v, multimap<string, Worker> &mmap){
for (vector<Worker>::const_iterator it=v.begin();it!=v.end();it++){
int dept_id = rand()%3;
switch (dept_id){
case 0:
mmap.insert(pair<string, Worker>("策划", *it));
break;
case 1:
mmap.insert(pair<string, Worker>("美术", *it));
break;
case 2:
mmap.insert(pair<string, Worker>("研发", *it));
break;
default:
cout << "ERROR DEPT CODE? Impossible!" << endl;
break;
}
}
}
void printGroupWorkers(const multimap<string, Worker> &mmap){
// 分部门打印员工
cout << "策划部门:" << endl;
multimap<string, Worker>::const_iterator dept_pos;
dept_pos = mmap.find("策划");
int num;
num = mmap.count("策划");
for (int i=0;i<num; i++, dept_pos++){
cout << "\tName:" << dept_pos->second.m_name
<< "\tSalary: " << dept_pos->second.m_salary
<< endl;
}
cout << "美术部门:" << endl;
dept_pos = mmap.find("美术");
num = mmap.count("美术");
for (int i=0;i<num; i++, dept_pos++){
cout << "\tName:" << dept_pos->second.m_name
<< "\tSalary: " << dept_pos->second.m_salary
<< endl;
}
cout << "研发部门:" << endl;
dept_pos = mmap.find("研发");
num = mmap.count("研发");
for (int i=0;i<num; i++, dept_pos++){
cout << "\tName:" << dept_pos->second.m_name
<< "\tSalary: " << dept_pos->second.m_salary
<< endl;
}
}
int main(){
// 创建员工
vector<Worker> v;
CreateWorkers(v);
// 进行分组
multimap<string, Worker> mmap;
SetGroup(v, mmap);
for (multimap<string, Worker>::iterator it=mmap.begin(); it!=mmap.end();it++){
cout << "部门:"<< (*it).first
<< "\tName: " << (*it).second.m_name
<< "\tSalary: " << (*it).second.m_salary
<< endl;
}
// 分组打印
printGroupWorkers(mmap);
}
网友评论