map(终极奥义)

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。数学上映射是一个值只能对应 另一个集合的唯一的一个值,不能有两个或以上,但是同一个 集合里面 会有 不同元素 对应另一个集合的 相同元素,这个 是可以允许的。

下面说说 map 基本用法:就用做复习吧

map 容器是默认递增序列的 ,如果想要改为递减的:map  >mp;   递增的也可以加 map>mp; 就是 跟 那个 里面的 英文单词的意思相反。

1.MAP 数据的插入 有三种方法 :其中有两种与另外一种是不一样的,两种是 :如果map里面 已经有了 这个相同的元素,那就不会再插进去了;而第三种 则会 覆盖掉 他的映射。

第一和第二种:

using namespace std;
#include
map>mp;
mp.insert(make_pair(5,5));
mp.insert(make_pair(100,1000));
#include
map>mp;
mp.insert(pair(10,10)); 
mp.insert(pair(20,20));

第三种:其实很像数组 的赋值操作:

#include
map>mp;
mp[1]=5;
mp[2]=100;
mp[0]=12;

第三种方法:其中中括号里面的 不是下标,而是一个key 值,后面的 值 是该值对应映射。

 2.map 元素的遍历,有正向遍历和逆向遍历 输出:

正向遍历:

for(auto it=mp.begin();it!=mp.end();it++)
	cout<first<<' '<second<

逆向遍历:

 

cout<<"逆向遍历输出"<first<<' '<second<
it1=mp.begin();
	while(it1!=mp.end())
	{
		cout<first<<' '<second<

 

这个是 数组形式输出,但是得要求第一个键值 得是 连续的。

#include
map>mp;
int main()
{
	int i;
	mp[1]=2;
	mp[2]=3;
	mp[3]=4;
	for(i=1;i<=mp.size();i++)
		{
			cout<

也可这样输出,迭代器用过一次后,需要重新 返回 第一个元素 的迭代器。

3.数据的删除,查找(找到时返回该元素迭代器,否则返回尾部的迭代器),和判断元素是否存在count ,如存在返回1,不存在返回0。如下操作:

以下的搜索操作都是对 键 的查找 ,不是对 对应的值 的查找,这点容易弄错。

#include
#include
#include
#include
using namespace std;
#include
map>mp;
int main()
{
	//第一种插入 
	mp[1]=5;
	mp[2]=100;
	mp[0]=12;
	// 一个 迭代器可以重复使用 ,但需要 更新 mp.begin()  
	//正向迭代器 遍历输出 
	cout<<"正向遍历输出"<first<<' '<second<(10,10)); 
	mp.insert(pair(20,20));
	
	//逆向迭代器 遍历输出 
	cout<<"逆向遍历输出"<first<<' '<second<::iterator it1=mp.begin();
	map::iterator it5;
	
	//单个删除
	cout<<"单个删除"<first<<' '<second<first<first<<' '<second<

查找第一个大于或等于 要查的值 用 lower_bound 第一个 大于 该值 用 upper_bound   他们返回的都是 迭代器。在查之前需要先是递增序列 ,才可行的 。也是对 键的查找 ,不是对值 的查找。 

看看代码 实现 :

it1=mp.begin();
	it1=mp.lower_bound(2);
	if(it1->first==2)       //返回的是第一个大于或等于查找的元素的迭代器 
	{						//如果迭代器指的就是这个查找的值,那就找到了 
		cout<<"找到了"<first<<' '<second<

这个 跟那个 it1=find(x) ,还是有点区别的,如果找到 it1,it1迭代器指的就不是 尾部元素的 迭代器。如果指的是最后一个元素的迭代器 ,那就是没有找到。

代码实现:

it1=mp.find(1000);
	if(it1!=mp.end())   //这个是跟 末尾迭代器 比的                      
	{					 
		cout<<"找到了"<first<<' '<second<

 

重要的 知识点

1.map 既然是映射,就可以根据左键值,找到右值,可以说是一种搜索吧(但是有的题目数据量大,用map搜会超时,这时就可以用数组来替代 map ) ,在一些 数据搜索时 ,就会用到这个 方法(信息与信息之间的匹配)。

当然这里的 map<  a ,b  > a和b的数据类型  可以是 int ,int  ;            string,int ;           string,string;         int ,string;等等 。

2.map 其实就是一个一个 的 键值对,键 不能重复 ,而值 可以重复,就像函数 一个自变量,不能 有两个或多个 y值于x对应,比如 1对应 2,2 对应 2,但是 1就不能对应 其他的 值了。

3.用map 来匹配信息 ,用 第三种方法来插入信息,mp[xx]=yy;  这样信息xx 与 信息 yy 之间 建立了某种关系 ,就可以 直接 mp[xx]  来找到这个yy ,但是 不能反过来寻找 。    

下面就是一些map 的排序相关的知识:

https://blog.csdn.net/chengqiuming/article/details/89816566  这个是一些大神的博客,写的很清楚

key值为结构体的时候,我们可以使用以下的方法    
    struct node
	{
		string name;
		int score;
		node(string name1,int now)
		{
			name=name1,score=now;
		}
		bool operator<(const node& a)const
		{
			if(score!=a.score)
				return score>a.score;
			return name p;
	maps;


如果是只对key的某一个元素进行排序的话,我们直接写一个cmp 出来就可以了。
	maps;
	struct cmp
	{
		//是对key 进行排序,如果对value的话,会出错的 
		bool operator()(const string& a,const string& b)
		{
			return a>b;   //也是不等式号指向哪,哪就大  
		}
	};

下面我们对value/key进行 排序,我们借助其他容器来实现操作:(根据自定义函数,我们可以写出排序规则)

#include
#include

using namespace std;
typedef pair p;

bool cmp(p a,p b)
{
	if(a.second!=b.second)
		return a.second>b.second;
	else
		return a.firstmp;
	mp[1]=3;
	mp[5]=1;
	mp[999]=111;
	mp[12]=111;
	
	vector

s(mp.begin(),mp.end()); sort(s.begin(),s.end(),cmp); for(int i=0;i

 当出现两个map 一起用的时候(有时是双对应的才可以解决问题),所以我们采用map 套用map,方法有些不一样:

#include
#include
#include
#include

using namespace std;
typedef pair p;

int main()
{
	map>s;
	s[1][2] = 3;
	s[2][2] = 15;
	for (auto it1 = s.begin(); it1 != s.end(); it1++)
	{
		cout << it1->first << ' ';
		maps2 = it1->second;
		for (auto it2 = s2.begin(); it2 != s2.end(); it2++)
			cout << it2->first << ' ' << it2->second << endl;

	}
	return 0;
}

这样可以 装下3个元素的对应关系,我们就可以解决问题了。
 

你可能感兴趣的:(STL)