【发布时间】:2010-06-28 16:52:53
【问题描述】:
我有这样的代码。我用
pvalueholder 是多态的类,它可以容纳各种类型,字符串..等。 它也可以有一个未定义的类型。
typedef hash_map<pvalueholder,pvalueholder,pvaluehasher > hashtype;
hashtype h;
pvalueholder v;
v="c";
h[v]=5; // h has one element
pvalueholder v2=h[v]; // here h gets a new key/value how is that possible?
cout << (string) (h[v]) << endl; // here h gets another new key/value how is that possible?
int i =0;
for (hashtype::iterator h1=h.begin(); h1!=h.end();h1++)
{
cout << "no: " << i++ << endl;
} // this prints three lines, it should print one...
这里有两个值是未定义的,第三个是预期的5。
size_t pvaluehasher::operator() (const pvalueholder& p) const
{
cout << "hashvalue:" << p.value->hashvalue() << endl;
return p.value->hashvalue();
}
返回 这是打印的内容: 哈希值:84696444 哈希值:84696444 哈希值:84696444 返回:1 哈希值:84696444 返回:1 哈希值:84696444 返回:1 返回:1 哈希值:84696444
你有什么想法吗? 谢谢。
解决方案: 对于 Microsoft STL,函数 operator()(parameter1,parameter2) 需要不同。 对于 microsoft,它需要返回 parameter1 和 parameter2 之间的小于关系。 对于 gcc,它需要返回相等性。我返回平等。 键的比较功能不正确... 该函数返回 true 表示相等,而它必须返回小于 Microsoft STL 的情况。
【问题讨论】:
标签: visual-c++ visual-studio-2008 stl