名前空間
変種
操作

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

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
定数
非メンバ関数
I/O
比較
operator==operator!=operator<operator>operator<=operator>=operator<=>
(C++20まで)(C++20まで)(C++20まで)(C++20まで)(C++20まで)(C++20)
数値変換
(C++11)(C++11)(C++11)
(C++11)(C++11) 
(C++11)(C++11)(C++11)
(C++11)
(C++11)
リテラル
ヘルパークラス
推論ガイド (C++17)
 
ヘッダ <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) (C++20 以降 constexpr)
template< class CharT, class Traits, class Alloc >

bool operator==( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(9) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(10) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator!=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(11) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(12) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator<( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(13) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(14) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator<=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(15) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(16) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator>( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(17) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(18) (C++20まで)
template< class CharT, class Traits, class Alloc >

bool operator>=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(19) (C++20まで)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(20) (C++20以降)

文字列の内容を別の文字列またはヌル終端CharT配列と比較します。

すべての比較はcompare()メンバー関数を介して行われます(これはTraits::compare()で定義されています)。

  • 2つの文字列が等しいとは、lhsrhsのサイズが等しく、lhsの各文字がrhsの同じ位置にある同等の文字を持っている場合です。
1-7) 2つのbasic_stringオブジェクトを比較します。
8-20) basic_stringオブジェクトとヌル終端CharT配列を比較します。

3方向比較演算子(/*comp-cat*/)の戻り値の型は、その修飾IDが存在し、型を指す場合はTraits::comparison_category、それ以外の場合はstd::weak_orderingです。/*comp-cat*/が比較カテゴリ型でない場合、プログラムは不正形式になります。

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

(C++20以降)

目次

[編集] パラメータ

lhs, rhs - 比較する文字列

[編集] 戻り値

1-6,8-19) 対応する比較が成り立つ場合はtrue、そうでない場合はfalse
7,20) static_cast</*comp-cat*/>(lhs.compare(rhs) <=> 0)

[編集] 計算量

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

[編集] 注記

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

(C++20以降)

[編集]

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2064 C++11 2つのbasic_stringを取るオーバーロードがnoexceptであったかどうかは一貫性がありませんでした。
CharT*を取るオーバーロードはnoexceptでしたが、UBを引き起こす可能性がありました。
一貫性を持たせました。
noexcept 削除
LWG 3432 C++20 operator<=>の戻り値の型は、比較カテゴリ型である必要はありませんでした。 必要
English 日本語 中文(简体) 中文(繁體)