jeudi 7 avril 2016

Interchangeable use of hash_map and unordered_map (C++11)

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?

Aucun commentaire:

Enregistrer un commentaire