/*
公司招聘n个员工,(ABCDEFGH),员工进入公司后需要指派到各部门工作
员工信息有:姓名,年龄,工资;部门分为算法,运营,设计,产品
随机给员工分配部门和工资
通过multimap进行信息的插入,key(部门编号),value(员工)
分部门显示员工信息
*/
/*实现步骤
1.创建N名员工,放入到vector中
2.遍历vector容器,取出每个员工,进行随机分组
3.分组后将员工部门编号作为key,员工作为value,放入到multimap中
4.分部门显示员工信息
*/
#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
#define staff_num 12
#define Algorithm_id 1
#define Operation_id 2
#define Design_id 3
#define Product_id 4
class staff
{
public:
staff(string name, int age, int salary)
{
this->m_name = name;
this->m_age = age;
this->m_salary = salary;
}
string m_name;
int m_age;
int m_salary;
};
void creatstaff(vector<staff> &v)
{
string name_seed[staff_num];
string name0 = "Staff_";
char name1 = 'A';
for (int i = 0; i < staff_num; i++)
{
name_seed[i] = name0 + name1;
name1++;
}
srand((unsigned int)time(NULL));
for (int i = 0; i < staff_num; i++)
{
v.push_back(staff(name_seed[i], rand() % 21 + 30, rand() % 41 + 20));
}
}
void printstaff(const vector<staff> &v)
{
cout << "Staff info: " << endl;
for (vector<staff>::const_iterator it = v.begin(); it != v.end(); it++)
{
cout << "Name: " << it->m_name << "\tAge: " << it->m_age << "\t\tSalary: " << it->m_salary <<"W" << endl;
}
cout << endl;
}
void printstaff(const multimap<int,staff>& m)
{
cout << "Staff info: (multimap) " << endl;
for (multimap<int,staff>::const_iterator it=m.begin(); it != m.end(); it++)
{
cout << "Department: " << it->first << "\t" << it->second.m_name << "\t\tAge: "
<< it->second.m_age << "\t\tSalary: " << it->second.m_salary << "W" << endl;
}
cout << endl;
}
void showstaff(multimap<int, staff>& m)
{
cout << "=========================Algorithm=========================" << endl;
multimap<int, staff>::iterator pos = m.find(Algorithm_id);
int count = m.count(Algorithm_id);
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
}
cout << endl;
cout << "=========================Operation=========================" << endl;
pos = m.find(Operation_id);
count = m.count(Operation_id);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
}
cout << endl;
cout << "==========================Design===========================" << endl;
pos = m.find(Design_id);
count = m.count(Design_id);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
}
cout << endl;
cout << "==========================Product==========================" << endl;
pos = m.find(Product_id);
count = m.count(Product_id);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
}
}
void setgroupstaff(vector<staff> &v, multimap<int, staff> &m)
{
srand((unsigned int)time(NULL));
for (vector<staff>::const_iterator it = v.begin(); it != v.end(); it++)
{
int deptid = rand()%4 + 1;
m.insert(make_pair(deptid, *it));
}
}
int main()
{
vector<staff> v_staff;
creatstaff(v_staff);
multimap<int, staff> m_staff;
setgroupstaff(v_staff,m_staff);
//printstaff(m_staff);
showstaff(m_staff);
system("pause");
return 0;
}