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) | (C++11 起无异常抛出) (C++20 起为 constexpr) |
template< class CharT, class Traits, class Alloc > bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(2) | (C++20 前) (C++11 起无异常抛出) |
template< class CharT, class Traits, class Alloc > bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(3) | (C++20 前) (C++11 起无异常抛出) |
template< class CharT, class Traits, class Alloc > bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(4) | (C++20 前) (C++11 起无异常抛出) |
template< class CharT, class Traits, class Alloc > bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(5) | (C++20 前) (C++11 起无异常抛出) |
template< class CharT, class Traits, class Alloc > bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(6) | (C++20 前) (C++11 起无异常抛出) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(7) | (C++20 起) |
比较 basic_string 对象与以 null 结尾的 T 数组 |
||
template< class CharT, class Traits, class Alloc > bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(8) | (C++20 起为 constexpr) |
template< class CharT, class Traits, class Alloc > bool operator==( const CharT* lhs, |
(9) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(10) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator!=( const CharT* lhs, |
(11) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(12) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<( const CharT* lhs, |
(13) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(14) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<=( const CharT* lhs, |
(15) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(16) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>( const CharT* lhs, |
(17) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(18) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>=( const CharT* lhs, |
(19) | (C++20 前) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(20) | (C++20 起) |
将字符串的内容与另一个字符串或以 null 结尾的 `CharT` 数组进行比较。
所有比较均通过 compare() 成员函数完成(该函数本身是根据 `Traits::compare()` 定义的)
- 如果 lhs 和 rhs 的大小相等,并且 lhs 中的每个字符在 rhs 中的相同位置都有等效字符,则两个字符串相等。
- 排序比较是按字典顺序进行的——比较由等效于 std::lexicographical_compare或 std::lexicographical_compare_three_way(从 C++20 开始) 的函数执行。
basic_string
对象。basic_string
对象和以 null 结尾的 CharT
数组。
三路比较运算符 (/*comp-cat*/) 的返回类型是 Traits::comparison_category(如果该限定 ID 存在并表示一种类型),否则为 std::weak_ordering。如果 /*comp-cat*/ 不是比较类别类型,则程序格式错误。
|
(C++20 起) |
目录 |
[编辑] 参数
lhs, rhs | - | 要比较内容的字符串 |
[编辑] 返回值
[编辑] 复杂度
与字符串大小呈线性关系。
[编辑] 注意
如果至少一个参数是 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<=> 的返回类型不要求是比较类别类型 |
需要 |