-9

How does the < and > operators work on pair when first value of pair is equal? I have read 2 different concepts on this which has confused me.

eg.

pair<int,int> p1(5,10);
pair<int,int> p2(5,12);

What should be the answer of p1 > p2 ?

Vanshika
  • 49
  • 1
  • 6
  • 1
    `p1 > p2` is false in the example you give because 10 > 12 is false – john Jul 29 '20 at 09:17
  • 4
    When in doubt take a look at [the reference](https://en.cppreference.com/w/cpp/utility/pair/operator_cmp): _"Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements."_ – Lukas-T Jul 29 '20 at 09:17
  • 3
    What are the two different concepts that you have read? – Caleth Jul 29 '20 at 10:24

1 Answers1

3

As usual the answer is in the docs:

Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements.

Return value:

operator<

If lhs.first<rhs.first, returns true. Otherwise, if rhs.first<lhs.first, returns false. Otherwise, if lhs.second<rhs.second, returns true. Otherwise, returns false

operator>

rhs < lhs

bolov
  • 72,283
  • 15
  • 145
  • 224