基于C++ STL实现 员工
multimap vector
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctime>
#define CHEHUA 0
#define MEISHU 1
#define GUANLI 2
using namespace std;
class worker {
public:
string name;
int salary;
};
void creat_worker(vector<worker>&v) {
string nameseed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++) {
worker worker;
worker.name = "员工";
worker.name += nameseed[i];
worker.salary = rand() % 10000 + 10001;
v.push_back(worker);
}
}
void set_group(vector<worker>& v, multimap<int, worker>& m) {
for (vector<worker>::iterator it = v.begin(); it != v.end(); it++) {
int depid = rand() % 3;// 0 1 2
m.insert(make_pair(depid, *it));//员工插入部门
}
}
void show(multimap<int, worker>&m) {
cout << "--------------------------------------------------" << endl;
cout << "策划部门:" << endl;
multimap<int, worker>::iterator pos = m.find(CHEHUA);
int count = m.count(CHEHUA);
int index = 0;
for (; pos != m.end()&&index<=count; pos++,index++) {
cout << "姓名:" << pos->second.name<< "工资:" << pos->second.salary<< endl;
}
cout << "--------------------------------------------------" << endl;
cout << "美术部门:" << endl;
multimap<int, worker>::iterator pos1 = m.find(MEISHU);
int count1 = m.count(MEISHU);
int index1 = 0;
for (; pos1 != m.end() && index1 <= count1; pos1++, index1++) {
cout << "姓名:" << pos1->second.name << "工资:" << pos1->second.salary << endl;
}
cout << "--------------------------------------------------" << endl;
cout << "管理部门:" << endl;
multimap<int, worker>::iterator pos2 = m.find(GUANLI);
int count2 = m.count(GUANLI);
int index2 = 0;
for (; pos2 != m.end() && index2 <= count2; pos2++, index2++) {
cout << "姓名:" << pos2->second.name << "工资:" << pos2->second.salary << endl;
}
cout << "--------------------------------------------------" << endl;
}
int main() {
//随机数种子
srand((unsigned int)time(NULL));
vector<worker> worker1;
creat_worker(worker1);
for (vector<worker>::iterator it = worker1.begin(); it != worker1.end(); it++) {
cout << "姓名:" <<it->name<<" 工资:" <<(*it).salary << endl;
}
//分组
multimap<int, worker>mworker;
set_group(worker1,mworker);
//输出
show(mworker);
return 0;
}