案例描述
实现步骤
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<ctime>
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class person
{
public:
string m_Name;
int m_Salary;
};
void Employee(vector<person>&v)
{
srand((unsigned int)time(NULL));
string nameseed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
person p;
p.m_Name = "员工";
p.m_Name += nameseed[i];
p.m_Salary = rand()%10001+10000;//10000~20000
v.push_back(p);
}
}
void setGroup(vector<person> &v,multimap<int,person> &m)
{
for (vector<person>::iterator vit = v.begin(); vit != v.end(); vit++)
{
//产生随机部门编号
int id = rand() % 3; //0 1 2
//将员工插入到分组中
m.insert(make_pair(id, *vit));
}
}
void showEmployee(multimap<int, person>& m)
{
cout << "策划部门:" << endl;
multimap<int, person>::iterator pos1 = m.find(CEHUA);
int count1=m.count(CEHUA);
int index1 = 0;
for ( ; pos1 !=m.end()&&index1<count1; pos1++,index1++)
{
cout << "姓名: " << pos1->second.m_Name
<< "薪资: " << pos1->second.m_Salary<<endl;
}
cout << "-------------------------------" << endl;
cout << "美术部门:" << endl;
multimap<int, person>::iterator pos2 = m.find(MEISHU);
int count2 = m.count(MEISHU);
int index2 = 0;
for (; pos2 != m.end() && index2 < count2; pos2++, index2++)
{
cout << "姓名: " << pos2->second.m_Name
<< "薪资: " << pos2->second.m_Salary << endl;
}
cout << "-------------------------------" << endl;
cout << "研发部门:" << endl;
multimap<int, person>::iterator pos3 = m.find(YANFA);
int count3 = m.count(YANFA);
int index3 = 0;
for (; pos3 != m.end() && index3 < count3; pos3++, index3++)
{
cout << "姓名: " << pos3->second.m_Name
<< "薪资: " << pos3->second.m_Salary << endl;
}
}
int main()
{
//1.创建员工
vector<person>v;
Employee(v);
//2.员工分组
multimap<int, person>m;
setGroup(v, m);
//3.分组显示员工
showEmployee(m);
system("pause");
return 0;
}