How to sort std :: map?

Here is my map:

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

where PositionMonthis the structure, for example:

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)
                {}
        };

Question : how to sort std::mapby the second parameter parameter parameter (through market_value_, let it be int)? Examples or links?

PS. Promotion methods are not interesting!

SFC. I cannot initialize my std :: map using functor comparison!

Thanks for the help!


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();
        }
    };

and in function:

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

where _EntryisPositionMonth

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

Done!

+3
source share
2 answers

There are several options for gamedev.net . Look down the thread to publish Fruni.

: Boost ? , , ++.

+2

, cplusplus.com artile std:: map , !

EDIT: fifth, , , .

+1

Source: https://habr.com/ru/post/1781474/


All Articles