operator==,!=,<,<=,>,>=,<=>(std::basic_string)
<string>
basic_string
objectsbool operator==( const std::basic_string <CharT,Traits,Alloc>& lhs,
(constexpr since C++20)
bool operator!=( const std::basic_string <CharT,Traits,Alloc>& lhs,
(noexcept since C++11)
bool operator<( const std::basic_string <CharT,Traits,Alloc>& lhs,
(noexcept since C++11)
bool operator<=( const std::basic_string <CharT,Traits,Alloc>& lhs,
(noexcept since C++11)
bool operator>( const std::basic_string <CharT,Traits,Alloc>& lhs,
(noexcept since C++11)
bool operator>=( const std::basic_string <CharT,Traits,Alloc>& lhs,
(noexcept since C++11)
constexpr /*comp-cat*/
operator<=>( const std::basic_string <CharT,Traits,Alloc>& lhs,
basic_string
object and null-terminated array of T
bool operator==( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator==( const CharT* lhs,
bool operator!=( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator!=( const CharT* lhs,
bool operator<( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator<( const CharT* lhs,
bool operator<=( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator<=( const CharT* lhs,
bool operator>( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator>( const CharT* lhs,
bool operator>=( const std::basic_string <CharT,Traits,Alloc>& lhs,
bool operator>=( const CharT* lhs,
constexpr /*comp-cat*/
operator<=>( const std::basic_string <CharT,Traits,Alloc>& lhs,
Compares the contents of a string with another string or a null-terminated array of CharT
.
All comparisons are done via the compare() member function (which itself is defined in terms of Traits::compare()
):
- Two strings are equal if both the size of lhs and rhs are equal and each character in lhs has equivalent character in rhs at the same position.
- The ordering comparisons are done lexicographically – the comparison is performed by a function equivalent to std::lexicographical_compare or std::lexicographical_compare_three_way (since C++20).
basic_string
objects.basic_string
object and a null-terminated array of CharT
.The return type of three-way comparison operators (/*comp-cat*/) is Traits::comparison_category if that qualified-id exists and denotes a type, std::weak_ordering otherwise. If /*comp-cat*/ is not a comparison category type, the program is ill-formed.
The <
, <=
, >
, >=
, and !=
operators are synthesized from operator<=> and operator== respectively.
[edit] Parameters
[edit] Return value
[edit] Complexity
Linear in the size of the strings.
[edit] Notes
If at least one parameter is of type std::string , std::wstring , std::u8string , std::u16string , or std::u32string , the return type of operator<=>
is std::strong_ordering.
[edit] Example
Reason: no example
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2064 | C++11 | whether overloads taking two basic_string s are noexcept was inconsistent;overloads taking a CharT* were noexcept but might raise UB
|
made consistent; noexcept removed |
LWG 3432 | C++20 | the return type of operator<=> was not required to be a comparison category type
|
required |