Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:56:40+00:00 2026-05-24T17:56:40+00:00

Is there any nice way to use an unordered_map so that you can access

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:56:40+00:00Added an answer on May 24, 2026 at 5:56 pm

    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_set of Person objects that is keyed on the value of Person::getName():

    #include <string>
    #include <iostream>
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/indexed_by.hpp>
    #include <boost/multi_index/hashed_index.hpp>
    #include <boost/multi_index/mem_fun.hpp>
    
    namespace bmi = boost::multi_index;
    
    struct Person
    {
        Person() = default;
        Person(std::string const& name) : name_(name) { }
        std::string const& getName() const noexcept { return name_; }
        void kill() const { std::cout << name_ << " is dead!\n"; }
    
    private:
        std::string name_;
    };
    
    typedef bmi::multi_index_container<
        Person,
        bmi::indexed_by<
            bmi::hashed_unique<
                bmi::const_mem_fun<Person, std::string const&, &Person::getName>
            >
        >
    > PersonUnorderedSet;
    
    int main()
    {
        Person p1("dave");
        Person p2("bob");
    
        PersonUnorderedSet set;
        set.insert(p1);
        set.insert(p2);
    
        set.find("dave")->kill();
    
        // for exposition, find is actually returning an iterator
        PersonUnorderedSet::const_iterator iter = set.find("bob");
        if (iter != set.end())
            iter->kill();
    
        // set semantics -- newly_added is false here, because
        // the container already contains a Person named 'dave'
        bool const newly_added = set.insert(Person("dave")).second;
    }
    

    (Note that I changed the signature of Person::getName() to return by const-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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to introduce the local variable iWantThisHere here, so i can
Hi are there any nice videos on how to use exceptions in Delphi.
Hi are there any nice videos or other resources on how to use Interfaces
Is there any way to say that if prerequisite for the given target doesn't
Is there any way to configure VS2010 to use different color schemes for different
Is there any way to do this? I have a set of items that
I was wondering, is there any way to perform random access in multimap's values.
Is there any way to easily use a remote javascript file as a content
Is there a nice way of achieving the following, without any additional mark-up? It
Is there any way to check spelling for ALL the words that I type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.