使用值对std :: map进行排序 [英] Sorting std::map using value

查看:103
本文介绍了使用值对std :: map进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要排序std :: map使用其值而不是其键。是有一个简单的方法来做。



我在以下线程中有一个解决方案:

std::map按数据排序?

有更好的解决方案。 >

 
map< long,double> testMap;
//一些代码生成地图中的值。

sort(testMap.begin(),testMap.end()); //有这样的任何功能排序地图吗?


解决方案

即使正确的答案已经发布,添加一个演示,演示如何清除这些错误:

  template< typename A,typename B& 
std :: pair< B,A> flip_pair(const std :: pair< A,B& p)
{
return std :: pair< B,A>(p.second,p.first);
}

template< typename A,typename B>
std :: multimap< B,A> flip_map(const std :: map< A,B& src)
{
std :: multimap< B,A& dst;
std :: transform(src.begin(),src.end(),std :: insertionter(dst,dst.begin()),
flip_pair< A,B&
return dst;
}

int main(void)
{
std :: map< int,double> src;

...

std :: multimap< double,int> dst = flip_map(src);
// dst现在按照以前在src中的值排序!
}






源(需要C ++ 11)



如果您使用 std :: map 对于源关联容器(例如 std :: unordered_map ),你可以编写一个单独的重载,但是最后动作仍然是相同的,所以一个广义使用可变参数模板的关联容器可用于映射结构:

  //翻转关联容器A,B对到B,A对
模板< typename A,typename B,template< class,class,class ...>类M,类... Args>
std :: multimap< B,A> flip_map(const M {
std :: multimap< B,A& dst;
std :: transform(src.begin(),src.end(),
std :: inserter(dst,dst.begin()),
flip_pair< A,B& ;
return dst;
}

这将适用于 std :: map std :: unordered_map 作为翻转的来源。


i need to sort a std::map using its value rather than its key. is there a easy way to do it.

i got one solution in the follwing thread:
std::map sort by data?
is there any better solution.

map<long, double> testMap;
// some code to generate the values in the map.

sort(testMap.begin(), testMap.end());  // is there any function like this to sort the map?

解决方案

Even though correct answers have already been posted, I thought I'd add a demo of how you can do this cleanly:

template<typename A, typename B>
std::pair<B,A> flip_pair(const std::pair<A,B> &p)
{
    return std::pair<B,A>(p.second, p.first);
}

template<typename A, typename B>
std::multimap<B,A> flip_map(const std::map<A,B> &src)
{
    std::multimap<B,A> dst;
    std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), 
                   flip_pair<A,B>);
    return dst;
}

int main(void)
{
    std::map<int, double> src;

    ...    

    std::multimap<double, int> dst = flip_map(src);
    // dst is now sorted by what used to be the value in src!
}


Generic Associative Source (requires C++11)

If you're using an alternate to std::map for the source associative container (such as std::unordered_map), you could code a separate overload, but in the end the action is still the same, so a generalized associative container using variadic templates can be used for either mapping construct:

// flips an associative container of A,B pairs to B,A pairs
template<typename A, typename B, template<class,class,class...> class M, class... Args>
std::multimap<B,A> flip_map(const M<A,B,Args...> &src)
{
    std::multimap<B,A> dst;
    std::transform(src.begin(), src.end(),
                   std::inserter(dst, dst.begin()),
                   flip_pair<A,B>);
    return dst;
}

This will work for both std::map and std::unordered_map as the source of the flip.

这篇关于使用值对std :: map进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆