operator==,!=,<,<=,>,>=,<=>(std::basic_string)
Определён в заголовочном файле <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, находящемуся на той же позиции. - Операторы сравнения порядка выполняют лексикографическое сравнение — сравнение осуществляется функцией, эквивалентной
std::lexicographical_compareилиstd::lexicographical_compare_three_way(начиная с C++20).
basic_string.basic_string и массив с нулевым символом-терминатором из CharT.| Возвращаемый тип операторов трёхстороннего сравнения ( Операторы | (начиная с C++20) |
Параметры
| lhs, rhs | - | строки, содержимое которых сравнивается |
Возвращаемое значение
true, если соответствующее условие сравнения выполняется, и false в противном случае.static_cast</*comp-cat*/>(lhs.compare(rhs) <=> 0).Сложность
Линейная относительно размера строк.
Примечания
| Если хотя бы один параметр имеет тип | (начиная с C++20) |
Пример
Отчёты об ошибках
Следующие отчёты об ошибках, изменяющие поведение, были применены задним числом к ранее опубликованным стандартам C++.
| DR | Применяется к | Поведение согласно опубликованному стандарту | Корректное поведение |
|---|---|---|---|
| LWG 2064 | C++11 | не было согласованности в том, являются ли перегрузки, принимающие два объекта basic_string, noexcept;перегрузки, принимающие CharT*, были noexcept, но могли приводить к неопределённому поведению (UB) | приведено к согласованному виду; noexcept удалён |
| LWG 3432 | C++20 | не требовалось, чтобы возвращаемый тип operator<=> был типом категории сравнения | теперь требуется |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/string/basic_string/operator_cmp