C++STL之map容器

STL新手点击:STL新手入门向

map

map映射容器的元素数据是由一个键值和一个映射数据组成的,键值与映照数据之间具有一一映照的关系。

map容器的数据结构也采用红黑树来实现的,插入元素的键值不允许重复,比较函数只对元素的键值进行

比较,元素的各项数据可通过键值检索出来。由于map与set采用的都是红黑树的结构,所以,用法基本相似。

                                          

map用法

头文件

#include<map>

基本操作

begin() 返回指向 map 头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果 map 为空则返回 true
end() 返回指向 map 末尾的迭代器
erase() 删除一个元素
find() 查找一个元素
insert() 插入元素
key_comp() 返回比较元素 key 的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向 map 尾部的逆向迭代器
rend() 返回一个指向 map 头部的逆向迭代器
size() 返回 map 中元素的个数
swap() 交换两个 map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素 value 的函数

创建map对象

#include<iostream>
#include<map>
using namespace std;
map<int,char>mp;//定义map容器 

 插入

	//数组方式插入 
	mp[1]='a';
	mp[1]='b';//key不允许重复,再次插入相当于修改value的值 
	mp[2]='a'; 
	mp[3]='b';
	mp[4]='c';

    //insert()方式插入
	mp.insert(map<int,char>::value_type(5,'d')); 

删除

//通过关键字key删除元素 
	mp.erase('b');

容器的size

//输出容器大小 
int s=mp.size();

元素的遍历,清空

//遍历输出map中的元素 
	map<int,char>::iterator it=mp.begin();
	while(it!=mp.end())
	{
		cout<<"key:"<<it->first<<" "; 
		cout<<"value:"<<it->second<<endl;;
		it++;
	}

mp.clear();//清空容器 

元素的查找

it=mp.find(1);//搜索键值为1的元素 
	/*若该键值存在,则返回该键值所在的
	  迭代器位置,不存在则返回end()迭代器位置 
	*/
	if(it!=mp.end())
	{
	    cout<<"存在键值为1的元素"<<endl;
	}

实例

#include<iostream>
#include<map>
using namespace std;
map<int,char>mp;//定义map容器 
int main()
{
	//数组方式插入 
	mp[1]='a';
	mp[1]='b';//key不允许重复,再次插入相当于修改value的值 
	mp[2]='a'; 
	mp[3]='b';
	mp[4]='c';
	cout<<"根据key值输出对应的value值"<<mp[1]<<endl;
	cout<<"无法根据value输出key值"<<mp['b']<<endl;
	
	//通过关键字key删除元素 
	mp.erase('b');
	
	//insert()方式插入
	mp.insert(map<int,char>::value_type(5,'d')); 
	
	//输出容器大小 
	int s=mp.size();
	cout<<s<<endl;
	
	//遍历输出map中的元素 
	map<int,char>::iterator it=mp.begin();
	while(it!=mp.end())
	{
		cout<<"key:"<<it->first<<" "; 
		cout<<"value:"<<it->second<<endl;;
		it++;
	}
	it=mp.find(1);//搜索键值为1的元素 
	/*若该键值存在,则返回该键值所在的
	  迭代器位置,不存在则返回end()迭代器位置 
	*/
	if(it!=mp.end())
	{
	    cout<<"存在键值为1的元素"<<endl;
	}
	//cout<<it->first<<it->second<<endl;	
	mp.clear();//清空容器 
	cout<<mp.size()<<endl;
}

map排序:

map本身默认按照key值升序排序 

//默认按照key值升序排序 
#include<iostream>
#include<map>
using namespace std;
map<int,char>mp;
int main()
{
	map<int,char>::iterator it;
    mp[1]='c';
    mp[3]='b';
    mp[2]='d';
    mp[4]='a'; 
    for(it=mp.begin();it!=mp.end();it++)
    {
    	cout<<it->first<<" "<<it->second<<endl;
	}
	cout<<"可以看到map容器默认按照key值升序排列"<<endl; 
}

重载()自定义排序

//重载()自定义排序 
#include<iostream>
#include<map>
using namespace std;
struct cmp{
	bool operator()(const int x,const int y)
	{
		return x>y;//按照key值降序排列 
	}
};
map<int,char,cmp>mp;
int main()
{
	map<int,char>::iterator it;
    mp[1]='c';
    mp[3]='b';
    mp[2]='d';
    mp[4]='a'; 
    for(it=mp.begin();it!=mp.end();it++)
    {
    	cout<<it->first<<" "<<it->second<<endl;
	}
}

按value自定义排序 

//按value自定义排序 
#include <iostream>  
#include <map>  
#include <vector>  
#include <algorithm>  
#include <utility>//pair的头文件 C++为什么pair不使用utility
//也可以运行可能是其他头文件中包含了utility头文件。比如map头文件 
using namespace std;
typedef pair<string, int> PAIR;  
  
bool cmp_by_value(const PAIR &x, const PAIR &y) {  
   return x.second>y.second;//按value降序排列 
   //return x.second<y.second;//按value升序排列 
}  
int main() {  
  map<string, int>mp;  
  mp["a"] = 7;  
  mp["c"] = 9;  
  mp["e"] = 2;  
  mp.insert(make_pair("d",10));  
  mp.insert(make_pair("b",8));  
 //把map中元素转存到vector中   
  vector<PAIR> v(mp.begin(),mp.end());   
  sort(v.begin(), v.end(), cmp_by_value);  
  for (vector<pair<string,int> >::iterator it=v.begin();it!=v.end();++it)  
   {  
    cout<<it->first<<'\t'<<it->second<<endl;  
   }
  return 0;  
}
/*可以简单的理解为如下:map可以当做一个容器(装载具有一定格式的数据);
pair可以理解为元素(放入到容器的的一个个个体),
发现pair并没有单独行动的典型用法,
正常都是配合map来使用(即把pair这个元素插入到map这个容器里面)

STL的<utility>头文件中描述了一个非常简单的模板类pair,
用来表示一个二元组或元素对,并提供了大小比较的比较运算符模板函数。
pair模板类需要两个参数:首元素的数据类型和尾元素的数据类型。
pair模板类对象有两个成员:first和second,分别表示首元素和尾元素。*/

当元素为结构体时自定义排序 

//当元素为结构体时自定义排序 
#include <iostream>  
#include <map>     
#include <utility> 
using namespace std; 
struct Info 
{ 
 string name; 
 float score; 
 //重载"<"操作符,自定义排序规则 
 bool operator < (const Info &a) const 
 { 
 if(score==a.score)
 return name>a.name;
 return score>a.score; 
 } 
}; 
int main(int argc, char* argv[]) 
{ 
 
 map<Info,int> m; 
 Info info; 
 info.name="Jack"; 
 info.score=60; 
 m[info]=25; 
 info.name="Tom";
 info.score=60;
 m[info]=30;
 info.name="Bomi"; 
 info.score=80; 
 m[info]=10; 
 info.name="Peti"; 
 info.score=66.5; 
 m[info]=30;  
 map<Info,int>::iterator it; 
 for(it=m.begin();it!=m.end();it++) 
 { 
 cout<<(*it).second<<" : "; 
 cout<<((*it).first).name<<" "<<((*it).first).score<<endl; 
 } 
 return 0; 
} 

猜你喜欢

转载自blog.csdn.net/love20165104027/article/details/81515466