Map的操作

map 存放元素和遍历
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;


void map_operate()
{
	map<int, string> map1;

	// 方法1
	map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	map1.insert(make_pair(3, "teacher03"));
	map1.insert(make_pair(4, "teacher04"));

	// 方法3
	map1.insert(map<int, string>::value_type(5, "teacher05"));
	map1.insert(map<int, string>::value_type(6, "teacher06"));

	// 方法4
	map1[7] = "teacher07";
	map1[8] = "teacher08";

	// 容器遍历
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍历结束" << endl;


}

int main()
{
	map_operate();
	system("pause");
	return 0;
}

上述四种插入方法的异同:

前三种方法返回的是pair<iterator, bool>

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;


void map_operate()
{
	map<int, string> map1;

	// 方法1
	pair<map<int, string>::iterator, bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	pair<map<int, string>::iterator, bool> mypair3 = map1.insert(make_pair(3, "teacher03"));
	map1.insert(make_pair(4, "teacher04"));

	// 方法3
	pair<map<int, string>::iterator, bool> mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05"));
	if (mypair5.second != true)
	{
		cout << "key 5 插入失败" << endl;
	}
	else
	{
		cout << mypair5.first->first << " : " << mypair5.first->second<< endl;
	}
	// 插入2个5  会插入失败
	pair<map<int, string>::iterator, bool> mypair6 = map1.insert(map<int, string>::value_type(5, "teacher06"));
	if (mypair6.second != true)
	{
		cout << "key 5 插入失败" << endl;
	}
	else
	{
		cout << mypair6.first->first << " : " << mypair6.first->second << endl;
	}


	// 方法4   相同的 key 会覆盖
	map1[7] = "teacher07";
	map1[7] = "teacher77";

	// 容器遍历
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍历结束" << endl;


}

int main()
{
	map_operate();
	system("pause");
	return 0;
}

前三种方法返回的是pair<iterator, bool> 若 key存在,那么会报错 方法四若key已经存在,则会修改(覆盖)

multimap 的使用案例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;

/*
* 一个key可以对应多个value  =  分组
* 通过multimap进行信息的插入/保存/显示
* 分部门显示员工信息
*/

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


};


void multimapDemo()
{
	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> map1;

	// sale 部门
	map1.insert(make_pair("sale", p1));
	map1.insert(make_pair("sale", p2));
	// development
	map1.insert(make_pair("development", p3));
	map1.insert(make_pair("development", p4));
	// Financial
	map1.insert(make_pair("financial", p5));

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

	// 按部门显示员工信息
	int count = map1.count("development");
	cout << "development 部门人数 : " << count << endl;

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

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


// 修改容器的内容   把 age = 32 的name 修改成为name32
void multimapDemo_02()
{
	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> map1;

	// sale 部门
	map1.insert(make_pair("sale", p1));
	map1.insert(make_pair("sale", p2));
	// development
	map1.insert(make_pair("development", p3));
	map1.insert(make_pair("development", p4));
	// Financial
	map1.insert(make_pair("financial", p5));

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



int main()
{
	// multimapDemo();
	multimapDemo_02();

	system("pause");
	return 0;
}
unordered_map是C++ STL中的一个容器,它提供了一种用于存储键值对的机制,其中键是唯一的,而值可以重复。unordered_map可以通过哈希表来实现,因此可以快速地插入、查找和删除元素。下面是一些unordered_map的常见操作: 1. 插入元素: 使用insert函数或[]操作符可以向unordered_map中插入键值对。例如: unordered_map<int, int> count; count.insert({1, 10}); count = 20; 2. 访问元素: 可以使用[]操作符来访问unordered_map中的元素。例如: int value = count; 3. 删除元素: 使用erase函数可以删除unordered_map中的元素。例如: count.erase(1); 4. 查找元素: 可以使用find函数来查找unordered_map中是否存在指定的键。如果元素存在,find函数会返回指向该元素的迭代器。如果元素不存在,则返回unordered_map的end迭代器。 5. 遍历元素: 可以使用范围循环或迭代器来遍历unordered_map中的键值对。例如: - 使用范围循环: ```cpp for (auto p : count) { int key = p.first; int value = p.second; // 对键值对进行操作 } ``` - 使用迭代器: ```cpp for (auto it = count.begin(); it != count.end(); ++it) { int key = it->first; int value = it->second; // 对键值对进行操作 } ``` 总结:unordered_map提供了一种方便的方式来存储和操作键值对,可以通过插入、访问、删除和查找元素来操作unordered_map。此外,还可以使用范围循环或迭代器来遍历unordered_map中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值