#include<iostream>
using namespace std;
#include"list"
#include<algorithm>
#include"vector"
#include"string.h"
#include"map"
#include"ctime"
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker {
public:
string m_name;
int m_salary;
};
void creatworker(vector<Worker>& v) {
string nameseed = "ABCDE";
for (int i = 0; i < 5; i++) {
Worker worker;
worker.m_name = "员工";
worker.m_name += nameseed[i];
worker.m_salary = rand() % 10000 + 10000;
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 depid = rand() % 3;
m.insert(make_pair(depid, *it));
}
}
void printgroup(multimap<int,Worker>& m) {
cout << "策划部门:" << endl;
multimap<int, Worker>::iterator pos = m.find(CEHUA);
int count = m.count(CEHUA);
for (int index = 0;pos!=m.end()&&index <count;pos++, index++) {
cout << pos->second.m_name << " " << pos->second.m_salary;
}
cout << endl;
cout << "美术部门:" << endl;
pos = m.find(MEISHU);
int count2 = m.count(MEISHU);
for (int index = 0; pos != m.end() && index < count2; pos++,index++) {
cout << pos->second.m_name << " " << pos->second.m_salary;
}
cout << endl;
cout << "研发部门:" << endl;
pos = m.find(YANFA);
int count3 = m.count(YANFA);
for (int index = 0; pos != m.end() && index < count3; pos++,index++) {
cout << pos->second.m_name << " " << pos->second.m_salary;
}
cout << endl;
}
int main()
{
srand((unsigned)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;
}
multimap<int, Worker>mworker;
setgroup(vworker, mworker);
printgroup(mworker);
system("pause");
return 0;
}
【C++】员工排序,vector、multimap
最新推荐文章于 2022-07-23 09:49:18 发布