0

I want to use a custom class of mine as the value of a std::unordered_map

such as

class customClass {
    private:
        int m_age;
        std::string m_name;
    public:
        int getAge() { return m_age; }
        std::string getName() { return m_name; }
};
...
std::unordered_map<std::string, customClass> entries;

I know for this to work and me be able to insert key / value pairs I have to overload some operators but do I overload the == operator for std::string? something else? Bit lost, cheers for any help

nectarine
  • 65
  • 4
  • You want your class as the **value** or the **key** of this `unordered_map`? You don't need to do anything to use it as the _value_. – Drew Dormann Feb 02 '21 at 21:51
  • You don't need to do anything. The requirements only apply to the key type and the standard library already has everything needed for `std::string`. For a duplicate for what you need for your own type, see: https://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key – NathanOliver Feb 02 '21 at 21:54

1 Answers1

1

std::unordered_map<std::string, myCustomClass> what operators to overload and how?

You don't need to implement any particular operators. The type just needs to be constructible, somehow.

Your class already is sufficient for the job.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180