3

Here is my map:

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

where PositionMonth is a structure, ex.:

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::map by second value key param (by market_value_, let it be int)? Examples or links?

PS. Boost methods not interested!

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

Thanks for 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 _Entry is PositionMonth

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

Done!

0

2 Answers 2

2

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

STL must do the job, I guess. PS. Besides additional includes - no way. Thanks!
Thanks, Tony, this link solved my question! By the way, I had googling and live tab with exactlly that link opened, while started to post my question on SO... I inserted std::map to the std::vector and totally sort that bitch! - WoW :D
1

Maybe the example code in the cplusplus.com artile on std::map constructor give the clarification you are looking for!

EDIT: The fifth map instantiated in the example code in the link above shows you how to change the comparator object.

1 Comment

@mosg: From the example code, 'fourth' and 'fifth' are constructed using the explicit default constructor. 'fifth' specifies the comparator object whereas assumes the default for the allocator. So both fourth and fifth doesn't have any objects filled in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.