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)
定义于头文件 <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 /*比较类别*/
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 /*比较类别*/
operator <=> ( const std:: basic_string < CharT,Traits,Alloc > & lhs,

const CharT * rhs ) ;
(20) (自 C++20 起)

比较字符串内容与另一个字符串或一个以空字符结尾的 CharT 数组。

所有比较均通过 compare() 成员函数执行(该函数本身通过 Traits::compare() 定义):

  • 两个字符串相等当且仅当 lhs rhs 的大小相同,且 lhs 中的每个字符都与 rhs 相同位置上的字符对应相等。
1-7) 比较两个 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 ) .

复杂度

与字符串大小呈线性关系。

注释

若至少有一个参数的类型为 std::string std::wstring std::u8string std::u16string std::u32string ,则 operator<=> 的返回类型为 std::strong_ordering

(C++20 起)

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 适用版本 发布行为 正确行为
LWG 2064 C++11 接收两个 basic_string 参数的重载是否标记为noexcept存在不一致;
接收 CharT* 参数的重载标记为noexcept但可能引发未定义行为
保持一致性;
移除noexcept限定
LWG 3432 C++20 operator<=> 的返回类型未被要求必须是比较类别类型 要求必须是比较类别类型