map使用手册

map的使用手册

  • 我找不到之前参考的博文链接了,只好写原创,对于原创作者感到抱歉

map的介绍

  • map是键值对<key,value>数据集合,key必须唯一
    • 主要用来查找key对应value,要求key必须是可排序的,必须支持<比较运算符
    • map默认是以key升序存放键值对<key,value>数据,比较适合二分查找

map的内部结构

  • map使用pair<key,value>类模板保存key与value
  • pair<key,value>有两个public成员变量:first和second
    • first存放key
    • second存放value
  • 在map里面可以使用map<>::value_type表示pair<key,value>

map的操作

map插入数据

  • 用insert函数插入数据,在数据的插入上涉及到集合的唯一性
    • 即当map中存在该关键字时,insert操作无法插入数据
  • 如果用数组方式,它可以覆盖以前该关键字对应的值
    map<int,string> m;
    //insert形式
    m.insert({ 1,"ABC"});//这样就行
    m.insert(2,make_pair(string("ABC")));
	m.insert(map<int,string>::value_type(3,"ABC"));
	//数组形式
	m[1]="efg";

map初始化

一般map的初始化

//列表初始化
map<int,string> m={{1,"duanjie"},{2,"yanwanli"}};
//数组赋值
m[3]="adc";
//insert添加
m.insert({ 4,"ABC"});

多层嵌套map的初始化

map<int, map<string, vector<string>>>  m;
m[1]["abc"].push_back("ABC");

map遍历

第一种:应用前向迭代器

    map<int,string> m={{1,"abc"},{2,"def"}};
//第1种
    map<int,string>::iterator iter;
    iter = m.begin();
    while(iter != m.end()){
        cout << iter->first << "-" << iter->second << endl;
        iter++;
    }
//第2种
    for(auto &it : m){
        cout << it.first << "-" << it.second <<endl;
    }

第二种:应用反相迭代器

  • 实现反向遍历
    map<int,char> m;
    for(map<int,char>::reverse_iterator rit=m.rbegin();rit!=m.rend();rit++) 
        cout<<(*rit).first<<","<<(*rit).second<<endl;    

第三种,用数组的形式

	//数组形式
	map<int,string> m;
	int L=m.size();
	for(int i=1;i<=L;i++){
		cout<<m[i]<<"  ";
	}

map的数据查找

第一种:count函数判断

  • 其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
	map<int,string> m={{1,"abc"},{2,"edf"}};
	if(m.count(1)){
		cout<<"YES"<<endl;
	}

第二种:find函数

  • 定位数据出现位置
  • 它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
	map<int,string> m={{1,"abc"},{2,"edf"}};
    map<int, string>::iterator iter;
    iter =m.find(1);
    if(iter !=m.end())
    {
    	cout<<"Find it :"<<iter->second<<endl;
    }
    else{
    	cout<<"Not Found!"<<endl;
    }

第三种:lower_bound函数与upper_bound函数

  • lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
  • upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)

例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper-bound(2)的话,返回的就是3,如果两个迭代器相等就意味着不存在这个关键字

	map<int,string> m={{1,"abc"},{4,"edf"}};
    map<int, string>::iterator iter1,iter2;
    iter1 =m.lower_bound(2);
    iter2=m.upper_bound(2);
    if(iter1->first==iter2->first)
    {
    	cout<<"Not Found!"<<endl;
    }
    else{
    	cout<<"Find it :"<<iter1->second<<endl;
    }

map的数据删除

erase()函数

  • 移除某个map中某个条目用erase()
    iterator erase(iterator it);//通过一个条目对象删除
    iterator erase(iterator first,iterator last)//删除一个范围
    size_type erase(const Key&key);//通过关键字删除

//例如
	map<int,string> m={{1,"abc"},{4,"edf"}};
    map<int, string>::iterator iter1,iter2;
    m.erase(1);
    if(m.count(1)){
    	cout<<"Yes"<<endl;
    }
    else{
    	cout<<"No"<<endl;
    }

clear()函数

  • clear()函数相当于
enumMap.erase(enumMap.begin(),enumMap.end());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章