C++STL中的map按Key值排序和按Value值排序

C++中的map内部实现是红黑树,是有序的,默认按less<Key>排序,对<运算符进行了重载

 map的定义:

template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map;

它有四个参数,其中我们比较熟悉的有两个: Key 和 Value。第四个是 Allocator,用来定义存储分配模型的,此处不作介绍。

重点看第三个参数: class Compare = less<Key> 

这也是一个class类型的,而且提供了默认值 less<Key>。 less是stl里面的一个函数对象,那么什么是函数对象呢?

所谓的函数对象:即调用操作符的类,其对象常称为函数对象(function object),它们是行为类似函数的对象。表现出一个函数的特征,就是通过“对象名+(参数列表)”的方式使用一个 类,其实质是对operator()操作符的重载。

现在我们来看一下less的实现:

template <class T> struct less : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x<y;}
};


它是一个带模板的struct,里面仅仅对()运算符进行了重载,实现很简单,但用起来很方便,这就是函数对象的优点所在。stl中还为四则运算等常见运算定义了这样的函数对象,与less相对的还有greater:

template <class T> struct greater : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x>y;}
};

对Key值进行排序:

map指定less作为其默认比较函数(对象),所以通常如果不自己指定Compare,map中键值对就会按照Key的less顺序进行组织存储,

#include<iostream>
#include<string>
#include<map>
using namespace std;


int main()
{
	map<string ,int> mp;
	mp["Liming"]=90;
	mp["Xiaohong"]=85;
	mp["Wangge"]=95;
	mp.insert(make_pair("Bob",60));
	map<string ,int>::iterator iter;
	for(iter=mp.begin();iter!=mp.end();iter++)
		cout<<iter->first<<"\t"<<iter->second<<endl;
	return 0;
}

输出:

 在实例化map时,可以指定第三个参数Compare,自定义排序规则

#include<iostream>
#include<string>
#include<map>
using namespace std;

typedef pair<string,int> PAIR;
struct CmpByKeylength
{
	bool operator()(const string&str1,const string& str2)
	{
		//按string的长度进行排序,长度相同时,比较字符串的大小
		return str1.length()<str2.length() || str1<str2;
	}

};

int main()
{
       //map的第三个参数是类对象
	map<string,int,CmpByKeylength> mp;
	mp["Liming"]=90;
	mp["Xiaohong"]=85;
	mp["Wangge"]=95;
	mp.insert(make_pair("Bob",60));
	map<string ,int>::iterator iter;
	for(iter=mp.begin();iter!=mp.end();iter++)
		cout<<iter->first<<"\t"<<iter->second<<endl;
	return 0;
}

输出:

对Value值进行排序:

对key值进行排序,借助的是map的参数接口,为其指定相应的Compare类。对Value进行排序,可以借助vector的sort函数,sort算法有个限制,利用sort算法只能对序列容器进行排序,就是线性的(如vector,list,deque),map是一个集合容器,它里面存储的元素是pair,它不是线性存储的(前面提过,像红黑树),所以利用sort不能直接和map结合进行排序。但是,我们可以把map中的元素放到序列容器(如vector)中,然后再对这些元素进行排序。C++pair用法

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

typedef pair<string,int> PAIR;
//重载流插入运算符
ostream& operator<<(ostream& out,const PAIR& p)
{
	return out<<p.first<<"\t"<<p.second<<endl;
}

bool cmp_by_value(const PAIR& p1,const PAIR& p2)
{
	return p1.second<p2.second;
}


int main()
{
	map<string,int> mp;
	mp["Liming"]=90;
	mp["Xiaohong"]=85;
	mp["Wangge"]=95;
	mp.insert(make_pair("Bob",60));

	vector<PAIR> vec(mp.begin(),mp.end());
      //sort的第三个参数是二元谓词,不是类对象
	sort(vec.begin(),vec.end(),cmp_by_value);
	vector<PAIR>::iterator iter;
	for(iter=vec.begin();iter!=vec.end();iter++)
		cout<<(*iter);    
       //不重载<<,输出用cout<<iter->first<<"\t"<<iter->second<<endl;
	return 0;
}

输出:

注意:map第三个参数和sort第三个参数的差别,map第三个参数是类对象,而sort第三个参数是二元谓词

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值