命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
常量
非成员函数
I/O
比较
operator==operator!=operator<operator>operator<=operator>=operator<=>
(until C++20)(until C++20)(until C++20)(until C++20)(until 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>
比较两个 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 起无异常抛出)
(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 起无异常抛出)
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 起无异常抛出)
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 起无异常抛出)
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 起无异常抛出)
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 起无异常抛出)
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 对象与以 null 结尾的 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 起)

将字符串的内容与另一个字符串或以 null 结尾的 `CharT` 数组进行比较。

所有比较均通过 compare() 成员函数完成(该函数本身是根据 `Traits::compare()` 定义的)

  • 如果 lhsrhs 的大小相等,并且 lhs 中的每个字符在 rhs 中的相同位置都有等效字符,则两个字符串相等。
1-7) 比较两个 basic_string 对象。
8-20) 比较 basic_string 对象和以 null 结尾的 CharT 数组。

三路比较运算符 (/*comp-cat*/) 的返回类型是 Traits::comparison_category(如果该限定 ID 存在并表示一种类型),否则为 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::stringstd::wstringstd::u8stringstd::u16stringstd::u32string 类型,则 operator<=> 的返回类型是 std::strong_ordering

(C++20 起)

[编辑] 示例

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2064 C++11 接受两个 basic_string 的重载是否为 noexcept 不一致;
接受 CharT* 的重载为 noexcept 但可能引发 UB
已使其一致;
noexcept 已移除
LWG 3432 C++20 operator<=> 的返回类型不要求是比较类别类型 需要