0

From an earlier post, Difference between hash_map and unordered_map?, I understand that hash_map that existed in prior-C++-11 was kind of non-standard and that has been replaced by standard unordered_map as interchangeable one. However, I have some doubt. Earlier (prior to C++11), we used hash_map as:

hash_map<string,string,shash,seq> some_hashmap;

where shash and seq could be defined as below:

class shash {
  public:
    size_t operator()(const string &a) const {
      register size_t ret = 0;
      register string::const_iterator a,e;
      a = s.begin();
      e = s.end();
      for(;a != e;++a) {
        ret = (ret << 5) + (ret >> 2) + *a;
      }
      return(ret);
    }
  };
class seq {
  public:
    bool operator()(const string &str1,const string &str2) const {
      register const char *s1 = str1.c_str();
      register const char *s2 = str2.c_str();
      for(;*s1 && *s2 && *s1 == *s2;++s1,++s2);
      return(!*s1 && !*s2);
    }
 };

Can unordered_map (C++11) be used interchangeably here? Or, is there any better alternative of shash and seq, or can they be omitted in unordered_map?

Community
  • 1
  • 1
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • you can (in some cases you must) definitely set the hash function (**shash** here) in std::unordered_map – Andrey Lyubimov Apr 07 '16 at 08:45
  • 2
    What happened when you tried it? – Kerrek SB Apr 07 '16 at 08:48
  • You have checked a reference about `std::unordered_map`, for example [this one](http://en.cppreference.com/w/cpp/container/unordered_map)? And what do you mean with the phrase "used interchangeably"? Can you elaborate on that? – Some programmer dude Apr 07 '16 at 08:49
  • By interchangeably, I mean unordered_map of C++11 can replace earlier hash_map with its usual intentions of having key-value pair and having same interface for usage – Dr. Debasish Jana Apr 07 '16 at 09:02
  • @KerrekSB Seems fine when I migrate to C++11, just wanted to confirm from the experts at Stackoverflow – Dr. Debasish Jana Apr 07 '16 at 09:03
  • FYI `seq`'s `bool operator()(const string &str1,const string &str2) const` can just be `return str1 == str2;`. – NathanOliver Apr 07 '16 at 14:25
  • The question implies there was a single `hash_map` implementation in circulation, but there were a great many, and the differences from C++11's `unordered_map` will vary. Are you specifically asking about the SGI STL implementation, Microsoft's `stdext::hash_map`, `__gnu_cxx::hash_map`...? Anyway - the quality of C++11 Standard Library `std::hash` functions varies widely, and some libs use power-of-two bucket counts / others prime, so whether `shash` is worth providing will vary. It's ***not*** a good idea to provide your own string comparison implementation. – Tony Delroy Apr 08 '16 at 02:24

0 Answers0