#include<iostream>
using namespace std;
#include<map>
#include<vector>
#include<ctime>
#define CeHua 0
const int MeiShu = 1;
#define YanFa 2
//员工分组
class Worker
{
public:
string m_Name;
int m_Salary;//工资
};
void creatWorker(vector<Worker>&v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker p;
p.m_Name = "员工";
p.m_Name += nameSeed[i];
p.m_Salary = rand() % 10000 + 10000;//生成10000-19999的随机数
//p.m_Salary = rand() % 100001 + 10000;//生成10000-20000的随机数
v.push_back(p);
}
}
//员工分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptId = rand() % 3;//产生0-2的随机数
//将员工插入到分组中
//key是部门编号,value是具体员工
m.insert(pair<int, Worker>(deptId, *it));
}
}
void showWorkerByGroup(multimap<int, Worker>&m)
{
cout << "策划部门:" << endl;
//怎么把策划部门的人都打出来呢?
//因为我们的key是升序排列的,所以可以
multimap<int, Worker>::iterator be,en;
be = m.find(CeHua);
en = m.find(MeiShu);
//第一种方式
for (;be!=en;be++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
//第二种方式
be = m.find(CeHua);
int num = m.count(CeHua);//统计策划部门具体人数
int index = 0;
for (; be != m.end() && index < num; be++, index++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
cout << "--------------------------------" << endl;
//美术部门
cout << "美术部门:" << endl;
be = m.find(MeiShu);
en = m.find(YanFa);
//第一种方式
for (; be != en; be++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
//第二种方式
be = m.find(MeiShu);
num = m.count(MeiShu);//统计策划部门具体人数
index = 0;
for (; be != m.end() && index < num; be++, index++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
cout << "--------------------------------" << endl;
//美术部门
cout << "研发部门:" << endl;
be = m.find(YanFa);
//第一种方式
for (; be != m.end(); be++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
//第二种方式
be = m.find(YanFa);
num = m.count(YanFa);//统计策划部门具体人数
index = 0;
for (; be != m.end() && index < num; be++, index++)
{
cout << "姓名:" << be->second.m_Name << " 薪水:" << be->second.m_Salary << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//创建员工
vector<Worker>vWorker;
creatWorker(vWorker);
测试
//for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
//{
// cout << it->m_Name << " " << it->m_Salary << endl;
//}
//
//2.员工分组
multimap<int, Worker>mWorker;//key值部门编号
setGroup(vWorker, mWorker);
//3.分组显示员工
showWorkerByGroup(mWorker);
system("pause");//按任意键继续
return 0;
}
STL案例员工分组,随机数,把键对应的所有value输出
最新推荐文章于 2024-08-19 18:41:12 发布