返回信息流为了提高map的速度,欲将已有的红黑树map改写为hash_map,但hash_map不是标准的STL,示例程序网上很难查。
现有
struct MyKey
{
int m_id;
void* m_ptr;
bool operator< (const MyKey& mykey) const
{
//return m_id > mykey.m_id; //最大堆,最小优先队列
return m_id < mykey.m_id; //最小堆,最大优先队列
}
bool operator== (const MyKey& mykey) const
{
return m_id == mykey.m_id; //for hash map
}
};
struct Comparison
{
bool operator()(const MyKey mykey1, const MyKey mykey2) const
{
return mykey1.m_id == mykey2.m_id;
}
};
需要以MyKey作为hash_map的key类型,但需要重载hash函数、operator()和operator==,但不清楚这三个函数到底应该怎么重载。需要用此hash_map的平台的环境有gcc3.x/gcc4.x/VS2008三种,最好能写成通用平台的hash_map。
现在map<MyKey, MyValue> mymap;是对的
但hash_map<MyKey, MyValue> mymap;编译不通过
但hash_map<MyKey, MyValue, hash<int>, Comparison> mymap;编译也不通过
急求可行的hash_map语法
在线等示例,多谢各位!
这是一条镜像帖。来源:北邮人论坛 / cpp / #44216同步于 2010/9/24
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
急求可行的hash_map语法,Key是自己定义的struct
josephbupt
2010/9/24镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
能传我一份示例么?不胜感激! 需要Key是自己定义的struct
【 在 ericyosho 的大作中提到: 】
: linux 平台下,和windows平台下,都有自己的hashmap。
: 直接条件编译算了。