Namespaces
Variants

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

From cppreference.net
std::basic_string
Constants
Non-member functions
I/O
Comparison
operator== operator!= operator< operator> operator<= operator>= operator<=>
(until C++20) (until C++20) (until C++20) (until C++20) (until C++20) (C++20)
Numeric conversions
(C++11) (C++11) (C++11)
(C++11) (C++11)
(C++11) (C++11) (C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)
HTMLタグ、属性、コードブロック内のテキストは翻訳せず、元のフォーマットを保持しました。C++固有の用語も翻訳していません。唯一翻訳したのは「(until C++20)」を「(C++20まで)」に変更した部分のみです。
ヘッダーで定義 <string>
二つの 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) (constexpr since C++20)
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以降)

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

すべての比較は compare() メンバ関数(これはそれ自体が Traits::compare() で定義されています)を通じて行われます:

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

三方比較演算子( /*comp-cat*/ )の戻り値の型は、 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::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<=> の戻り値型が比較カテゴリ型であることが要求されていなかった 要求するように変更