案例---员工分组

该程序使用C++创建了一个person类,包含姓名和薪资属性。通过vector存储员工,用multimap按照部门(策划、美术、研发)进行分组。程序随机生成员工的姓名和薪资,并按部门展示员工信息。
摘要由CSDN通过智能技术生成

案例描述

 实现步骤

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<ctime>
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class person
{
public:
	string m_Name;
	int m_Salary;
};
void Employee(vector<person>&v)
{
	srand((unsigned int)time(NULL));
	string nameseed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		person p;
		p.m_Name = "员工";
		p.m_Name += nameseed[i];
		p.m_Salary = rand()%10001+10000;//10000~20000
		v.push_back(p);
	}
}
void setGroup(vector<person> &v,multimap<int,person> &m)
{
	for (vector<person>::iterator vit = v.begin(); vit != v.end(); vit++)
	{
		//产生随机部门编号
		int id = rand() % 3; //0 1 2
		//将员工插入到分组中
		m.insert(make_pair(id, *vit));
	}
}
void showEmployee(multimap<int, person>& m)
{
	cout << "策划部门:" << endl;
	multimap<int, person>::iterator pos1 = m.find(CEHUA);
	int count1=m.count(CEHUA);
	int index1 = 0;
	for ( ; pos1 !=m.end()&&index1<count1; pos1++,index1++)
	{
		cout << "姓名: " << pos1->second.m_Name
			<< "薪资: " << pos1->second.m_Salary<<endl;
	}
	cout << "-------------------------------" << endl;
	cout << "美术部门:" << endl;
	multimap<int, person>::iterator pos2 = m.find(MEISHU);
	int count2 = m.count(MEISHU);
	int index2 = 0;
	for (; pos2 != m.end() && index2 < count2; pos2++, index2++)
	{
		cout << "姓名: " << pos2->second.m_Name
			<< "薪资: " << pos2->second.m_Salary << endl;
	}
	cout << "-------------------------------" << endl;
	cout << "研发部门:" << endl;
	multimap<int, person>::iterator pos3 = m.find(YANFA);
	int count3 = m.count(YANFA);
	int index3 = 0;
	for (; pos3 != m.end() && index3 < count3; pos3++, index3++)
	{
		cout << "姓名: " << pos3->second.m_Name
			<< "薪资: " << pos3->second.m_Salary << endl;
	}
}
int main()
{
	//1.创建员工
	vector<person>v;
	Employee(v);

	//2.员工分组
	multimap<int, person>m;
	setGroup(v, m);

	//3.分组显示员工
	showEmployee(m);

	system("pause");
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值