stl案例2之员工分组

2021-03-02 15:52:22 浏览数 (1)

案例描述:

实现步骤:

代码语言:javascript复制
#include<iostream>
using namespace std;
#include<map>
#include<string>
#include<vector>
#include<ctime>
#define CEHUA 1
#define MEISHU 2
#define YANFA 3
//员工类
class Worker
{
public:
	//无参构造
	Worker(){}
	//有参构造函数进行初始化赋值
	Worker(string name,int salary,int department):name(name),salary(salary),department(department){}
	//获取姓名
	string getName() { return name; }
	//设置姓名
	void setName(string name) { this->name = name; }
	//获取工资
	int  getSalary() { return salary; }
	//设置工资
	void setSalary(int salary) { this->salary = salary; }
	//获取部门
	int  getDepartment() { return department; }
	//设置部门
	void setDepartment(int dp) { department = dp; }
	string name; //员工姓名
	int salary; //员工工资
	int department;//部门
};
//随机分配部门和工资
void random(vector<Worker>& wV);
//打印输出
void print(vector<Worker>& wV);
//将十名员工放入vector容器中
void  creatWorker(vector<Worker>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i  )
	{
		Worker w;
		w.name = "员工";
		w.name  = nameSeed[i];
		v.push_back(w);
    }
	random(v);
	//print(v);
}
//随机分配部门和工资
void random(vector<Worker>& wV)
{
	//随机给每名员工分配部门和工资
	srand((unsigned int)time(NULL));
	for (int i = 0; i < wV.size(); i  )
	{
		int money = rand() % 10001   6000;
		wV[i].setSalary(money);
		int departID = rand() % 3   1;
		wV[i].setDepartment(departID);
	}
}
//VECTOR打印输出
void print(vector<Worker>& wV)
{
	for (int i = 0; i < wV.size(); i  )
	{
		if (wV[i].getDepartment() == CEHUA)
		{
			cout << "员工的姓名: " << wV[i].getName() << "  t员工的工资: " << wV[i].getSalary() << "  t员工的部门:  策划" << endl;
		}
		if (wV[i].getDepartment() == MEISHU)
		{
			cout << "员工的姓名: " << wV[i].getName() << "  t员工的工资: " << wV[i].getSalary() << "  t员工的部门:  美术" << endl;
		}
		if (wV[i].getDepartment() == YANFA)
		{
			cout << "员工的姓名: " << wV[i].getName() << "  t员工的工资: " << wV[i].getSalary() << "  t员工的部门:  研发" << endl;
		}
	}
}
//员工分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
	//按照key值,即部门编号进行分组
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it  )
	{
		m.insert(make_pair((*it).department, *it));
	}

}
//分别打印某个部门的人数
void showWorkerByGroup(multimap<int,Worker>& m)
{
	cout << "策划部门; " << endl;
	//查找map中某个key是否存在,存在就返回其迭代器------find()
	multimap<int, Worker>::iterator pos = m.find(CEHUA);
	//统计每个部门有几个人
	int  count = m.count(CEHUA);
	int index = 0;
	for (; pos != m.end() && index < count; index  , pos  )
	{
		cout << "姓名: " << (*pos).second.getName() << "  工资: " << (*pos).second.getSalary() << endl;
	}
	cout << "----------------------------" << endl;
	cout << "nn美术部门: " << endl;
	//查找map中某个key是否存在,存在就返回其迭代器------find()
	pos = m.find(MEISHU);
	//统计每个部门有几个人
	count = m.count(MEISHU);
	index = 0;
	for (; pos != m.end() && index < count; index  , pos  )
	{
		cout << "姓名: " << (*pos).second.getName() << "  工资: " << (*pos).second.getSalary() << endl;
	}
	cout << "----------------------------" << endl;
	cout << "nn研发部门: " << endl;
	//查找map中某个key是否存在,存在就返回其迭代器------find()
      pos = m.find(YANFA);
	//统计每个部门有几个人
	count = m.count(YANFA);
	index = 0;
	for (; pos != m.end() && index < count; index  , pos  )
	{
		cout << "姓名: " << (*pos).second.getName() << "  工资: " << (*pos).second.getSalary() << endl;
	}
}
int main()
{
	//1.创建员工
	vector<Worker> v;
	creatWorker(v);
	//2.员工分组
	multimap<int, Worker> m;
	setGroup(v, m);
	showWorkerByGroup(m);
	system("pause");
	return 0;
}
stl

0 人点赞