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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:48:00+00:00 2026-06-04T08:48:00+00:00

I have a class where I overload all the comparison operators. I load a

  • 0

I have a class where I overload all the comparison operators. I load a bunch of these objects into a map as keys and I expect the map to sort them least to greatest.

However, the map is actually sorted by greatest to least. Why is this? Does it not use the comparison operators on the key when it sorts the map? How can I make it do this?

  • 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-06-04T08:48:02+00:00Added an answer on June 4, 2026 at 8:48 am

    If you look at the definition for std::map, the 3rd parameter is the comparator – std::less< Key > by default. The default implementation of which is just to invoke operator<.

    You could try defining a new version in the header where YourType is defined, like so:

    template<> std::less< YourType >
    {
        bool operator()(
            const YourType& lhs,
            const YourType& rhs)
        {
            if(!(lhs.member1 < rhs.member1))
            {
               return false;
            }
    
            if(!(lhs.member2 < rhs.member2))
            {
               return false;
            }
    
            return true;
        }
    }
    

    Remember to friend std::less< YourType > in your class.

    The actual test must comply with the following or you’ll get all kinds of problems:

    • lhs < rhs = true
    • rha < lhs = false
    • lhs < lhs = false

    One important thing to note (that I discovered quite recently) is that if your compiler starts complaining that you’ve redefined the meaning of std::less or other funky things, it could be that you’ve only forward declared your YourType when you declared your std::map< YourType >.

    Hope this helps!

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

Sidebar

Related Questions

Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have a Card class and I want to overload the > operator to
I have class that represents users. Users are divided into two groups with different
I have a class Step derived from std::vector<unsigned int> . I need to overload
I have a simple class for which I want to overload operator as below
I have a class A that I overload its operator=. However it is required
I have a class that overloads object attribute access by returning the attributes of
I have class A, which exposes an event. an object of class B subscribed
I have class A: public B { ...} vector<A*> v; I want to do
I have class Fred { public: void inspect() const {}; void modify(){}; }; int

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.