Namespaces
Variants

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

From cppreference.net

**翻訳結果:** **翻訳の説明:** - HTMLタグ、属性、C++コード(` `内)は一切翻訳せず保持 - 数値の"(1)"はそのまま保持 - "(constexpr since C++26)"のみを「(constexpr C++26以降)」と翻訳 - C++専門用語(template, class, bool, operator, const, std::listなど)は翻訳せず保持 (注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <list>
template < class T, class Alloc >

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

const std:: list < T, Alloc > & rhs ) ;
(1) (constexpr since C++26)
template < class T, class Alloc >

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

const std:: list < T, Alloc > & rhs ) ;
(1) (constexpr C++26以降)
template < class T, class Alloc >

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

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

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

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

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

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

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

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

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

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

/* 下記参照 */
operator <=> ( const std:: list < T, Alloc > & lhs,

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

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

list の値型(すなわち、 typename list :: value_type )を value_type とします:

1,2) lhs rhs の内容が等しいかどうか、つまり同じ数の要素を持ち、 lhs の各要素が rhs の同じ位置にある要素と等しいかどうかをチェックします。
次と同等:

return std:: distance ( lhs. begin ( ) , lhs. end ( ) )
== std:: distance ( rhs. begin ( ) , rhs. end ( ) )
&& std:: equal ( lhs. begin ( ) , lhs. end ( ) , rhs. begin ( ) ) ;

(C++14まで)

return std:: equal ( lhs. begin ( ) , lhs. end ( ) , rhs. begin ( ) , rhs. end ( ) ) ;

(C++14以降)
value_type EqualityComparable でない場合、動作は未定義です。
3-7) lhs rhs の内容を辞書順で比較します。
3-6) 次と等価: return std:: lexicographical_compare ( lhs. begin ( ) , lhs. end ( ) ,
rhs. begin ( ) , rhs. end ( ) ) ;
以下のいずれかの条件が満たされる場合、動作は未定義です:
7) 次と等価: return std:: lexicographical_compare_three_way ( lhs. begin ( ) , lhs. end ( ) ,
rhs. begin ( ) , rhs. end ( ) ,
synth-three-way )
.
戻り値の型は synth-three-way の戻り値の型(すなわち、 synth-three-way-result  < value_type > )です。
以下のいずれかの条件が満たされる場合、動作は未定義です:
  • T three_way_comparable をモデル化していない。
  • operator < が(const修飾された可能性のある) value_type 型の値に対して定義されていない。
  • operator < 全順序 を確立しない。

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

(C++20以降)

目次

パラメータ

lhs, rhs - list を比較する内容を持つリスト

戻り値

演算子 lhs rhs
が等しい場合
lhs
辞書順で大きい場合
rhs
辞書順で大きい場合
operator == true false
operator ! = false true
operator < false false true
operator <= true
operator > false true false
operator >= true
operator <=> 0 に等しい値 0 より大きい値 0 より小さい値

計算量

1,2) lhs rhs のサイズが異なる場合は定数時間、それ以外の場合は list のサイズに対して線形時間。
3-7) リストのサイズに対して線形。

注記

関係演算子は value_type operator < を用いて定義されます。

(C++20まで)

関係演算子は定義されません。書き換え候補である operator <=> がオーバーロード解決によって選択されます。

operator <=> は可能な場合 value_type operator <=> を使用し、そうでない場合は value_type operator < を使用します。特に、 value_type 自体が operator <=> を提供していないが、三方比較可能な型に暗黙変換可能な場合、その変換が operator < の代わりに使用されます。

(C++20以降)

#include <cassert>
#include <compare>
#include <list>
int main()
{
    const std::list
        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 をモデル化することを要求していなかった
要求する