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:
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?
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?