STL案例_员工分组
STL案例_员工分组
#include<iostream>
using namespace std;
#include<map>
#include<vector>
#include<ctime>
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
/*
-公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
- 员工信息有:姓名工资组成; 部门分为:策划、美术、研发
–随机给10名员工分配部门和工资
- 通过multimap进行信息的插入key(部门编号) value(员工)
- 分部门显示员工信息
*/
class worker {
public:
string m_name;
int m_salary;
};
void createWorkers(vector<worker> &v)
{
string workerSeed = "ABCDEFGHIJ";
for (int i = 0; i < workerSeed.size(); i++)
{
worker worker;
worker.m_name = "员工";
worker.m_name += workerSeed[i];
worker.m_salary = rand() % 10000 + 10000; //10000-19999
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 departID = rand() % 3; // 0 1 2
//将员工插入分组
//key部门,value 员工
//cout << departID <<"姓名" <<it->m_name << endl;
m.insert(pair<int, worker>(departID, *it));
}
}
void showWorkerGroup(multimap<int, worker> &m)
{
cout << "策划部门" << endl;
multimap<int, worker>::iterator pos = m.find(CEHUA);
int count = m.count(CEHUA);
int 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(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;
}
cout << "------" << endl;
}
int main()
{
srand((unsigned int)time (NULL));
//1.创建员工
vector<worker> v;
createWorkers(v);
//测试
/*for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
{
cout << it->m_name << " " << it->m_salary << endl;
}*/
//2.员工分组
multimap<int, worker> m;
setGroup(v, m);
//3.分组显示员工
showWorkerGroup(m);
system("pause");
return 0;
}
评论列表 (0 条评论)