名稱空間
變體
操作

operator==,!=,<,<=,>,>=,<=>(std::vector)

來自 cppreference.com
< cpp‎ | 容器‎ | vector
 
 
 
 
在標頭檔案 <vector> 中定義
template< class T, class Alloc >

bool operator==( const std::vector<T, Alloc>& lhs,

                 const std::vector<T, Alloc>& rhs );
(1) (C++20 起為 constexpr)
template< class T, class Alloc >

bool operator!=( const std::vector<T, Alloc>& lhs,

                 const std::vector<T, Alloc>& rhs );
(2) (C++20 前)
template< class T, class Alloc >

bool operator<( const std::vector<T, Alloc>& lhs,

                const std::vector<T, Alloc>& rhs );
(3) (C++20 前)
template< class T, class Alloc >

bool operator<=( const std::vector<T, Alloc>& lhs,

                 const std::vector<T, Alloc>& rhs );
(4) (C++20 前)
template< class T, class Alloc >

bool operator>( const std::vector<T, Alloc>& lhs,

                const std::vector<T, Alloc>& rhs );
(5) (C++20 前)
template< class T, class Alloc >

bool operator>=( const std::vector<T, Alloc>& lhs,

                 const std::vector<T, Alloc>& rhs );
(6) (C++20 前)
template< class T, class Alloc >

constexpr synth-three-way-result<T>
    operator<=>( const std::vector<T, Alloc>& lhs,

                 const std::vector<T, Alloc>& rhs );
(7) (C++20 起)

比較兩個 vector 的內容。

1,2) 檢查 lhsrhs 的內容是否相等,即它們擁有相同數量的元素,並且 lhs 中的每個元素與 rhs 中相同位置的元素比較相等。
3-6) 按字典順序比較 lhsrhs 的內容。比較透過等價於 std::lexicographical_compare 的函式執行。
7) 按字典順序比較 lhsrhs 的內容。比較的執行如同呼叫 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                       rhs.begin(), rhs.end(), synth-three-way)
返回型別是 synth-three-way 的返回型別(即 synth-three-way-result <T>)。
如果以下條件均不滿足,則行為未定義
  • T 滿足 three_way_comparable
  • 為型別(可能 const 限定的)T 的值定義了 <,並且 < 是全序關係。

<<=>>=!= 運算子分別從 operator<=>operator== 合成

(C++20 起)

目錄

[編輯] 引數

lhs, rhs - 要比較其內容的 vector
-
為使用過載 (1,2),T 必須滿足 EqualityComparable 的要求。
-
為使用過載 (3-6),T 必須滿足 LessThanComparable 的要求。排序關係必須建立全序。

[編輯] 返回值

1) 如果 vector 的內容相等,則為 true,否則為 false
2) 如果 vector 的內容不相等,則為 true,否則為 false
3) 如果 lhs 的內容按字典順序_小於_ rhs 的內容,則為 true,否則為 false
4) 如果 lhs 的內容按字典順序_小於或等於_ rhs 的內容,則為 true,否則為 false
5) 如果 lhs 的內容按字典順序_大於_ rhs 的內容,則為 true,否則為 false
6) 如果 lhs 的內容按字典順序_大於或等於_ rhs 的內容,則為 true,否則為 false
7) 如果 lhsrhs 中存在第一對不等價的元素,則為這些元素的相對順序,否則為 lhs.size() <=> rhs.size()

[編輯] 複雜度

1,2) 如果 lhsrhs 大小不同,則為常數時間,否則與 vector 的大小成線性關係。
3-7)vector 的大小成線性關係。

[編輯] 註解

關係運算符是根據元素型別的 operator< 定義的。

(C++20 前)

關係運算符透過 synth-three-way 定義,它如果可能則使用 operator<=>,否則使用 operator<

值得注意的是,如果元素本身不提供 operator<=>,但可以隱式轉換為三向可比較型別,則將使用該轉換而不是 operator<

(C++20 起)

[編輯] 示例

#include <cassert>
#include <compare>
#include <vector>
 
int main()
{
    const std::vector
        a{1, 2, 3},
        b{1, 2, 3},
        c{7, 8, 9, 10};
 
    assert
    (""
        "Compare equal containers:" &&
        (a != b) == false &&
        (a == b) == true &&
        (a < b) == false &&
        (a <= b) == true &&
        (a > b) == false &&
        (a >= b) == true &&
        (a <=> b) != std::weak_ordering::less &&
        (a <=> b) != std::weak_ordering::greater &&
        (a <=> b) == std::weak_ordering::equivalent &&
        (a <=> b) >= 0 &&
        (a <=> b) <= 0 &&
        (a <=> b) == 0 &&
 
        "Compare non equal containers:" &&
        (a != c) == true &&
        (a == c) == false &&
        (a < c) == true &&
        (a <= c) == true &&
        (a > c) == false &&
        (a >= c) == false &&
        (a <=> c) == std::weak_ordering::less &&
        (a <=> c) != std::weak_ordering::equivalent &&
        (a <=> c) != std::weak_ordering::greater &&
        (a <=> c) < 0 &&
        (a <=> c) != 0 &&
        (a <=> c) <= 0 &&
    "");
}

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3431 C++20 operator<=> 未要求 T
滿足 three_way_comparable
requires