Is there any nice way to use an unordered_map so that you can access objects by a member variable in constant time (average case)? The following example has this functionality but requires the name of each Person to be duplicated as the Key:
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
class Person {
public:
Person() : name_("") {}
Person(const std::string& name) : name_(name) {}
std::string getName() const { return name_; }
void kill() const { std::cout << name_ << " is dead!" << std::endl; }
private:
std::string name_;
};
int main(int argc, const char* argv[]) {
Person p1("dave");
Person p2("bob");
std::unordered_map<std::string, Person> map = {
{p1.getName(), p1}, // Duplicating the
{p2.getName(), p2} // keys here
};
map["dave"].kill();
return 0;
}
I’m thinking that somehow the value_type would need to be Person itself, instead of a pair<string, Person> and the unordered_map would need to know to use Person::getName when hashing and accessing objects.
The ideal solution would allow me to set up an unordered_map (or unordered_set if it’s more apt for the job) that knows to use Person::getName to get the key of each object. I would then be able to insert them simply by giving the object (and no key because it knows how to get the key) and access them by giving keys that would compare equal to the return value of Person::getName.
Something along the lines of:
// Pseudocode
std::unordered_map<Person, Person::getName> map = {p1, p2};
map["dave"].kill();
So is it possible to instantiate an unordered_map template class that can do this neatly?
If you’re not opposed to using Boost, then Boost.MultiIndex makes this extremely simple without adding any needless inefficiency. Here’s an example that effectively creates an
unordered_setofPersonobjects that is keyed on the value ofPerson::getName():(Note that I changed the signature of
Person::getName()to return byconst-reference rather than by value for efficiency’s sake, but the change isn’t strictly required.)It should be noted that C++14’s support for transparent comparators would allow you to use
std::unordered_set<Person>here without any need for Boost.