operator==,!=,<,<=,>,>=,<=>(std::basic_string)
来自cppreference.com
| 在标头 <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 > 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 起) |
比较字符串与另一字符串或 CharT 的空终止数组的内容。
所有比较通过 compare() 成员函数进行(它自身以 Traits::compare() 定义):
- 若
lhs与rhs的大小相等,且lhs中的每个字符有在rhs中在同一位置的等价字符,则两个字符串相等。
- 按字典序进行顺序比较——以等价于 std::lexicographical_compare 或 std::lexicographical_compare_three_way (C++20 起)的函数进行比较。
1-7) 比较两个
basic_string 对象。8-20) 比较
basic_string 对象和 CharT 的空终止数组。|
三路比较运算符的返回类型(
|
(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,则 |
(C++20 起) |
示例
| 本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2064 | C++11 | 接收二个 basic_string 的重载是否为 noexcept 不一致;接收一个 CharT* 的重载为 noexcept 但可能引发 UB
|
使之一致; 移除 noexcept |
| LWG 3432 | C++20 | 未要求 operator<=> 的返回类型为比较类别类型
|
已要求 |