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
/*比较类别*/
|
(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
>
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
/*比较类别*/
|
(20) | (自 C++20 起) |
比较字符串内容与另一个字符串或一个以空字符结尾的
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++ 标准。
| DR | 适用版本 | 发布行为 | 正确行为 |
|---|---|---|---|
| LWG 2064 | C++11 |
接收两个
basic_string
参数的重载是否标记为noexcept存在不一致;
接收
CharT*
参数的重载标记为noexcept但可能引发未定义行为
|
保持一致性;
移除noexcept限定 |
| LWG 3432 | C++20 |
operator<=>
的返回类型未被要求必须是比较类别类型
|
要求必须是比较类别类型 |