Namespaces
Variants

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

ja.cppreference.net より
 
 
 
std::basic_string
 
ヘッダ <string>
で定義 2 つの basic_string オブジェクトを比較する
template< class CharT, class Traits, class Alloc >
bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(1) (C++11 から noexcept)
(C++20 から constexpr)
template< class CharT, class Traits, class Alloc >
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(2) (C++20 まで)
(C++11 から noexcept)
template< class CharT, class Traits, class Alloc >
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(3) (C++20 まで)
(C++11 から noexcept)
template< class CharT, class Traits, class Alloc >
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(4) (C++20 まで)
(C++11 から noexcept)
template< class CharT, class Traits, class Alloc >
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(5) (C++20 まで)
(C++11 から noexcept)
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(6) (C++20 まで)
(C++11 から noexcept)
template< class CharT, class Traits, class Alloc >
constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(7) (C++20 から)
basic_string オブジェクトと T
template< class CharT, class Traits, class Alloc >
bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
のヌル終端配列を比較する (8)
template< class CharT, class Traits, class Alloc >
bool operator==( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 から constexpr) (9)
template< class CharT, class Traits, class Alloc >
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(C++20 まで) (10)
template< class CharT, class Traits, class Alloc >
bool operator!=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 まで) (11)
template< class CharT, class Traits, class Alloc >
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(C++20 まで) (12)
template< class CharT, class Traits, class Alloc >
bool operator<( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 まで) (13)
template< class CharT, class Traits, class Alloc >
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(C++20 まで) (14)
template< class CharT, class Traits, class Alloc >
bool operator<=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 まで) (15)
template< class CharT, class Traits, class Alloc >
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(C++20 まで) (16)
template< class CharT, class Traits, class Alloc >
bool operator>( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 まで) (17)
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(C++20 まで) (18)
template< class CharT, class Traits, class Alloc >
bool operator>=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 まで) (19)
template< class CharT, class Traits, class Alloc >
constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(C++20 まで) (20)

(C++20 から)CharT文字列の内容を、別の文字列または

のヌル終端配列と比較します。 すべての比較は compare()Traits::compare() メンバ関数(それ自体が

  • に基づいて定義されます)を介して行われます。 lhs2 つの文字列は、rhs と lhs のサイズが等しく、rhs 内の各文字が同じ位置に
  • 内の同等の文字を持つ場合に等しくなります。 順序比較は辞書順に行われます–。比較は std::lexicographical_compare または std::lexicographical_compare_three_way(C++20 から) と同等の関数によって実行されます。
1-7) 2 つの basic_string オブジェクトを比較します。
8-20) basic_string オブジェクトと CharT のヌル終端配列を比較します。

三方比較演算子 (/*comp-cat*/) の戻り値の型は、その qualified-id が存在し型を示す場合は Traits::comparison_category、それ以外の場合は std::weak_ordering です。/*comp-cat*/ が比較カテゴリ型でない場合、プログラムは ill-formed です。

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

(C++20 から)

パラメータ

lhs, rhs - 内容を比較する文字列

戻り値

1-6,8-19) true 対応する比較が成立する場合は、 false それ以外の場合。
7,20) static_cast < /*comp-cat*/ > ( lhs. compare ( rhs ) <=> 0 ) .

計算量

文字列のサイズに対して線形。

注記

少なくとも1つのパラメータが型 std::string , std::wstring , std::u8string , std::u16string , または std::u32string である場合、 operator<=> の戻り値の型は std::strong_ordering となります。

(C++20以降)

例

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2064 C++11 2つのbasic_stringを取るオーバーロードがnoexceptかどうかが一貫していなかった;
CharT*を取るオーバーロードはnoexceptだが未定義動作を引き起こす可能性があった
一貫性を持たせた;
noexceptを削除
LWG 3432 C++20 operator<=>の戻り値型が比較カテゴリ型であることが要求されていなかった 要求するように変更