Пространства имён
Варианты
Действия

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

Материал из cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
Функции-элементы
Доступ к элементам
Итераторы
Ёмкость
Операции
Поиск
Константы
Правила вывода(C++17)
Функции, не являющиеся элементами
Ввод/Вывод
Сравнение
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==(conststd::basic_string<CharT,Traits,Alloc>& lhs,

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

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

                 conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
template<class CharT, class Traits, class Alloc >

constexprbool
    operator==(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++20)
(2)
template<class CharT, class Traits, class Alloc >

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

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

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

                 conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
(3)
template<class CharT, class Traits, class Alloc >

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

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

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

                conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
(4)
template<class CharT, class Traits, class Alloc >

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

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

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

                 conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
(5)
template<class CharT, class Traits, class Alloc >

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

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

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

                conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
(6)
template<class CharT, class Traits, class Alloc >

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

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

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

                 conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(начиная с C++11)
(до C++20)
template<class CharT, class Traits, class Alloc >

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

                 conststd::basic_string<CharT,Traits,Alloc>& rhs )noexcept;
(7) (начиная с C++20)
Сравнивает объект basic_string и массив T, завершающийся нулём
(8)
template<class CharT, class Traits, class Alloc >

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

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

constexprbool operator==(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                           const CharT* rhs );
(начиная с C++20)
template<class CharT, class Traits, class Alloc >

bool operator==(const CharT* lhs,

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

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

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

bool operator!=(const CharT* lhs,

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

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

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

bool operator<(const CharT* lhs,

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

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

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

bool operator<=(const CharT* lhs,

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

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

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

bool operator>(const CharT* lhs,

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

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

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

bool operator>=(const CharT* lhs,

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

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

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

Сравнивает содержимое строки с другой строкой или массивом CharT, завершающимся нулём.

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

  • Две строки равны, если размеры lhs и rhs равны и каждый символ в 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::string, std::wstring, std::u8string, std::u16string или std::u32string, тип возвращаемого значения operator<=> это std::strong_ordering.

(начиная с C++20)

[править]Пример

[править]Отчёты о дефектах

Следующие изменения поведения были применены с обратной силой к ранее опубликованным стандартам C++:

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