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 /*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 ></

将字符串的内容与另一个字符串或 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++标准。

缺陷报告 适用范围 发布时的行为 正确行为
LWG 2064 C++11 接受两个 basic_string 参数的重载函数是否标记为noexcept存在不一致性;
接受 CharT* 参数的重载虽标记为noexcept但可能引发未定义行为
实现一致性;
移除noexcept限定
LWG 3432 C++20 operator<=> 的返回类型未被要求必须是比较类别类型 要求必须是比较类别类型