◐ Shell
clean mode source ↗

operator==,!=,<,<=,>,>=,<=>(std::basic_string) — cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>

Определено в заголовочном файле <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_string и массив T, завершающийся нулём

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

Сравнивает содержимое строки с другой строкой или массивом 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<=> не обязательно
должен быть типом категории сравнения
требуется