2

As far as I know, map keeps the data inside sorted and uses the "<" operator for this purpose. What happens if I don't overload "<" operator for that class?

One more question. Should I write the overloading function inside the class or outside the class?

Thanks in advance.

tam bakaka
  • 353
  • 1
  • 2
  • 8
  • No, `std::map` doesn't use ` – deviantfan Oct 18 '15 at 10:48
  • I'm pretty sure you don't ask for _The STL_. Read the tag wiki please, before using this tag. – πάντα ῥεῖ Oct 18 '15 at 10:50
  • Your second question already has [good answers here on Stack Overflow](http://stackoverflow.com/questions/4622330/operator-overloading-member-function-vs-non-member-function). I answered your first question here because the information in the other question mentioned earlier in the comments doesn't address everything worth addressing here. –  Oct 18 '15 at 10:51
  • @πάνταῥεῖ Actually, the tag wiki supports this interpretation of STL. I agree with you, but the tag wiki does not. –  Oct 18 '15 at 10:52

1 Answers1

4

As far as I know, map keeps the data inside sorted and uses the "<" operator for this purpose.

Sort of, but not directly. std::map<Key, T, Compare, Allocator> uses Compare for that purpose. Compare defaults to std::less<Key>, which in turn typically defaults to using the < operator. However, even in the standard library implementation, there are cases where std::less<Key> behaves differently from <.

What happens if I don't overload "<" operator for that class?

That depends. You can specify a different class as the Compare template argument which avoids using std::less. You can add a specialisation of std::less which avoids using <. But if you don't do either, you'll typically get error messages about < not being defined.