Spec-Zone.ru › C++

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

Определено в заголовке <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::u8string, std::u16string, или std::u32string, возвращаемый тип operator<=> равен std::strong_ordering.

(с C++20)

Пример

Отчеты об ошибках

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

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

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API