gpt4 book ai didi

c++ - 为什么不同字符串的 std::Hash 相等?

转载 作者:行者123 更新时间:2023-11-28 02:53:42 24 4
gpt4 key购买 nike

<分区>

为什么 std::Hash 对于不同的字符串有相同的结果?我使用的是 msvc2010sp1,当看到这个结果时我很惊讶:

int _tmain(int argc, _TCHAR* argv[])
{
std::string sUniqId ("IndexBuf");
std::stringstream sStream;

sStream << 10;
std::string sUniqId10 (sUniqId);
sUniqId10.append (sStream.str());
size_t uHashStr = std::hash<std::string>()(sUniqId10);

sStream.str("");
sStream << 11;
std::string sUniqId11 (sUniqId);
sUniqId11.append(sStream.str());
size_t uHashStr1 = std::hash<std::string>()(sUniqId11);

sStream.str("");
sStream << 12;
std::string sUniqId12 (sUniqId);
sUniqId12.append(sStream.str());
size_t uHashStr2 = std::hash<std::string>()(sUniqId12);

cout <<"str: " << sUniqId10.c_str() << "\t" << "Hash1: " << uHashStr << endl;
cout <<"str2: " << sUniqId11.c_str() << "\t" << "Hash2: " << uHashStr1 << endl;
cout <<"str3: " << sUniqId12.c_str() << "\t" << "Hash3: " << uHashStr2 << endl;

return 0;
}

输出:

str:  IndexBuf10        Hash1: 1286096800
str2: IndexBuf11 Hash2: 1286096800
str3: IndexBuf12 Hash3: 1286096800

有人知道为什么会这样吗?

附注此示例适用于 msvc2013 update1

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com