基于C++ STL实现 员工随机分组

基于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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值