basic_string_operator_cmp
basic_string_operator_cmp (basic_string 비교 연산자)
이 페이지는 std::basic_string의 비교 연산자들에 대해 설명해요. 두 문자열의 내용을 비교하거나, 문자열과 널 종료 문자 배열(CharT*)을 비교하는 방법을 다루고 있어요. C++20부터는 삼중 비교 연산자(<=>)도 제공되어 더 간결하게 비교할 수 있답니다.
출처: cppreference
본문
개요
<string> 헤더에 정의된 비교 연산자들은 basic_string 객체의 내용을 다른 문자열이나 CharT의 널 종료 배열과 비교해요. 모든 비교는 compare() 멤버 함수를 통해 수행되며, 이 함수는 내부적으로 Traits::compare()를 사용해요.
- 두 문자열이 같으려면
lhs와rhs의 크기가 같고, 같은 위치의 각 문자가 서로 동일해야 해요. - 순서 비교는 사전식(lexicographical)으로 이루어져요.
std::lexicographical_compare또는 C++20부터는std::lexicographical_compare_three_way와 동등한 방식으로 비교해요.
오버로드 목록
두 basic_string 객체 비교 (1~7)
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 );
noexceptsince C++11,constexprsince 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 );
- until C++20,
noexceptsince 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 );
- until C++20,
noexceptsince 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 );
- until C++20,
noexceptsince 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 );
- until C++20,
noexceptsince 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 );
- until C++20,
noexceptsince 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 ;
- since C++20
basic_string 객체와 CharT 널 종료 배열 비교 (8~20)
template < class CharT , class Traits , class Alloc > bool operator == ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
constexprsince C++20
template < class CharT , class Traits , class Alloc > bool operator == ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator != ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator != ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator < ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator < ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator <= ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator <= ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator > ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator > ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator >= ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- until C++20
template < class CharT , class Traits , class Alloc > bool operator >= ( const CharT * lhs , const std :: basic_string < CharT , Traits , Alloc >& rhs );
- until C++20
template < class CharT , class Traits , class Alloc > constexpr /*comp-cat*/ operator <=> ( const std :: basic_string < CharT , Traits , Alloc >& lhs , const CharT * rhs );
- since C++20
삼중 비교 연산자의 반환 타입
C++20부터 삼중 비교 연산자(<=>)의 반환 타입(/*comp-cat*/)은 다음과 같이 결정돼요. Traits::comparison_category가 존재하고 타입을 나타내면 그 타입을 사용하고, 그렇지 않으면 std::weak_ordering을 사용해요. 만약 /*comp-cat*/이 비교 카테고리 타입이 아니라면 프로그램은 ill-formed예요. <, <=, >, >=, != 연산자는 각각 <=>와 ==로부터 합성돼요.
매개변수
| lhs, rhs | - | 내용을 비교할 문자열 |
|---|
반환값
비교 결과에 따라 bool 값을 반환하거나, C++20의 <=> 연산자에서는 비교 카테고리 타입을 반환해요.
복잡도
문자열의 크기에 선형이에요.
참고 사항
C++20부터, 매개변수 중 하나라도 std::string, std::wstring, std::u8string, std::u16string, std::u32string 타입이라면 operator<=>의 반환 타입은 std::strong_ordering이에요.
예제
이 섹션은 아직 작성되지 않았어요. (Reason: no example)
결함 보고서
다음의 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | 적용 대상 | 발표된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2064 | C++11 | 두 basic_string을 받는 오버로드의 noexcept 여부가 일관되지 않았고, CharT*를 받는 오버로드는 noexcept였지만 UB를 일으킬 수 있었어요. |
일관되게 수정하고 noexcept를 제거했어요. |
| LWG 3432 | C++20 | operator<=>의 반환 타입이 비교 카테고리 타입일 필요가 없었어요. |
비교 카테고리 타입을 요구하도록 했어요. |