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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:04:02+00:00 2026-05-28T06:04:02+00:00

As according to cppreference : In inequality comparisons (<, >), the first elements are

  • 0

As according to cppreference:

In inequality comparisons (<, >), the first elements are compared
first, and only if the inequality comparison is not true for them, the
second elements are compared.

which translates to something like this:

return ((a.first < b.first) || (!(b.first < a.first) && (a.second < b.second)));

My quesion is, why is it so unintuitive? What is the reasoning behind it? And are there examples where this reasoning leads to the correct answers?

I thought the implementation will simply be:

return a.first < b.first && a.second < b.second
  • 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-28T06:04:03+00:00Added an answer on May 28, 2026 at 6:04 am

    This sort of comparison is called a lexicographical ordering and is one of the more natural ways to combine two different orderings into one.

    The orderings that are requested in C++ are called strict weak orderings. This means that the following should be true:

    • Irreflexivity: x < x is always false.
    • Transitivity: If x < y and y < z, then x < z.
    • Antisymmetry: If x < y, then y < x is always false.
    • Transitivity of Equivalence: If x and y are incomparable and y and z are incomparable, then x and z are incomparable.

    These properties are what you need in order to guarantee that you can take a list of objects and put them into sorted ascending order. This means that you can use std::sort on them, or store them in a std::set.

    You can prove with a bit of math that if you have two different strict weak orderings, then the lexicographical ordering you get by combining them as std::pair does is also a strict weak ordering. The lexicographical ordering is one of the few ways that you can combine strict weak orderings to make new strict weak orderings.

    However, the ordering that you’ve suggested is not a strict weak ordering and will cause certain assumptions to break. In particular, consider the pairs (0, 5), (3, 3), and (1, 6). Then (0, 5) is incomparable with (3, 3) and (3, 3) is incomparable with (1, 6). However, we do indeed have that (0, 5) < (1, 6), which breaks the rule of transitivity of equivalence. As a result, many sorting algorithms that assume that equivalence is transitive (which includes most major sorting algorithms) will not work correctly on your range, meaning that std::sort might behave incorrectly. It also means that you also couldn’t store these in a std::set, because the std::set interally stores everything in some kind of sorted order (usually a balanced binary search tree) and you might get completely wrong results.

    Hope this helps!

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

Sidebar

Related Questions

According to MSDN form.RightToLeftLayout = True; form.RightToLeft = ifWeWantRTL() ? RightToLeft.True : RightToLeft.False; is
According to Wikipedia, on the Comparison of programming languages page, it says that F#
According to http://en.cppreference.com/w/cpp/string/byte/memcpy C++'s memcpy takes three parameters: destination, source and size/bytes. It also
According to what I've heard, integer values that are not the processor's word size
According to be Spring Framework documentation, logging is the only mandatory dependency of Spring.
According to MDN documentation for JSON.stringify : Properties of non-array objects are not guaranteed
According to the Ruby Set class's documentation, == Returns true if two sets are
According to MS when you show a modal form in VB6 it does not
According documentation: System.Array.Sort<T> - sorts the elements in an entire System.Array using the System.IComparable
According Mac App Store rules 2.30: Program may write only to allowed locations. My

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.