STL - miltimap(可重映射)

#include <iostream>
#include <map>
#include <string>

using namespace std;

//Multimap 案例:
//1个key值可以对应多个valude  =分组 
//公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) 
//人员信息有:姓名,年龄,电话、工资等组成
//通过 multimap进行 信息的插入、保存、显示
//分部门显示员工信息 

class Person
{
public:
	string	name;
	int		age;
	string	tel;
	double	saly;
};

void multimapInit()
{
	Person p1, p2, p3, p4, p5;

	p1.name = "王1";
	p1.age = 31;

	p2.name = "王2";
	p2.age = 32;

	p3.name = "张3";
	p3.age = 33;

	p4.name = "张4";
	p4.age = 34;

	p5.name = "赵5";
	p5.age = 35;

	multimap<string, Person> map2;
	//sale部门
	map2.insert(make_pair("sale", p1));
	map2.insert(make_pair("sale", p2));

	//development 部门
	map2.insert(make_pair("development", p3));
	map2.insert(make_pair("development", p4));

	//Financial 部门
	map2.insert(make_pair("Financial", p5));


	for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++)
	{
		cout << it->first << "\t" << it->second.name << endl;
	}
	cout << "遍历结束" << endl;

	//
	int num2 = map2.count("development");
	cout << "development部门人数==>" << num2 << endl;

	cout << "development部门员工信息" << endl;
	multimap<string, Person>::iterator it2 = map2.find("development");

	int tag = 0;
	while (it2 != map2.end() && tag < num2)
	{
		cout << it2->first << "\t" << it2->second.name << endl;
		it2++;
		tag++;
	}

}


//age = 32修改成 name32
void multimapChange()
{
	Person p1, p2, p3, p4, p5;

	p1.name = "王1";
	p1.age = 31;

	p2.name = "王2";
	p2.age = 32;

	p3.name = "张3";
	p3.age = 33;

	p4.name = "张4";
	p4.age = 34;

	p5.name = "赵5";
	p5.age = 35;

	multimap<string, Person> map2;
	//sale部门
	map2.insert(make_pair("sale", p1));
	map2.insert(make_pair("sale", p2));

	//development 部门
	map2.insert(make_pair("development", p3));
	map2.insert(make_pair("development", p4));

	//Financial 部门
	map2.insert(make_pair("Financial", p5));


	cout << "\n按照条件 检索数据 进行修改 " << endl;
	for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++)
	{
		//cout << it->first << "\t" << it->second.name << endl;
		if (it->second.age == 32)
		{
			it->second.name = "name32";
		}
	}


	for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++)
	{
		cout << it->first << "\t" << it->second.name << endl;
	}


}

int main()
{
	multimapInit();
	multimapChange();
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值