名稱空間
變體
操作

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

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

bool operator==( const std::multiset<Key, Compare, Alloc>& lhs,

                 const std::multiset<Key, Compare, Alloc>& rhs );
(1)
template< class Key, class Compare, class Alloc >

bool operator!=( const std::multiset<Key, Compare, Alloc>& lhs,

                 const std::multiset<Key, Compare, Alloc>& rhs );
(2) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator<( const std::multiset<Key, Compare, Alloc>& lhs,

                const std::multiset<Key, Compare, Alloc>& rhs );
(3) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator<=( const std::multiset<Key, Compare, Alloc>& lhs,

                 const std::multiset<Key, Compare, Alloc>& rhs );
(4) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator>( const std::multiset<Key, Compare, Alloc>& lhs,

                const std::multiset<Key, Compare, Alloc>& rhs );
(5) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator>=( const std::multiset<Key, Compare, Alloc>& lhs,

                 const std::multiset<Key, Compare, Alloc>& rhs );
(6) (C++20 前)
template< class Key, class Compare, class Alloc >

synth-three-way-result<T>
    operator<=>( const std::multiset<Key, Compare, Alloc>& lhs,

                 const std::multiset<Key, Compare, Alloc>& rhs );
(7) (C++20 起)

比較兩個 multiset 的內容。

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

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

(C++20 起)

目錄

[編輯] 引數

lhs, rhs - 要比較內容的 multiset
-
為了使用過載 (1,2),Key 必須滿足 EqualityComparable 的要求。

[編輯] 返回值

1) 如果 multiset 的內容相等,則為 true,否則為 false
2) 如果 multiset 的內容不相等,則為 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 的大小不同,則為常數時間,否則為 multiset 大小的線性時間。
3-7) multiset 大小的線性時間。

[編輯] 註解

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

(C++20 前)

關係運算符是根據 synth-three-way 定義的,它儘可能使用 operator<=>,否則使用 operator<

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

(C++20 起)

[編輯] 示例

#include <cassert>
#include <compare>
#include <set>
 
int main()
{
    const std::multiset
        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
要求