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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:45:01+00:00 2026-06-06T03:45:01+00:00

I know it is bad practice to use equality instead of equivalence when sorting

  • 0

I know it is bad practice to use equality instead of equivalence when sorting a map.

//Equality
bool isEqual(const A& a1, const A& a2 ) 
{
   return a1 == a2 ;
} 

However I have a couple of very involved classes that I want to map and I have defined operator== for these classes, but not operator<.

Could anyone give me good reasons not to use operator== for map comparisons? I can not think of a example where it would break down in my class (I can add my class source code if needed)

Also should I just bite the built and write the new operators< :-/ ?

I would do something like this for my map.

 std::map<A,B,isEqual> ex1;

Also would

 ex1.find(A);

Now use equality instead of equivalence?

  • 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-06T03:45:04+00:00Added an answer on June 6, 2026 at 3:45 am

    Because std::map, std::set and their multi siblings are sorted structures as per the C++ standard, and equality cannot be used for sorting. On the other hand, strict weak ordering can be used to determine equality.

    As to why the structures are sorted, the standard requires that insertion and lookup be of logarithmic complexity, which is achievable by using a binary search tree. The only use for an equality comparison in such a structure is to test for the existence of an element.

    If you only had equality comparison, then element lookup would have to traverse the structure, making a comparison each time until the element is found, resulting in linear time complexity.

    If you did this

    std::map<A,B,isEqual> ex1;
    

    then the map’s comparison would not satisfy strict weak ordering, so neither the sorting of the map nor the element lookup would work. So iisn’t just bad practice, it just doesn’t work at all. You have to either implement operator< or provide a comparison functor, and which ever one you chose has to implement strict weak ordering. There is simply no way around that.

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

Sidebar

Related Questions

I know it's a bad practice to use database in the view. However, I'm
I know that having diamond inheritance is considered bad practice. However, I have 2
I know this is bad practice. Don't write code like this if at all
I know frames are bad. However I have to stick with them a little
As a common rule, it is very often considered a bad practice to use
I know that this would be bad practice although I know that I would
I know this is really bad practice, but it's already been put in place
Please let me know if this is bad practice or in someway a bad
Is it bad practice to use triggers or scripts to maintain data integrity that
I know document.write is considered bad practice; and I'm hoping to compile a list

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.