C++ 利用容器创建部门员工信息并显示

/*
公司招聘n个员工,(ABCDEFGH),员工进入公司后需要指派到各部门工作
员工信息有:姓名,年龄,工资;部门分为算法,运营,设计,产品
随机给员工分配部门和工资
通过multimap进行信息的插入,key(部门编号),value(员工)
分部门显示员工信息
*/
/*实现步骤
1.创建N名员工,放入到vector中
2.遍历vector容器,取出每个员工,进行随机分组
3.分组后将员工部门编号作为key,员工作为value,放入到multimap中
4.分部门显示员工信息
*/

#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
#define staff_num 12
#define Algorithm_id 1
#define Operation_id 2
#define Design_id 3
#define Product_id 4
class staff
{
public:
	staff(string name, int age, int salary)
	{
		this->m_name = name;
		this->m_age = age;
		this->m_salary = salary;
	}
	string m_name;
	int m_age;
	int m_salary;
};
void creatstaff(vector<staff> &v)
{
	string name_seed[staff_num];
	string name0 = "Staff_";
	char name1 = 'A';
	for (int i = 0; i < staff_num; i++)
	{
		name_seed[i] = name0 + name1;
		name1++;
	}
	srand((unsigned int)time(NULL));
	for (int i = 0; i < staff_num; i++)
	{
		v.push_back(staff(name_seed[i], rand() % 21 + 30, rand() % 41 + 20));
	}
}
void printstaff(const vector<staff> &v)
{
	cout << "Staff info: " << endl;
	for (vector<staff>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "Name: " << it->m_name << "\tAge: " << it->m_age << "\t\tSalary: " << it->m_salary <<"W" << endl;
	}
	cout << endl;
}
void printstaff(const multimap<int,staff>& m)
{
	cout << "Staff info: (multimap) " << endl;
	for (multimap<int,staff>::const_iterator it=m.begin(); it != m.end(); it++)
	{
		cout << "Department: " << it->first << "\t" << it->second.m_name << "\t\tAge: "
			<< it->second.m_age << "\t\tSalary: " << it->second.m_salary << "W" << endl;
	}
	cout << endl;
}
void showstaff(multimap<int, staff>& m)
{
	cout << "=========================Algorithm=========================" << endl;
	multimap<int, staff>::iterator pos = m.find(Algorithm_id);
	int count = m.count(Algorithm_id);
	int index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "=========================Operation=========================" << endl;
	pos = m.find(Operation_id);
	count = m.count(Operation_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "==========================Design===========================" << endl;
	pos = m.find(Design_id);
	count = m.count(Design_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "==========================Product==========================" << endl;
	pos = m.find(Product_id);
	count = m.count(Product_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
}
void setgroupstaff(vector<staff> &v, multimap<int, staff> &m)
{
	srand((unsigned int)time(NULL));
	for (vector<staff>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		int deptid = rand()%4 + 1;
		m.insert(make_pair(deptid, *it));
	}
}
int main()
{
	vector<staff> v_staff;
	creatstaff(v_staff); 
	multimap<int, staff> m_staff;
	setgroupstaff(v_staff,m_staff);

	//printstaff(m_staff);
	showstaff(m_staff);
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yvie_Bae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值