-1

So if you create a map with a class as the key. Then the keys are pointers to classes?

If std::string is a class, how does it manage to use character array data as keys in the map??

So the comparator can make 2 'different' class instances appear equal because their 'strings' match? If you confirm this, I shall mark your answer.

  • 1
    No, keys are not pointers to classes, unless you make a map that uses `string*` keys (a bad idea). Map uses operator implementation for `<` to compare keys. – Sergey Kalinichenko May 12 '18 at 19:18
  • Do you mean a class like `class Foo{ int val; std::string s}`, and then only comparing the values of `s` to sort? Some sample code would make the question much clearer. – wally May 13 '18 at 15:43

1 Answers1

0

The key itself is stored in a std::map. So if you have a class called Foo then an instance of Foo is stored in the map.

Whether you use Foo or std::string as the key doesn't matter to the std::map. What does matter is that there is a comparator defined so that the std::map can know how to sort the classes as keys.

wally
  • 10,717
  • 5
  • 39
  • 72