C++

STL - map常用方法

map简述

map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,其作用类似于python之中的字典,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

map的基本操作函数:

优点

1.自动建立Key - value的对应。key 和 value可以是任意你需要的类型
2.根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次
3.快速插入Key -Value 记录
4.快速删除记录
5.根据Key 修改value记录

1.创建map容器

首先需要引入map的头文件
#include <map>
使用模板 map<键,值> name(给map容器的命名)
下面列出常用的几种
map<int,int> , map<string,int> , map<int,string>
map<char,int> , map<int,char> .

插入数据

  • map提供了三种插入方法
    1.用insert函数插入pair数据
    2.用insert函数插入value_type数据
    3.用数组方式插入数据
//使用pair插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
	mapnum.insert(pair<int, string>(1, "one"));
	mapnum.insert(pair<int, string>(2, "two"));
	mapnum.insert(pair<int, string>(3, "three"));
	//声名迭代器
	map<int, string>::iterator iter;
	for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
		cout << iter->first << " " << iter->second << endl;
	system("pause");
	return 0;
}
//使用value_type插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
	mapnum.insert(map<int, string>::value_type(1, "one"));
	mapnum.insert(map<int, string>::value_type(2, "two"));
	mapnum.insert(map<int, string>::value_type(3, "three"));
	//声名迭代器
	map<int, string>::iterator iter;
	for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
		cout << iter->first << " " << iter->second << endl;
	system("pause");
	return 0;
}
//使用数组的方式插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
	mapnum[1] = "one";
	mapnum[2] = "two";
	mapnum[3] = "three";
	//声名迭代器
	map<int, string>::iterator iter;
	for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
		cout << iter->first << " " << iter->second << endl;
	system("pause");
	return 0;
}

遍历数据

  • 向前迭代器和反向迭代器
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
	mapnum.insert(pair<int, string>(1, "one"));
	mapnum.insert(pair<int, string>(2, "two"));
	mapnum.insert(pair<int, string>(3, "three"));
	//声名迭代器
	map<int, string>::iterator iter;
	
	//反向迭代
	map<int, string>::reverse_iterator iter1;
	//第一种向前迭代器遍历
	for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
		cout << iter->first << " " << iter->second << endl;
	
	
	//第二种应用反向迭代器
	for(iter1 = mapnum.rbegin();iter1 != mapnum.rend();iter1++)
		cout << iter1->first << " " << iter1->second << endl;

	int nsize = mapnum.size();
	for (int i = 1; i <= nsize; i++)
	{
		cout << mapnum[i] << endl;
	}

		system("pause");
	return 0;
}

**其他常用方法

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

using namespace std;
map<int, string> m;

int main()
{
    //使用数组方法插入数据
	m[1] = "w";
	m[2] = "x";
	m[3] = "can";
	m[4] = "do";
	m[5] = "it";
	m[4] = "haha";
	//得到map的大小 
	int nsize = m.size();

	//count()统计某指定元素出现的次数
	cout << m.count(1) << endl;

	//find()查找某一个元素
	map<int, string>::iterator it;
	it = m.find(1);
	if (it == m.end())cout << "没有找到!" << endl;
	else cout << "找到该元素!" << endl;
	
	
	//erase 删除一个元素
	it = m.find(1);
	if (it == m.end())cout << "没有找到!" << endl;
	else m.erase(1);

	system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/wlw-x/p/11551275.html