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 7546211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:08:25+00:00 2026-05-30T09:08:25+00:00

When I make a std::map<my_data_type, mapped_value> , what C++ expects from me is that

  • 0

When I make a std::map<my_data_type, mapped_value>, what C++ expects from me is that my_data_type has its own operator<.

struct my_data_type
{
    my_data_type(int i) : my_i(i) { }

    bool operator<(const my_data_type& other) const { return my_i < other.my_i; }

    int my_i;
};

The reason is that you can derive operator> and operator== from operator<. b < a implies a > b, so there’s operator>. !(a < b) && !(b < a) means that a is neither less than b nor greater than it, so they must be equal.

The question is: Why hasn’t the C++ designer require operator== to be explicitly defined? Obviously, operator== is inevitable for std::map::find() and for removing duplicates from the std::map. Why implement 5 operations and call a method twice in order not to compel me to explicitly implement operator==?

  • 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-30T09:08:27+00:00Added an answer on May 30, 2026 at 9:08 am

    operator== is inevitable for std::map::find()

    This is where you go badly wrong. map does not use operator== at all, it is not “inevitable”. Two keys x and y are considered equivalent for the purposes of the map if !(x < y) && !(y < x).

    map doesn’t know or care whether you’ve implemented operator==. Even if you have, it need not be the case that all equivalent keys in the order are equal according to operator==.

    The reason for all this is that wherever C++ relies on orders (sorting, maps, sets, binary searches), it bases everything it does on the well-understood mathematical concept of a “strict weak order”, which is also defined in the standard. There’s no particular need for operator==, and if you look at the code for these standard functions you won’t very often see anything like if (!(x < y) && !(y < x)) that does both tests close together.

    Additionally, none of this is necessarily based on operator<. The default comparator for map is std::less<KeyType>, and that by default uses operator<. But if you’ve specialized std::less for KeyType then you needn’t define operator<, and if you specify a different comparator for the map then it may or may not have anything to do with operator< or std::less<KeyType>. So where I’ve said x < y above, really it’s cmp(x,y), where cmp is the strict weak order.

    This flexibility is another reason why not to drag operator== into it. Suppose KeyType is std::string, and you specify your own comparator that implements some kind of locale-specific, case-insensitive collation rules. If map used operator== some of the time, then that would completely ignore the fact that strings differing only by case should count as the same key (or in some languages: with other differences that are considered not to matter for collation purposes). So the equality comparison would also have to be configurable, but there would only be one “correct” answer that the programmer could provide. This isn’t a good situation, you never want your API to offer something that looks like a point of customization but really isn’t.

    Besides, the concept is that once you’ve ruled out the section of the tree that’s less than the key you’re searching for, and the section of the tree for which the key is less than it, what’s left either is empty (no match found) or else has a key in it (match found). So, you’ve already used current < key then key < current, leaving no other option but equivalence. The situation is exactly:

    if (search_key < current_element)
        go_left();
    else if (current_element < search_key)
        go_right();
    else
        declare_equivalent();
    

    and what you’re suggesting is:

    if (search_key < current_element)
        go_left();
    else if (current_element < search_key)
        go_right();
    else if (current_element == search_key)
        declare_equivalent();
    

    which is obviously not needed. In fact, it’s your suggestion that’s less efficient!

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

Sidebar

Related Questions

std::map<std::string, int> m; // Can I make assumption that m[NoSuchKey] will return 0? std::cout
I' m re-implementing std::map . I need to make sure that any data type
I wanted to make a map with my own struct 'Point2' as key, however
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
Suppose some data structure: typedef struct { std::string s; int i; } data; If
Extends . I have: struct Coord { int row, col ; bool operator<( const
I've a incertitude about std::map on c++: I did a Object C_Configuration that loads
consider this translation unit: #include <map> #include <string> int main() { std::map<std::string, std::size_t> mp;
I'm trying to export classes from a DLL that contain objects such as std::vectors
I wondered, for(std::map<int, double, std::greater<int> >::iterator it = mymap.begin(); it != mymap.end(); ++it) {

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.