Search code examples
c++c++11stlstdhash

Why std::Hash equal for differen strings?


Why std::Hash has equal result for different strings? I used msvc2010sp1 and I was suprised when saw this result:

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;
  }

output:

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

Anybody know why this occur?

p.s. This example work correctly for msvc2013 update1


Solution

  • Hashes are not required to be unique. For example, many algorithms first hash to select a "bucket" which is a linked-list of the actual items. Most likely the hash algorithm changed between versions.