对于自定义KEY需要重载==判断是否相等,用()获取hash值
struct stream_order_key
{
std::string instrument;
ioption::TChnOrderLocalIDType orderLocalID;
stream_order_key() : orderLocalID(0) {}
bool operator==(const stream_order_key& s) const
{
return instrument==s.instrument && orderLocalID==s.orderLocalID;
}
size_t operator()(const stream_order_key& s) const
{
size_t seed = 0;
boost::hash_combine(seed, boost::hash_value(s.instrument));
boost::hash_combine(seed, boost::hash_value(s.orderLocalID));
return seed;
}
};
typedef std::unordered_map<<span style="font-family: Arial, Helvetica, sans-serif;">stream_order_key</span><span style="font-family: Arial, Helvetica, sans-serif;">, int, stream_order_key> stream_order_map;</span>
添加一个hash算法:
inline size_t operator()(const T & ov) const
{
/// BKD HASH
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
const char* str = ov.c_str();
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
对于insert无法覆盖问题解决办法:
使用
stream_order_map order_map;
order_map[s] = a;
order_map[s] = b;
可以解决