命名空间
变体
操作

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) (noexcept since C++11)
(constexpr since C++20)
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) (until C++20)
(noexcept since 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) (until C++20)
(noexcept since 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) (until C++20)
(noexcept since 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) (until C++20)
(noexcept since 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) (until C++20)
(noexcept since 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) (since 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) (until C++20)
template< class CharT, class Traits, class Alloc >

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

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

bool operator!=( const CharT* lhs,

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

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

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

bool operator<( const CharT* lhs,

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

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

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

bool operator<=( const CharT* lhs,

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

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

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

bool operator>( const CharT* lhs,

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

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

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

bool operator>=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(19) (until 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) (since C++20)

将字符串的内容与另一个字符串或空终止 CharT 数组进行比较。

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

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

三路比较运算符(/*comp-cat*/)的返回类型是 Traits::comparison_category(如果该限定标识符存在并表示类型),否则为 std::weak_ordering。如果 /*comp-cat*/ 不是比较类别类型,则程序是非良构的。

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(since 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

(since C++20)

[编辑] 示例

[编辑] 缺陷报告

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

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