如何对std :: map进行排序? [英] HowTo sort std::map?

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

问题描述

这是我的地图:

typedef std::map<int/*security id*/, PositionMonth> PortfolioMonth;

其中PositionMonth是结构,例如:

struct PositionMonth
        {
            Nav::Shares shares_;
            Nav::Amount market_value_;

            PositionMonth(void)
                {}
            PositionMonth(const Nav::Amount& market_value
                    , const Nav::Shares& shares)
                : market_value_(market_value)
                , shares_(shares)
                {}
        };

问题:如何按第二个值键参数对std::map进行排序(按market_value_,使其为整数)?示例或链接?

Question: how to sort std::map by second value key param (by market_value_, let it be int)? Examples or links?

PS.增强方法不感兴趣!

PS. Boost methods not interested!

PPS.我无法使用compare functor初始化我的std :: map!

PPS. I'm unable to initialise my std::map with compare functor!

感谢帮助!

我的解决方案(或我自己如何做):

My solution (or how I did it myself):

template<class T>
    struct LessSecondCcy
        : std::binary_function<T,T,bool>
    {
        inline bool operator ()(const T& _left, const T& _right)
        {
            return _left.second.market_value_.currency() < _right.second.market_value_.currency();
        }
    };

和功能:

typedef std::pair<int/*security id*/, _Entry> data_t;

其中_EntryPositionMonth

std::vector<data_t> vec(item.funds_end_.begin(), item.funds_end_.end());
std::sort(vec.begin(), vec.end(), Nav::LessSecondCcy<data_t>());

完成!

推荐答案

There are several options gamedev.net. Look down the thread for the posting by Fruny.

此外:为什么您不将Boost视为可能的解决方案提供商?对于专业的C ++编码人员来说,这是一个受人尊敬的,经过同行评估的,有据可查的解决方案.

Aside: Why would you not consider Boost as a possible solution provider? It's a respected, peer evaluated, well documented solution for professional C++ coders.

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

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