operator==,!=,<,<=,>,>=,<=>(std::basic_string)
定义于头文件 <string> |
||
比较两个 basic_string 对象 |
||
template< class CharT, class Traits, class Alloc > bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(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, |
(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, |
(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, |
(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, |
(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, |
(6) | (until C++20) (noexcept since C++11) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(7) | (since C++20) |
比较 basic_string 对象和空终止 T 数组 |
||
template< class CharT, class Traits, class Alloc > bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(8) | (constexpr since C++20) |
template< class CharT, class Traits, class Alloc > bool operator==( const CharT* lhs, |
(9) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(10) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator!=( const CharT* lhs, |
(11) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(12) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator<( const CharT* lhs, |
(13) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(14) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator<=( const CharT* lhs, |
(15) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(16) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator>( const CharT* lhs, |
(17) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(18) | (until C++20) |
template< class CharT, class Traits, class Alloc > bool operator>=( const CharT* lhs, |
(19) | (until C++20) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(20) | (since C++20) |
将字符串的内容与另一个字符串或空终止 CharT
数组进行比较。
所有比较都通过 compare() 成员函数完成(该函数本身根据 Traits::compare()
定义)
- 如果 lhs 和 rhs 的大小相等,并且 lhs 中的每个字符在 rhs 中相同位置都有等效字符,则两个字符串相等。
- 排序比较按字典顺序完成——比较由等效于 std::lexicographical_compare或 std::lexicographical_compare_three_way(since C++20) 的函数执行。
basic_string
对象。basic_string
对象和一个空终止 CharT
数组。
三路比较运算符(/*comp-cat*/)的返回类型是 Traits::comparison_category(如果该限定标识符存在并表示类型),否则为 std::weak_ordering。如果 /*comp-cat*/ 不是比较类别类型,则程序是非良构的。
|
(since C++20) |
内容 |
[编辑] 参数
lhs, rhs | - | 要比较内容的字符串 |
[编辑] 返回值
[编辑] 复杂度
与字符串的大小呈线性关系。
[编辑] 注解
如果至少一个参数的类型为 std::string、 std::wstring、 std::u8string、 std::u16string 或 std::u32string,则 |
(since C++20) |
[编辑] 示例
本节尚不完整 原因:没有示例 |
[编辑] 缺陷报告
以下行为更改缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2064 | C++11 | 接受两个 basic_string 的重载是否为 noexcept 不一致;接受 CharT* 的重载是 noexcept 的,但可能引发 UB |
变得一致; noexcept 已移除 |
LWG 3432 | C++20 | operator<=> 的返回类型不需要是比较类别类型 |
已要求 |