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

这里写目录标题

      • STL(二) map容器插入、删除小技巧
        • 一、map插入
          • 1、用数组方式插入数据
          • 2、在insert函数中使用make_pair()函数
          • 3、插入前先查找该key值,是否已存在
          • 4、insert_or_assign直接插入,如果有该key,则替换velue;如果没有该key值,直接插入
          • 5、try_emplace,如果有该key,则velue值保持不变;如果没有该key值,直接插入
        • 二、map删除

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

一、map插入
1、用数组方式插入数据
#include 
#include 
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 
#include 
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 
#include 
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 
#include 
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 
#include 
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 
#include 
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]

你可能感兴趣的:(STL,c++,开发语言,算法)