C++ map自定义排序

STL中使用映射(map)时,常常希望能够根据自己的需要进行排序,排序的方法进行如下整理:

一、对key值进行特定的排序

map容器里面有两个值一个key一个是valuemap<key,value>,其实map里面还有第三个参数,是一个类,用来对mapkey进行排序的类,定义如下

template<class _Kty,
    class _Ty,
    class _Pr = less<_Kty>,
    class _Alloc = allocator<pair<const _Kty, _Ty> > >
    class map

less<_Kty>的代码

struct less
        : public binary_function<_Ty, _Ty, bool>
    {    // functor for operator<
    bool operator()(const _Ty& _Left, const _Ty& _Right) const
        {    // apply operator< to operands
        return (_Left < _Right);
        }
    };

那么根据上面的代码我们也可以写出一个greater类来让key按照降序排列

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

class MyCompare {
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};

void test01()
{
	//默认从小到大排序
	//利用仿函数实现从大到小排序
	map<int, int, MyCompare> m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 30));
	m.insert(make_pair(3, 50));
	m.insert(make_pair(4, 20));
	m.insert(make_pair(5, 40));

	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key:" << it->first << " value:" << it->second << endl;
	}
}
int main() {

	test01();

	system("pause");

	return 0;
}

在这里插入图片描述
以上就对key值进行了你想要的排序方式。

二、对value的值进行排序

因为map的模板里面没有对value的值进行排序的参数,所以只能借助sort函数,然而sort函数只能对vector,list,queue等排序,无法对map排序,那么就需要把map的值放入vector中在对vector进行排序,在对vector进行输出,从而间接实现了对map的排序。sort也有第三个参数,跟上面那个map类似,所以可以写一个类或者函数来将其排序。

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

typedef pair<string, int> PAIR;

bool cmp_val(const PAIR &left,const PAIR &right)
{
    return left.second < right.second;
}

int main()
{
    map<string, int> ma;
    ma["Alice"] = 86;
    ma["Bob"] = 78;
    ma["Zip"] = 92;
    ma["Stdevn"] = 88;
    vector<PAIR> vec(ma.begin(),ma.end());
    sort(vec.begin(),vec.end(),cmp_val);
    for (vector<PAIR>::iterator ite = vec.begin(); ite != vec.end(); ++ite)
    {
        cout << ite->first << " " << ite->second << endl;
    }
    getchar();
}

这样就通过cmp_val函数对vector进行了排序,然后在将其输出即可。

如果感觉写函数过于繁琐也可以直接在sort里面用lambda表达式,代码如下

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

typedef pair<string, int> PAIR;

int main()
{
    map<string, int> ma;
    ma["Alice"] = 86;
    ma["Bob"] = 78;
    ma["Zip"] = 92;
    ma["Stdevn"] = 88;
    vector<PAIR> vec(ma.begin(),ma.end());
    sort(vec.begin(), vec.end(),[](const PAIR &left, const PAIR &right)
    {
        return left.second < right.second;
    });
    for (vector<PAIR>::iterator ite = vec.begin(); ite != vec.end(); ++ite)
    {
        cout << ite->first << " " << ite->second << endl;
    }
    getchar();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting_1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值