容器知识巩固小案例-员工分组(C++)

/*案例描述: 
	·公司今天招聘了10个员工(ABCDEFGHI),10名员工进入公司后,需要指派员工在哪个部门工作
	·员工信息有:姓名 工资;	部门分为:策划、美术、研发
	·随机给10名员工分配部门和工资
	·通过multimap进行信息插入key(部门编号) value(员工)
	·分部门显示员工信息
	
	
  实现步骤:
	·创建10名员工,放到vector中
	·遍历vector容器,取出每个员工,进行随机分组
	·分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
	·分部分显示员工信息 
*/ 
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
//枚举部门0plan\1art\2research
enum dept
{
    
    
	plan,
	art,
	research
};
//worker类
class Worker
{
    
    
public:
	string m_Name;
	int m_Salary;
};
//创建存储worker信息的vector容器
void createVector(vector<Worker>& v)
{
    
    
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < nameSeed.length(); i++) {
    
    
		int salary = rand() % 10000 + 10000;
		Worker w;
		w.m_Name = "员工";
		//w.m_Name = "员工" + nameSeed[i];	输出时出现乱码
		w.m_Name += nameSeed[i];
		w.m_Salary = salary;
		v.push_back(w);
	}
}
//随机分配部门,组成对组,存入multimap容器
void setGroup(vector<Worker> v, multimap<int, Worker>& m)
{
    
    
	for (vector<Worker> ::iterator it = v.begin(); it != v.end(); it++) {
    
    
		//产生随机部门编号
		int deptId = rand() % 3;	//0 1 2
		//将员工插入分组
		m.insert(make_pair(deptId, (*it)));
	}
}
//分部门打印出worker信息,注意find、count的运用
void showWorkerByGroup(const multimap<int, Worker>& m)
{
    
    
	cout << "策划部门:" << endl;
	multimap<int, Worker> :: const_iterator pos = m.find(plan);
	int num = m.count(plan);
	int index = 0;
	for (; pos != m.end() && index < num; pos++, index++) {
    
    
		cout << "姓名: " << pos->second.m_Name << "  薪资 : " << pos->second.m_Salary << endl;
	}

	cout << "美术部门:" << endl;
	pos = m.find(art);
	num = m.count(art);
	index = 0;
	for (; pos != m.end() && index < num; pos++, index++) {
    
    
		cout << "姓名: " << pos->second.m_Name << "  薪资 : " << pos->second.m_Salary << endl;
	}

	cout << "研发部门:" << endl;
	pos = m.find(research);
	num = m.count(research);
	index = 0;
	for (; pos != m.end() && index < num; pos++, index++) {
    
    
		cout << "姓名: " << pos->second.m_Name << "  薪资 : " << pos->second.m_Salary << endl;
	}

}

void printVector(const vector<Worker>& v)
{
    
    
	//const_iterator
	for (vector<Worker> ::const_iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << "name :  " << it -> m_Name << "  salary :  " << it->m_Salary << endl;
	}
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	//创建员工
	vector<Worker> vWorker;
	createVector(vWorker);
	//printVector(vWorker);
	//员工分组
	multimap<int, Worker> mWorker;
	setGroup(vWorker, mWorker);
	//分组显示
	showWorkerByGroup(mWorker);

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Fighting_gua_biu/article/details/114182269