Namespaces
Variants

operator==,!=,<,<=,>,>=,<=>(std::basic_string)

来自 zh.cppreference.net
 
 
 
std::basic_string
 
定义于头文件 <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)
template< class CharT, class Traits, class Alloc >
bool operator==( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 起为 constexpr) (9)
template< class CharT, class Traits, class Alloc >
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(直至 C++20) (10)
template< class CharT, class Traits, class Alloc >
bool operator!=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(直至 C++20) (11)
template< class CharT, class Traits, class Alloc >
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(直至 C++20) (12)
template< class CharT, class Traits, class Alloc >
bool operator<( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(直至 C++20) (13)
template< class CharT, class Traits, class Alloc >
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(直至 C++20) (14)
template< class CharT, class Traits, class Alloc >
bool operator<=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(直至 C++20) (15)
template< class CharT, class Traits, class Alloc >
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(直至 C++20) (16)
template< class CharT, class Traits, class Alloc >
bool operator>( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(直至 C++20) (17)
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(直至 C++20) (18)
template< class CharT, class Traits, class Alloc >
bool operator>=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(直至 C++20) (19)
template< class CharT, class Traits, class Alloc >
constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(直至 C++20) (20)

(C++20 起)CharT将 string 的内容与另一个 string 或空终止的

数组进行比较。 所有比较均通过 compare()Traits::compare() 成员函数执行(该函数本身依据

  • 定义): lhs若两个 string 的 rhs 长度相等,且 lhs 中的每个字符在 rhs 的相同位置都有相等字符,则它们相等。
  • 比较操作按字典序进行 –——比较由等价于 std::lexicographical_compare 的函数执行,或等价于 std::lexicographical_compare_three_way(C++20 起)。
1-7) 比较两个 basic_string 对象。
8-20) 比较一个 basic_string 对象和一个以空字符结尾的 CharT 数组。

三路比较运算符的返回类型(/*comp-cat*/)是:若该限定标识存在且表示一个类型,则为 Traits::comparison_category,否则为 std::weak_ordering。若 /*comp-cat*/ 不是比较类别类型,则程序非良构。

运算符 <、<=、>、>= 和 != 分别从 和 经由operator<=>合成operator==生成。

(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 ,则 operator<=> 的返回类型为 std::strong_ordering 。

(C++20 起)

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 适用版本 发布行为 正确行为
LWG 2064 C++11 接收两个 basic_string 参数的重载是否标记为noexcept存在不一致;
接收 CharT* 参数的重载标记为noexcept但可能引发未定义行为
保持一致性;
移除noexcept限定
LWG 3432 C++20 operator<=> 的返回类型未被要求必须是比较类别类型 要求必须是比较类别类型