yuanjia1011 / sparsehash

Automatically exported from code.google.com/p/sparsehash
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

when the dense_hash_map "key_type" is std::string, how to use it ? #57

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
===================================================================
#include <iostream>
#include <string>
#include "google/dense_hash_map"

using google::dense_hash_map;      // namespace where class lives by default
using namespace std;

inline unsigned int BKDRHash(const string& str)
{
   unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
   unsigned int hash = 0;

   for(std::size_t i = 0; i < str.size(); i++)
   {
      hash = (hash * seed) + str[i];
   }
   return hash;
}

struct my_hash
{
    size_t operator()(const string& str) const
    {
        return BKDRHash(str);
    }
};

int main()
{
  dense_hash_map<const string, string,  my_hash > months;
  months.set_empty_key("");
  string str = "march";
  months["march"] = "cx";
  months["september"] = "kevin";

  cout << "september -> " << months["september"] << endl;
  return 0;
}
====================================================================

What is the expected output? What do you see instead?
================================================================
g++ test2.cpp  -I/users/cx/lib/sparsehash-1.7/src
/users/cx/lib/sparsehash-1.7/src/google/sparsehash/densehashtable.h: In member 
function 'google::dense_hashtable<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, my_hash, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SelectKey, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SetKey, 
std::equal_to<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, google::libc_allocator_with_realloc<std::pair<const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > 
>::KeyInfo& google::dense_hashtable<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, my_hash, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SelectKey, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SetKey, 
std::equal_to<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, google::libc_allocator_with_realloc<std::pair<const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > 
>::KeyInfo::operator=(const google::dense_hashtable<std::pair<const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
my_hash, google::dense_hash_map<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, my_hash, std::equal_to<const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SelectKey, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::KeyInfo::operator=(const 
google::dense_hashtable<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, my_hash, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SelectKey, 
google::dense_hash_map<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, my_hash, std::equal_to<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >::SetKey, 
std::equal_to<const std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, google::libc_allocator_with_realloc<std::pair<const 
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > 
>::KeyInfo&)' first required here 
=======================================================================

What version of the product are you using? On what operating system?
gcc 3.4, under linux.

Original issue reported on code.google.com by cx3...@gmail.com on 4 Jul 2010 at 3:37

GoogleCodeExporter commented 9 years ago
Those template error messages from gcc are pretty great, aren't they?

Try removing the 'const' from
   dense_hash_map<const string, string,  my_hash > months;

Original comment by csilv...@gmail.com on 4 Jul 2010 at 7:48

GoogleCodeExporter commented 9 years ago
Oh, these error messages are boring me to death……
By the way, I removed the 'const', but it still doesn't work.
Can you give me a simple example about the 'key_type=string'?
Thanks a lot :)  

Original comment by cx3...@gmail.com on 5 Jul 2010 at 12:59

GoogleCodeExporter commented 9 years ago
Are you *sure* the code still fails to compile when the const is removed?  It 
fails for me with the const, but works fine with the const removed.

The problem is that set_deleted_key() tries to assign deleted_key to something 
of type key_type.  Obviously, you can't set anything to a const string, so the 
compile complains.

Original comment by csilv...@gmail.com on 6 Jul 2010 at 3:43

GoogleCodeExporter commented 9 years ago
I haven't heard anything more, so I'm going to assume the problem here is with 
the extraneous const in your key-type.  I've committed a fix to that particular 
problem, and it will be in the next sparsehash release.

Original comment by csilv...@gmail.com on 29 Jul 2010 at 5:15

GoogleCodeExporter commented 9 years ago
This should be fixed in sparsehash 1.8, just released.

Original comment by csilv...@gmail.com on 29 Jul 2010 at 9:53