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 起为 noexcept)
(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 起为 noexcept) |
|
template
<
class
CharT,
class
Traits,
class
Alloc
>
bool
operator
<
(
const
std::
basic_string
<
CharT,Traits,Alloc
>
&
lhs,
|
(3) |
(C++20 前)
(C++11 起为 noexcept) |
|
template
<
class
CharT,
class
Traits,
class
Alloc
>
bool
operator
<=
(
const
std::
basic_string
<
CharT,Traits,Alloc
>
&
lhs,
|
(4) |
(C++20 前)
(C++11 起为 noexcept) |
|
template
<
class
CharT,
class
Traits,
class
Alloc
>
bool
operator
>
(
const
std::
basic_string
<
CharT,Traits,Alloc
>
&
lhs,
|
(5) |
(C++20 前)
(C++11 起为 noexcept) |
|
template
<
class
CharT,
class
Traits,
class
Alloc
>
bool
operator
>=
(
const
std::
basic_string
<
CharT,Traits,Alloc
>
&
lhs,
|
(6) |
(C++20 前)
(C++11 起为 noexcept) |
|
template
<
class
CharT,
class
Traits,
class
Alloc
>
constexpr
/*comp-cat*/
|
(7) | (C++20 起) |
|
比较
basic_string
对象与
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
></
|
将字符串的内容与另一个字符串或
CharT
类型的空终止数组进行比较。
所有比较均通过
compare()
成员函数执行(该函数本身通过
Traits::compare()
定义):
- 两个字符串相等当且仅当 lhs 与 rhs 的大小相等,且 lhs 中的每个字符在相同位置都与 rhs 中的对应字符等效。
- 排序比较采用字典序方式执行——通过一个功能等同于 std::lexicographical_compare 或 std::lexicographical_compare_three_way (C++20 起) 的函数完成比较操作。
basic_string
对象。
basic_string
对象和一个以空字符结尾的
CharT
数组。
|
三路比较运算符( /*comp-cat*/ )的返回类型是:若存在限定标识符 Traits :: comparison_category 且其表示一个类型,则为该类型;否则为 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但可能引发未定义行为
|
实现一致性;
移除noexcept限定 |
| LWG 3432 | C++20 |
operator<=>
的返回类型未被要求必须是比较类别类型
|
要求必须是比较类别类型 |