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);
pair 的關係運算符
lhsrhs 這兩個 pair 物件之間執行適當的比較操作。

如果兩個 pair 物件的 first 成員彼此相等,並且 second 成員也彼此相等(在這兩種情況下都使用 operator== 進行比較),則這兩個物件相等。

類似地,運算子 <><=>= 在由成員 firstsecond 形成的序列上執行字典序比較(在所有情況下都使用 operator< 自反地進行比較)。

它們的行為就好像定義為
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); }

這些運算子在標頭檔案 <utility> 中被過載。

引數

lhs, rhs
pair 物件(分別位於運算子的左側和右側),具有相同的模板引數(T1T2)。

示例

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;
}

輸出
foo and bar are not equal
foo is less than bar
foo is less than or equal to bar


返回值

如果條件成立,則為 true;否則為 false

資料競爭

訪問兩個物件 lhsrhs,並訪問最多其所有成員。
在任何情況下,該函式都無法修改其引數(const 限定)。

異常安全

如果成員的型別支援具有無異常保證的適當操作,則該函式永遠不會引發異常(無異常保證)。
如果成員的型別不支援使用適當的運算子進行比較,則會導致未定義的行為

另見