multimap员工分类案例

根据C++学习视频写的一个小案例,巩固multimap容器的学习。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;

#define SALE_DEPARTMENT 1 //销售部门
#define DEVELOP_DEPARTMEN 2 //研发部门
#define FINACIAL_DEPARTMENT 3 //财务部门

class Worker{
public:
public:
	string mName;
	string mTele;
	int mAge;
	int mSalary;
};

//创建员工
void Create_Worker(vector<Worker>& vWorker){
	string seedName = "ABCDE";
	for (int i = 0; i < 5; i++){
		Worker worker;
		worker.mName = "员工";
		worker.mName += seedName[i];
		worker.mAge = rand() % 10 + 20;
		worker.mTele = "13227848860";
		worker.mSalary = rand() % 10000 + 10000;
		//保存员工信息
		vWorker.push_back(worker);
	}
}

//员工分组
void WorkerByGroup(vector<Worker>& vWorker, multimap<int, Worker>& WorkerGroup){
	//把员工随机分配到不同部门
	srand(time(NULL));
	for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++){
		int departID = rand() % 3 + 1;
		switch (departID){
		case SALE_DEPARTMENT:
			WorkerGroup.insert(make_pair(SALE_DEPARTMENT, *it));
			break;
		case DEVELOP_DEPARTMEN:
			WorkerGroup.insert(make_pair(DEVELOP_DEPARTMEN, *it));
			break;
		case FINACIAL_DEPARTMENT:
			WorkerGroup.insert(make_pair(FINACIAL_DEPARTMENT, *it));
			break;
		default:
			break;
		}
	}
}

void ShowGroupWorkers(multimap<int, Worker>& WorkerGroup, int departID){
	multimap<int, Worker>::iterator it = WorkerGroup.find(departID);
	int DepartCount = WorkerGroup.count(departID);
	int num = 0;
	for (multimap<int, Worker>::iterator pos = it; pos != WorkerGroup.end() && num < DepartCount; pos++, num++){
		cout << "姓名:" << pos->second.mName
			<< " 年龄:" << pos->second.mAge
			<< " 电话:" << pos->second.mTele
			<< " 薪水:" << pos->second.mSalary
			<< endl;
	}
}

//打印每一部门的员工信息
void PrintWorkerByGroup(multimap<int, Worker>& WorkerGroup){
	cout << "销售部门员工:" << endl;
	ShowGroupWorkers(WorkerGroup, SALE_DEPARTMENT);
	cout << "研发部门员工:" << endl;
	ShowGroupWorkers(WorkerGroup, DEVELOP_DEPARTMEN);
	cout << "财务部门员工:" << endl;
	ShowGroupWorkers(WorkerGroup, FINACIAL_DEPARTMENT);
}

int main(void){

	//存放新员工信息
	vector<Worker> vWorker;
	//multimap保存分组信息
	multimap<int, Worker> WorkerGroup;
	//创建员工
	Create_Worker(vWorker);
	//员工分组
	WorkerByGroup(vWorker, WorkerGroup);
	//打印每一部门的员工信息
	PrintWorkerByGroup(WorkerGroup);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值