HowTo sort std::map?
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 l开发者_如何学Cinks?
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!
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.
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.
精彩评论