STL(二) map容器插入、删除小技巧

STL(二) map容器插入、删除小技巧

一、map插入
1、用数组方式插入数据
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	// 1 用数组方式插入数据
	cout << "1、mapIntStringTemp[1] = '1'" << endl;
	mapIntStringTemp[1] = "1";
	mapIntStringTemp[2] = "2";
	mapIntStringTemp[3] = "3";
	mapIntStringTemp[4] = "4";
	mapIntStringTemp[5] = "5";
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

1、mapIntStringTemp[1] = '1'
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
2、在insert函数中使用make_pair()函数
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	// 2 在insert函数中使用make_pair()函数
	cout << "2、mapIntStringTemp.insert(std::make_pair(1, '1'))" << endl;
	mapIntStringTemp.insert(std::make_pair(1, "1"));
	mapIntStringTemp.insert(std::make_pair(2, "2"));
	mapIntStringTemp.insert(std::make_pair(3, "3"));
	mapIntStringTemp.insert(std::make_pair(4, "4"));
	mapIntStringTemp.insert(std::make_pair(5, "5"));
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

2、mapIntStringTemp.insert(std::make_pair(1, '1'))
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
3、插入前先查找该key值,是否已存在
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	cout << "3、mapIntStringTemp.find(3)" << endl;
	map<int, string>::iterator mapIter = mapIntStringTemp.find(3);
	if (mapIter != mapIntStringTemp.end())
	{
		mapIntStringTemp[3] = "3";
	}
	else
	{
		mapIntStringTemp.emplace(3, "3");
		// mapIntStringTemp.insert(std::make_pair(3, "3"));
	}
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

3、mapIntStringTemp.find(3)
key = [3] value = [3]
4、insert_or_assign直接插入,如果有该key,则替换velue;如果没有该key值,直接插入
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	cout << "4、mapIntStringTemp.insert_or_assign(4, '4')" << endl;
	mapIntStringTemp.insert_or_assign(1, "1");
	mapIntStringTemp.insert_or_assign(2, "2");
	mapIntStringTemp.insert_or_assign(3, "3");
	mapIntStringTemp.insert_or_assign(4, "4");
	mapIntStringTemp.insert_or_assign(5, "5");
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	cout << "mapInfo.insert_or_assign(3, '5');" << endl;
	mapIntStringTemp.insert_or_assign(3, "5");
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

4、mapIntStringTemp.insert_or_assign(4, '4')
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
mapInfo.insert_or_assign(3, '5');
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [5]
key = [4] value = [4]
key = [5] value = [5]
5、try_emplace,如果有该key,则velue值保持不变;如果没有该key值,直接插入
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	// 5 try_emplace,如果有该key,则velue值保持不变;如果没有该key值,直接插入
	cout << "5、mapIntStringTemp.try_emplace(5, '5')" << endl;
	mapIntStringTemp.try_emplace(1, "1");
	mapIntStringTemp.try_emplace(2, "2");
	mapIntStringTemp.try_emplace(3, "3");
	mapIntStringTemp.try_emplace(4, "4");
	mapIntStringTemp.try_emplace(5, "5");
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });

	cout << "mapInfo.try_emplace(3, '5');" << endl;
	mapIntStringTemp.try_emplace(3, "5");
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

5、mapIntStringTemp.try_emplace(5, '5')
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
mapInfo.try_emplace(3, '5');
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
二、map删除
#include <map>
#include <string>
using namespace std;
void test()
{
	map<int, string> mapIntStringTemp;
	cout << "6、mapIntStringTemp.erase(6)" << endl;
	mapIntStringTemp.insert_or_assign(1, "1");
	mapIntStringTemp.insert_or_assign(2, "2");
	mapIntStringTemp.insert_or_assign(3, "3");
	mapIntStringTemp.insert_or_assign(4, "4");
	mapIntStringTemp.insert_or_assign(5, "5");
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });

	cout << "6、mapIntStringTemp.erase(3)" << endl;
	map<int, string>::iterator mapIter = mapIntStringTemp.find(3);
	if (mapIter != mapIntStringTemp.end())
	{
		mapIntStringTemp.erase(3);
	}
	std::for_each(mapIntStringTemp.begin(), mapIntStringTemp.end(), [](map<int, string>::reference mapPrint) {cout << "key = [" << mapPrint.first << "] value = [" << mapPrint.second << "]" << endl; });
	mapIntStringTemp.clear();
	cout << endl;
}

输出

6、mapIntStringTemp.erase(6)
key = [1] value = [1]
key = [2] value = [2]
key = [3] value = [3]
key = [4] value = [4]
key = [5] value = [5]
6、mapIntStringTemp.erase(3)
key = [1] value = [1]
key = [2] value = [2]
key = [4] value = [4]
key = [5] value = [5]
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)提供了一个名为`std::map`的关联容器,它基于红黑树实现,用于存储键值对,并按照键的顺序进行排序。下面是`std::map`容器的一些常用方法: 1. 插入元素: ```cpp std::map<Key, Value> myMap; myMap.insert(std::make_pair(key, value)); // 使用insert方法插入键值对 myMap[key] = value; // 使用下标操作符[]插入键值对,如果键已存在,则会更新值 ``` 2. 删除元素: ```cpp myMap.erase(key); // 根据键删除元素 myMap.clear(); // 清空所有元素 ``` 3. 访问元素: ```cpp Value value = myMap[key]; // 使用下标操作符[]访问指定键对应的值 auto it = myMap.find(key); // 使用find方法查找指定键的迭代器 if (it != myMap.end()) { Value value = it->second; // 通过迭代器访问指定键对应的值 } ``` 4. 遍历容器: ```cpp for (const auto& pair : myMap) { Key key = pair.first; // 键 Value value = pair.second; // 值 // 其他操作 } ``` 5. 判断元素是否存在: ```cpp if (myMap.count(key) > 0) { // 键存在 } ``` 6. 获取容器大小和判断容器是否为空: ```cpp size_t size = myMap.size(); // 获取容器中键值对的个数 bool isEmpty = myMap.empty(); // 判断容器是否为空 ``` 这些是`std::map`容器的一些常用方法,还有其他一些方法和成员函数可以进一步扩展其功能。你可以参考C++标准库的文档以获取更详细的信息。 希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值