34.1案例描述:
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
员工信息有:姓名、工资组成;部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息插入 key(部门编号)value(员工)
分部门显示员工信息
34.2实现步骤
创建10名员工,放到vector中
遍历vector容器,取出每个员工,进行随机分组
分组后,将员工部门编号作为key,具体员工作为value,放到multimap容器中
分部门显示员工信息
34.3自己实现的:
#include <iostream>
#include <map>
#include<vector>
#include<ctime>
using namespace std;
class Person
{
public:
Person(string name, int wage)
{
this->m_Name = name;
this->m_Wage = wage;
}
string m_Name;
int m_Wage;
};
void printEmployeeend(multimap<int, Person>& e)
{
for (multimap<int, Person>::iterator it = e.begin(); it != e.end(); it++)
{
switch (it->first)
{
case 1:
cout << "部门为:策划" << " 员工名字:" << it->second.m_Name << " 员工工资:" << it->second.m_Wage << endl;
break;
case 2:
cout << "部门为:美术" << " 员工名字:" << it->second.m_Name << " 员工工资:" << it->second.m_Wage << endl;
break;
case 3:
cout << "部门为:研发" << " 员工名字:" << it->second.m_Name << " 员工工资:" << it->second.m_Wage << endl;
break;
default:
break;
}
}
}
void test01()
{
vector<Person>employee;
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
int randroom = rand() % 4000 + 3000;
string name = "员工";
name += nameSeed[i];
Person p(name, randroom);
employee.push_back(p);
}
multimap<int, Person>employeeend;
for (int j = 0; j < 10; j++)
{
int sectionID = rand() % 3 + 1;
Person peo= employee.back();
employeeend.insert(pair<int, Person>(sectionID, peo));
employee.pop_back();
}
printEmployeeend(employeeend);
}
int main()
{
srand((unsigned int)time(NULL));
test01();
system("pause");
return 0;
}
部门为:策划 员工名字:员工G 员工工资:4965
部门为:策划 员工名字:员工B 员工工资:6629
部门为:美术 员工名字:员工J 员工工资:3387
部门为:研发 员工名字:员工I 员工工资:5753
部门为:研发 员工名字:员工H 员工工资:3320
部门为:研发 员工名字:员工F 员工工资:5527
部门为:研发 员工名字:员工E 员工工资:6902
部门为:研发 员工名字:员工D 员工工资:4599
部门为:研发 员工名字:员工C 员工工资:4038
部门为:研发 员工名字:员工A 员工工资:3515
黑马实现:
#include <iostream>
#include <map>
#include<vector>
#include<ctime>
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
using namespace std;
class Worker
{
public:
string m_Name;
int m_Salary;
};
void createWorker(vector<Worker>& v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10000 + 10000;
v.push_back(worker);
}
}
//员工分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptID = rand() % 3;
//将员工插入到分组中
m.insert(make_pair(deptID, *it));
}
}
void showWorkerByGourp(multimap<int, Worker>& m)
{
//遍历
cout << "策划部门:" << endl;
multimap<int, Worker>::iterator pos = m.find(CEHUA);//pos位置
int count = m.count(CEHUA);//统计策划部门具体人数
int index = 0;//索引值
for (; pos != m.end() && index < count; pos++, index++)//index < count 的作用是确保只显示属于当前分组的员工数量
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
cout << "-------------------------------" << endl;
cout << "美术部门:" << endl;
pos = m.find(MEISHU);
count = m.count(MEISHU);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
cout << "-------------------------------" << endl;
cout << "研发部门:" << endl;
pos = m.find(YANFA);
count = m.count(YANFA);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
//创建员工
vector<Worker>vWorker;
createWorker(vWorker);
//员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
//分组显示员工
showWorkerByGourp(mWorker);
system("pause");
return 0;
}
策划部门:
姓名:员工A工资:18388
姓名:员工B工资:12425
姓名:员工D工资:13401
-------------------------------
美术部门:
姓名:员工F工资:10457
姓名:员工G工资:19652
-------------------------------
研发部门:
姓名:员工C工资:16464
姓名:员工E工资:18850
姓名:员工H工资:16180
姓名:员工I工资:19393
姓名:员工J工资:12785
注意,工作时不要把名字作为key,防止重名