Namespaces
Variants

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

From cppreference.net

ヘッダーで定義 <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 /* 詳細は後述 */
operator <=> ( const std:: vector < T, Alloc > & lhs,

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

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

value_type vector の値型(すなわち、 typename vector :: 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 - vector の内容を比較する

戻り値

演算子 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 のサイズが異なる場合は定数時間、それ以外の場合は vector のサイズに対して線形時間。
3-7) vector のサイズに対して線形。

注記

関係演算子は value_type operator < によって定義されます。

(C++20まで)

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

operator <=> は可能な場合 value_type operator <=> を使用し、そうでない場合は value_type operator < を使用します。特に、 value_type 自体が 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
    (""
        "等しいコンテナの比較:" &&
        (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 &&
        "等しくないコンテナの比較:" &&
        (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 をモデル化することを要求していなかった
要求する