Namespaces
Variants

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

From cppreference.net
std::basic_string
Constants
Non-member functions
I/O
Comparison
operator== operator!= operator< operator> operator<= operator>= operator<=>
(until C++20) (until C++20) (until C++20) (until C++20) (until C++20) (C++20)
Numeric conversions
(C++11) (C++11) (C++11)
(C++11) (C++11)
(C++11) (C++11) (C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)
Определено в заголовке <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) (noexcept начиная с C++11)
(constexpr начиная с C++20)
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)
(noexcept начиная с 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 ) ;
(3) (до C++20)
(noexcept начиная с 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 ) ;
(4) (до C++20)
(noexcept начиная с 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 ) ;
(5) (до C++20)
(noexcept начиная с 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 ) ;
(6) (до C++20)
(noexcept начиная с C++11)
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) (constexpr начиная с C++20)
template < class CharT, class Traits, class Alloc >

bool operator == ( const CharT * lhs,

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

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

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

bool operator ! = ( const CharT * lhs,

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

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

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

bool operator < ( const CharT * lhs,

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

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

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

bool operator <= ( const CharT * lhs,

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

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

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

bool operator > ( const CharT * lhs,

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

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

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

bool operator >= ( const CharT * lhs,

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

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

const CharT * rhs ) ;
(20) (начиная с C++20)

Сравнивает содержимое строки с другой строкой или нуль-терминированным массивом CharT .

Все сравнения выполняются с помощью compare() функции-члена (которая сама определена в терминах Traits::compare() ):

  • Две строки равны, если размеры lhs и rhs равны и каждый символ в lhs имеет эквивалентный символ в rhs на той же позиции.
1-7) Сравнивает два объекта basic_string .
8-20) Сравнивает объект basic_string и null-terminated массив 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 .

(since C++20)

Пример

Отчеты о дефектах

Следующие отчеты об изменениях поведения, влияющие на дефекты, были применены ретроактивно к ранее опубликованным стандартам C++.

DR Применяется к Поведение в опубликованной версии Корректное поведение
LWG 2064 C++11 было противоречие в том, являются ли noexcept перегрузки, принимающие два basic_string ;
перегрузки, принимающие CharT* были noexcept, но могли вызывать неопределённое поведение
сделано согласованным;
noexcept удалён
LWG 3432 C++20 тип возвращаемого значения operator<=> не требовался быть типом категории сравнения требуется