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

来自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
成员函数
元素访问
迭代器
容量
操作
搜索
常量
推导指引 (C++17)
非成员函数
I/O
比较
operator==operator!=operator<operator>operator<=operator>=operator<=>
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
数值转换
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
辅助类
 
定义于头文件 <string>
比较二个 basic_string 对象
(1)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
template< class CharT, class Traits, class Alloc >

constexpr bool
    operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(C++20 起)
(2)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
(3)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
(4)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
(5)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
(6)
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 );
(C++11 前)
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 ) noexcept;
(C++11 起)
(C++20 前)
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_stringT 的空终止数组
(8)
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(C++20 前)
template< class CharT, class Traits, class Alloc >

constexpr bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                           const CharT* rhs );
(C++20 起)
template< class CharT, class Traits, class Alloc >

bool operator==( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 前)
(9)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator!=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 前)
(10)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(C++20 前)
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 前)
template< class CharT, class Traits, class Alloc >

bool operator<=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 前)
(12)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(C++20 前)
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 前)
template< class CharT, class Traits, class Alloc >

bool operator>=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20 前)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(14) (C++20 起)

比较 string 与另一 string 或 CharT 的空终止数组的内容。

所有比较通过 compare() 成员函数进行(它自身以 Traits::compare() 定义):

  • lhsrhs 的大小相等,且 lhs 中的每个字符有在 rhs 中在同一位置的等价字符,则二个字符串相等。
  • 顺序比较按字典序——以等价于 std::lexicographical_compare std::lexicographical_compare_three_way (C++20 起)的函数进行比较。
1-7) 比较二个 basic_string 对象。
8-14) 比较 basic_string 对象和 CharT 的空终止数组。

三路比较运算符的返回类型( /*comp-cat*/ )为 Traits::comparison_category ,若该有限定标识合法并指代类型,否则为 std::weak_ordering 。若 /*comp-cat*/ 不是比较类别类型,则程序非良构。

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(C++20 起)

参数

lhs, rhs - 要比较内容的字符串

返回值

1-6,8-13) 若对应比较关系成立则为 true ,否则为 false
7,14) static_cast</*comp-cat*/>(lhs.compare(rhs) <=> 0)

复杂度

与字符串大小成线性。

注解

若至少一个参数拥有类型 std::stringstd::wstringstd::u8stringstd::u16stringstd::u32string ,则 operator<=> 的返回类型是 std::strong_ordering

(C++20 起)

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
LWG 2064 C++11 接收二个 basic_string 的重载是否为 noexcept 不一致;
接收一个 CharT* 的重载为 noexcept 但可能引发 UB
使之一致;
移除 noexcept
LWG 3432 C++20 未要求 operator<=> 的返回类型为比较类别类型 已要求