#include<iostream>
using namespace std;
#include<map>
#include<vector>
#include<ctime>
//案例描述
//10名员工(ABCDEFGHIJ)进入公司之后,需要指派其在哪个部门工作
//员工信息有:姓名,工资;部门分为:策划,美术和研发
//随机给10名员工分配部门和工资
//通过multimap进行信息的插入,key(部门编号)value(员工)
//分部门显示员工信息
//实现步骤
//1.创建10名员工,放到vector中
//2.遍历vector容器,取出每个员工,进行随机分组
//3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
//4.分部门显示员工信息
#define CEHUA 1
#define MEISHU 2
#define YANFA 3
class Person
{
public:
Person(string name, int salary)
{
this->m_Name = name;
this->m_Salary = salary;
}
string m_Name;
int m_Salary;
};
void PrintPerosn(vector<Person> v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_Name << "\t" << "薪资:" << it->m_Salary << endl;
}
}
//1.创建10名员工,放到vector中
void CreatPerson(vector<Person>& v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
string name = "员工";
name += nameSeed[i];
int salary = rand() % 10000 + 10000;
Person p(name, salary);
v.push_back(p);
}
}
//2.遍历vector容器,取出每个员工,进行随机分组
void SetGroup(vector<Person>& v, multimap<int, Person>& m)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
int key = rand() % 3 + 1;//随机部门
m.insert(make_pair(key, (*it)));//把(部门,员工)插入map容器中
}
}
//3.分部门显示员工信息
void Print(multimap<int, Person> m)
{
multimap<int, Person>::iterator it = m.begin();
int num = m.count(CEHUA);
cout << "策划部门:" << num<<"个" << endl;
for (int i=0;i<num;i++)
{
cout << "姓名:" << it->second.m_Name << "\t" << "工资:" << it->second.m_Salary << endl;
it++;
}
num = m.count(MEISHU);
cout << "美术部门:" <<num<<"个"<< endl;
for (int i = 0; i < num; i++)
{
cout << "姓名:" << it->second.m_Name << "\t" << "工资:" << it->second.m_Salary << endl;
it++;
}
num = m.count(YANFA);
cout << "研发部门:" <<num<<"个"<< endl;
for (int i = 0; i < num; i++)
{
cout << "姓名:" << it->second.m_Name << "\t" << "工资:" << it->second.m_Salary << endl;
it++;
}
}
int main()
{
srand((unsigned int)time(NULL));
vector<Person>v;
//1.创建10名员工,放到vector中
CreatPerson(v);
//test
//PrintPerosn(v);
//2.遍历vector容器,取出每个员工,进行随机分组
multimap<int, Person> m;
SetGroup(v,m);
//3.分部门显示员工信息
Print(m);
system("pause");
return 0;
}
网友评论