请问.net 中的hash_map

mmosquito 2003-06-20 08:05:49
我想使用一个
hash_map<string,int>
代码如下:
std::hash_map<std::string,int> myMap;
myMap["hello"] = 1;

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

请问如何实现?
...全文
30 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmosquito 2003-08-24
  • 打赏
  • 举报
回复
谢谢qqchen79
没想到沉了这么久的帖子也有人看,再次感谢ms mvp
其实不久之后我也解决了这个问题,ms没有文档,其实看看源代码就好。
magicblue 2003-08-24
  • 打赏
  • 举报
回复
qqchen79(知秋一叶 [MS MVP])

it seems too fragile. why MS insist on using P.J's stl rather than SGI?

i create a template class to implement serialization. when i compiling it, i get a dozen of errors in xdebug file that belong to P.J stl
nsly 2003-08-22
  • 打赏
  • 举报
回复
我觉得是现insert后,然后才可以赋值。乱说的,因为没有做过。
qqchen79 2003-08-21
  • 打赏
  • 举报
回复
stl_port allows string to be used as the key of hash_set/hash_map because it provides a default implementation of Hash<string> (the hash functor specialized for string).

On the other hand, VC7 Stl doesn't provide such a functor. Thus you have to work out one on your own:

using namespace std;

class string_hash
{
public:
enum
{ // parameters for hash table
bucket_size = 4, // 0 < bucket_size
min_buckets = 8
}; // min_buckets = 2 ^^ N, 0 < N

string_hash() {}
size_t operator()(const string& key) const
{
long h = 0;
for(int i = 0; i < key.size(); ++ i) {
h += key[i];
}
return (size_t)h;
}

bool operator()(const string& key1, const string& key2) const
{
return (key1.compare(key2) < 0);
}
};

//this is just a simple implementation demonstrates what need to be done for a
//hash functor. It's not optimized.
....
hash_map<std::string, int, string_hash> myMap;
....
bm1408 2003-08-20
  • 打赏
  • 举报
回复
看了一下!

应该为:
std::hash_map<std::string,int> myMap;
myMap["hello"] = 1;
bm1408 2003-08-20
  • 打赏
  • 举报
回复
up!
ahao 2003-08-19
  • 打赏
  • 举报
回复
.net2003上通过
没问题
mmosquito 2003-06-23
  • 打赏
  • 举报
回复
只允许int作key,还叫什么模板?stl_port就可以
std::hash_map<std::string,int> myMap;
myMap["hello"] = 1;
joachern 2003-06-23
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
为什么要把string作为key呢?好像这个key只能是int?

如果不是非要把string作为key,那么
std::hash_map<int,std::string> myMap;
myMap[1] ="hello";

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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