如何对std :: map进行排序? [英] HowTo sort 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;
其中_Entry
是PositionMonth
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屋!