这是我的地图:
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)
{}
};
问题: 如何按照第二个值键参数(即 market_value_
,假设为整数)对 std::map
进行排序?有示例或链接吗?
附注:不感兴趣的 Boost 方法!
附注2:我无法使用比较函数初始化我的 std::map!
谢谢帮助!
我的解决方案(或者说我是如何自己解决的):
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>());
完成!
std::map
插入到std::vector
中,并完全排序那只母狗!- WoW :D - mosg