map:

map的功能:

自动建立Key - value的对应。key 和 value可以是任意你需要的类型。

根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。

快速插入Key -Value 记录。

快速删除记录

根据Key 修改value记录。

遍历所有记录。

#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#define p_Map map <int,string>:: iterator
#define v_Map map<int,string>:: value_type
using namespace std;
const int INF = 120000+5;

int main()
{
	
	map <int,string> data;
	//三种插入数据的方法 
	//第一种 
	data.insert(pair<int,string>(1,"adaaed"));
	//第二种 
	map <int,string>:: value_type a1 (2,"dasda");
	data.insert(a1);
	/*第一,第二种插入方式,都不能覆盖前面的值;
	也就是说如果插入多次关键值相等的数据,以第一次为准*/ 
	
	//第三种插入方式(可以覆盖之前的值) 
	data[1] = "fsfs" ;		//注意[]里面可以是任意类型的值,如果key是字符串类型值
							//也可以写成data["fsfs"] = 1; 
	
	//查看map的大小,即里面有多少的元素
	int len = data.size(); 
	
	//遍历map,有两种方式
	//第一种,正向遍历 
	map <int, string>:: iterator iter1;
	for(iter1 = data.begin(); iter1 != data.end(); iter1++)
	{
		cout<<iter1->first << " "<<iter1->second<<endl;
	}
	//第二种,反向遍历,注意要用方向迭代器 
	map <int,string>:: reverse_iterator iter2;
	for(iter2 = data.rbegin(); iter2 != data.rend(); iter2++)
	{
		cout<<iter2->first<<" "<<iter2->second<<endl;
	} 
	
	
	//通过关键字来查找数据,有两种方法
	/*
	第一种:用count函数来判定关键字是否出现,
	其缺点是无法定位数据出现位置,由于map的特性,
	一对一的映射关系,就决定了count函数的返回值只有两个,要
	么是0,要么是1,出现的情况,当然是返回1了
	*/ 
	int is = data.count(2);
	cout<<is<<endl;
	/*
	第二种:用find函数来定位数据出现位置,它返回的一个迭代器,
	当数据出现时,它返回数据所在位置的迭代器,
	如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
	*/ 
	map <int,string>:: iterator find1 = data.find(2) ;
	cout<<find1->first<<" "<<find1->second<<endl;

	
	
	
	
	
	return 0;
}