hash_map testMap; 为什么会出错。

ttzzgg_80713 2002-06-08 09:35:35
class test
{
public:
test();
virtual ~test();
};

void main( void )
{
typedef std::hash_map<string, test> testHash;
typedef hash_map<string, test>::iterator iterHash;
typedef pair<string, test> testPair;

testPair tp;
test t;
testHash th;
iterHash iter;

tp.first = string("test1");
tp.second = t;

th.insert(tp);

iter = th.begin();

printf(iter->first.c_str());
printf("\n");

}


D:\Program Files\Microsoft Visual Studio .NET\Vc7\include\xhash(38): error C2440: “类型转换” : 无法从“const std::string”转换为“size_t”

???为什么,谢谢大家
...全文
504 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcl_tk 2002-06-13
  • 打赏
  • 举报
回复
这个问题VC stl的作者给了个回答以及解决办法,如下:

The V7 version of hash_map has no default hash function for strings.
We've already fixed this oversight -- it will appear in a future
VC++ release. For now, you can add your own comparator along the
lines shown below:

struct mycomp
{ // define hash function for strings
enum
{ // parameters for hash table
bucket_size = 4, // 0 < bucket_size
min_buckets = 8}; // min_buckets = 2 ^^ N, 0 < N

size_t operator()(const string& s1) const
{ // hash string s1 to size_t value
const unsigned char *p = (const unsigned char *)s1.c_str();
size_t hashval = 0;

for (size_t n = s1.size(); 0 < n; --n)
hashval += *p++; // or whatever
return (hashval);
}

bool operator()(const string &s1, const string &s2) const
{ // test if s1 ordered before s2
return (s1 < s2);
}
};

To use:

hash_map<string, int, mycomp> mymap;

HTH,

P.J. Plauger
Dinkumware, Ltd.
tcl_tk 2002-06-10
  • 打赏
  • 举报
回复
我使用VC7+STLPort进行了测试,没有问题。但是使用VC7的hash_map就出现问题。可能是一个Bug。建议使用STLPort。
hash_map的key是可以使用string的,而且很常用到
fangrk 2002-06-10
  • 打赏
  • 举报
回复
Defined in the header hash_map, and in the backward-compatibility header hash_map.h. This class is an SGI extension; it is not part of the C++ standard.
ttzzgg_80713 2002-06-10
  • 打赏
  • 举报
回复
不错,不错。stlport可以。结了
ttzzgg_80713 2002-06-10
  • 打赏
  • 举报
回复
真的吗?我去下个看看。成功就给分。
ttzzgg_80713 2002-06-09
  • 打赏
  • 举报
回复
VC.net中可以有。看了楼上各位的意思,好象是不能用 string作key值??是吗?
ttzzgg_80713 2002-06-09
  • 打赏
  • 举报
回复
upup
ttzzgg_80713 2002-06-09
  • 打赏
  • 举报
回复
up
masterdog 2002-06-08
  • 打赏
  • 举报
回复
对呀!我也觉得奇怪,我曾用链地址法作存储结构、除留余数法作散列函数编了个hash_map,发现用户定义的数据结构必须含有比较成分,即必须自行定义相等条件或者不等条件,否则没法比较。
codingcoding 2002-06-08
  • 打赏
  • 举报
回复
vc6不支持hash_map linux下可以。或者你可以再window下用cygwin
上面这个程序包含的头文件不对,要改正一下再用
codingcoding 2002-06-08
  • 打赏
  • 举报
回复
th.insert(tp);
这一句有问题。如果pair有一个匹配的值的话,她不被允许插入到hash_map。
你可以看看这个程序,
#include <iostream>
#include <functional>
#include <ospace/stl/hashmap.h>

typedef hash_map< char, int, hash<char>, equal_to<char> > map_type;

void
main()
{
map_type m;

// Store mappings between roman numerals and decimals.
m[ 'l' ] = 50;
m[ 'x' ] = 20; // Deliberate mistake.
m[ 'v' ] = 5;
m[ 'i' ] = 1;
cout << "m[ 'x' ] = " << m[ 'x' ] << "\n";

m[ 'x' ] = 10; // Correct mistake.
cout << "m[ 'x' ] = " << m[ 'x' ] << "\n";

// Note default value ( 0 ) is added.
cout << "m[ 'z' ] = " << m[ 'z' ] << "\n";
cout << "m.count( 'z' ) = " << m.count( 'z' ) << "\n";

pair< map_type::iterator, bool > p;
p = m.insert( pair< char, int >( char, int )( 'c', 100 ) );
if ( p.second )
cout << "First insertion successful\n";

p = m.insert( pair< char, int >( char, int )( 'c', 100 ) );
if ( p.second )
cout << "Second insertion successful\n";
else
cout
<< "Existing pair "
<< ( *( p.first ) ).first
<< " -> "
<< ( *( p.first ) ).second
<< "\n";
}
mickwang 2002-06-08
  • 打赏
  • 举报
回复
vc6中使用hash_map包含那个文件?
ttzzgg_80713 2002-06-08
  • 打赏
  • 举报
回复
救命呀。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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