hash_map对于自己定义的机构有限制吗?最大多少字节?

plasrj 2009-02-10 05:50:34
如题,还有下面这个程序问什么不通?也是关于hashmap的

#include "stdafx.h"
#include <fstream>
#include <stdio.h>
#include <string>
#include <hash_map>
using namespace std;
using namespace stdext;

typedef struct
{
char dip;
}flow4tuple;

struct greater_flow {
bool operator()( flow4tuple & x, flow4tuple & y) const{
if( x.sip>y.sip)

return false;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
hash_map< flow4tuple,int,hash_compare< flow4tuple,greater_flow> > iphash;

typedef pair < flow4tuple,int> flow4tuplepair;
hash_map< flow4tuple, int>::iterator hash1;

flow4tuple tempflow4tuple;
tempflow4tuple.dip=1;


iphash.insert(flow4tuplepair(tempflow4tuple,2));

if((hash1=iphash.find(tempflow4tuple))!=iphash.end())
printf("mlgb");

}


...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
plasrj 2009-02-10
  • 打赏
  • 举报
回复
谢谢楼上
Vitin 2009-02-10
  • 打赏
  • 举报
回复
hash_map 还没有进入标准。

不过,对于新特性,一般不会有这种限制。除了运行环境本身的限制,没有其他的了。
Vitin 2009-02-10
  • 打赏
  • 举报
回复

struct greater_flow {
bool operator()( const flow4tuple & x, const flow4tuple & y) const{
if( x.dip>y.dip)
return true; // 原先的定义不满足 strict weakly ordering
else
return false;
}
};

inline size_t hash_value(flow4tuple f)
{
return (hash_value(f.dip));
}

两个问题:

1、原先的 greater_flow 的语义为“小于等于”,不满足 strict weakly ordering 的要求,不能作为比较函数;上述代码改为“大于”,可满足要求;

2、对于新类型,需要定义它的hash函数。
plasrj 2009-02-10
  • 打赏
  • 举报
回复
自己顶一下,写错了,应该为结构

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧