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

< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
非成员函数
I/O
比较
operator==operator!=operator<operator>operator<=operator>=operator<=>
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
数值转换 (C++11)
辅助类
推导指引(C++17)
 
定义于头文件 <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++14 前)
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++14 起)
(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++14 前)
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++14 起)
(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++14 前)
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++14 起)
(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++14 前)
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++14 起)
(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++14 前)
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++14 起)
(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++14 前)
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++14 起)
(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 起)的函数进行比较。

三路比较运算符的返回类型( /*comp-cat*/ )为 Traits::comparison_category ,若它存在,否则为 std::weak_ordering

(C++20 起)
1-7) 比较二个 basic_string 对象。
8-14) 比较 basic_string 对象和 CharT 的空终止数组。

参数

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 起)