function template
<utility>

std::relational operators (pair)

(1)
template <class T1, class T2>
  bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
(2)
template <class T1, class T2>
  bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
(3)
template <class T1, class T2>
  bool operator<  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
(4)
template <class T1, class T2>
  bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
(5)
template <class T1, class T2>
  bool operator>  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
(6)
template <class T1, class T2>
  bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
Relational operators for pair
Performs the appropriate comparison operation between the pair objects lhs and rhs.

Two pair objects compare equal to each other if both their first members compare equal to each other and both their second members compare also equal to each other (in both cases using operator== for the comparison).

Similarly, operators <, >, <= and >= perform a lexicographical comparison on the sequence formed by members first and second (in all cases using operator< reflexively for the comparisons).

They behave as if defined as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class T1, class T2>
  bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return lhs.first==rhs.first && lhs.second==rhs.second; }

template <class T1, class T2>
  bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(lhs==rhs); }

template <class T1, class T2>
  bool operator<  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); }

template <class T1, class T2>
  bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(rhs<lhs); }

template <class T1, class T2>
  bool operator>  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return rhs<lhs; }

template <class T1, class T2>
  bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(lhs<rhs); }


These operators are overloaded in header <utility>.

Parameters

lhs, rhs
pair object (to the left- and right-hand side of the operator, respectively), having both the same template parameters (T1 and T2).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// pair relational operators
#include <utility>      // std::pair
#include <iostream>     // std::cout

int main ()
{
  std::pair<int,char> foo (10,'z');
  std::pair<int,char> bar (90,'a');

  if (foo==bar) std::cout << "foo and bar are equal\n";
  if (foo!=bar) std::cout << "foo and bar are not equal\n";
  if (foo< bar) std::cout << "foo is less than bar\n";
  if (foo> bar) std::cout << "foo is greater than bar\n";
  if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
  if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";

  return 0;
}


Output:
foo and bar are not equal
foo is less than bar
foo is less than or equal to bar

Return Value

true if the condition holds, and false otherwise.

Data races

Both objects, lhs and rhs, are accessed, and up to all of its members are accessed.
In any case, the function cannot modify its arguments (const-qualified).

Exception safety

If the type of the members supports the appropriate operation with no-throw guarantee, the function never throws exceptions (no-throw guarantee).
If the type of the members do not support being compared with the proper operator, it causes undefined behavior.

See also