multimap案例

题目描述

  • 公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作
  • 人员信息有:姓名年龄电话工资等组成
  • 通过multimap进行信息的插入保存显示
  • 分部门显示员工信息显示全部员工信息

代码

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <map>
using namespace std;

enum {RENLI,MEISHU,YANFA};

class Person 
{
public:
	Person(string name, int money):m_Name(name),m_money(money) {}
public:
	string m_Name;
	int m_money;
};

void createWorker(vector<Person> &v)
{
	string nameSeed = "ABCDE";
	for (int i = 0;i<5;i++)
	{
		string name = "员工";
		name += nameSeed[i];

		int money = rand() % 10001 + 10000; 

		Person p(name,money);
		
		v.push_back(p);
	}
}

void setGroup(vector<Person>&v,multimap<int,Person>&m)
{
	for (vector<Person>::iterator it = v.begin();it != v.end();it++)
	{
		//随机产生部门编号
		int departId = rand() % 3;
		//将员工分到mutimap容器中
		m.insert(make_pair(departId,*it));
	}
}

void showGroup(multimap<int,Person>&m)
{
	cout << "RENLI:" << endl;
	multimap<int, Person>::iterator pos = m.find(RENLI);
	int index = 0;
	int num = m.count(RENLI);
	for (;pos != m.end(),index<num;pos++,index++) 
	{
		cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
	}

	cout << "MEISHU:" << endl;
	pos = m.find(MEISHU);
	index = 0;
	num = m.count(MEISHU);
	for (; pos != m.end(), index < num; pos++, index++)
	{
		cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
	}

	cout << "YANFA:" << endl;
	pos = m.find(YANFA);
	index = 0;
	num = m.count(YANFA);
	for (; pos != m.end(), index < num; pos++, index++)
	{
		cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_money << endl;
	}
}

int main()
{
	//创建随机数种子
	srand((unsigned int)time(NULL));

	//创建5个员工
	vector<Person>v;
	createWorker(v);

	//员工创建测试
	/*for (vector<Person>::iterator it = v.begin(); it != v.end();it++)
	{
		cout << "name:" << it->m_Name << " " << "money:" << it->m_money << endl;
	}*/

	//设置分组,分组的multimap容器
	multimap<int, Person>m;
	setGroup(v,m);

	//分部门显示员工
	showGroup(m);

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

coder_Alger

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

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

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

打赏作者

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

抵扣说明:

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

余额充值