hash_set vs hash_map

I have a class that looks like:

1
2
3
4
5
6
7
8
class MyClass {
private:
   std::string _name;
   // Some other stuff
public:
   std::string getName(); // returns _name 
   // Some other stuff
};



I want to store pointers of MyClass in a data structure that is efficient in lookup (and insertion if possible). Order is not important. I will search for objects using their name (_name).

After some research, I have come to the decision that I should either use __gnu_cxx::hash_set or __gnu_cxx::hash_map. hash_set looked like a better option since it will probably use less memory (I don't have to store objects name redundantly). However, it is not really an associative container. hash_map, in this case, can be the correct option. Can someone help me?

I decided to use hash_set and I defined the compare (eq) and hash functions based on _name:

1
2
3
4
5
6
7
8
9
10
11
class MyEq {
public:
    bool operator() ( MyClass* obj1,  MyClass* obj2) const
    {	return (obj1->getName()==obj2->getName()); }
};

class MyHash {
public:
    size_t operator()( MyClass* obj) const
    {	return SuperFastHash(obj->getName().c_str(), obj->getName().length()); }
};

SuperFastHash --> http://www.azillionmonkeys.com/qed/hash.html

The problem is that when I try to lookup an object with its name, I do a dirty trick like the following:

1
2
3
4
5
6
// Create a temp class just to search for the given name
MyClass* temp = new MyClass();
temp->setName("Name-To-Be-Searched");
myHashSetContainer.find(temp);
delete temp;
// other stuff .... 

What is the optimal solution for this problem? I have millions of MyClass objects around. These objects are created once at the beginning, then hundreds of millions of lookups are done. Would hash_map be a better option? Or some other container?


Last edited on
Why did you choose the proprietary hash_set and hash_map over std::tr1::unordered_set and std::tr1::unordered_map?
Thanks for mentioning std based containers. I can use std::tr1::unordered_set or std::tr1::unordered_map instead of hash_set/hash_map. I did not really try to compare std containers to hash_set/hash_map.

The basic question is whether I can use hash based set instead of hash based map given the definition of the problem. Or, which one is a better option? hash based map or hash based set?
Use a map with the name as the key.
Topic archived. No new replies allowed.