名前空間
変種
操作

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

From cppreference.com
< cpp‎ | コンテナ‎ | deque
 
 
 
 
ヘッダー <deque> で定義
template< class T, class Alloc >

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

                 const std::deque<T, Alloc>& rhs );
(1)
template< class T, class Alloc >

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

                 const std::deque<T, Alloc>& rhs );
(2) (C++20まで)
template< class T, class Alloc >

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

                const std::deque<T, Alloc>& rhs );
(3) (C++20まで)
template< class T, class Alloc >

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

                 const std::deque<T, Alloc>& rhs );
(4) (C++20まで)
template< class T, class Alloc >

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

                const std::deque<T, Alloc>& rhs );
(5) (C++20まで)
template< class T, class Alloc >

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

                 const std::deque<T, Alloc>& rhs );
(6) (C++20まで)
template< class T, class Alloc >

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

                 const std::deque<T, Alloc>& rhs );
(7) (C++20以降)

2つのdequeの内容を比較します。

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>)です。
以下の条件のいずれも満たされない場合、動作は未定義です
  • Tthree_way_comparableのモデルです。
  • 型(おそらくconst修飾された)Tの値に対して<が定義されており、<は全順序関係です。

<, <=, >, >=, != 演算子は、それぞれ operator<=>operator== から合成されます。

(C++20以降)

目次

[編集] パラメータ

lhs, rhs - 比較するdeque
-
オーバーロード(1,2)を使用するには、TEqualityComparableの要件を満たす必要があります。
-
オーバーロード(3-6)を使用するには、TLessThanComparableの要件を満たす必要があります。順序関係は全順序を確立する必要があります。

[編集] 戻り値

1) dequeの内容が等しい場合はtrue、そうでない場合はfalse
2) dequeの内容が等しくない場合は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 のサイズが異なる場合は定数時間、そうでない場合はdequeのサイズに線形時間。
3-7) dequeのサイズに線形時間。

[編集] 備考

関係演算子は、要素型のoperator<を介して定義されます。

(C++20まで)

関係演算子はsynth-three-wayを介して定義されます。synth-three-wayは、可能であればoperator<=>を使用し、そうでなければoperator<を使用します。

特に、要素自身がoperator<=>を提供しないが、三方比較可能な型に暗黙的に変換できる場合、その変換がoperator<の代わりに使用されます。

(C++20以降)

[編集]

#include <cassert>
#include <compare>
#include <deque>
 
int main()
{
    const std::deque
        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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 3431 C++20 operator<=>Tを必要としませんでした
three_way_comparableのモデルとするために
要求するようになった
English 日本語 中文(简体) 中文(繁體)