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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值