Namespaces
Variants

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

Aus de.cppreference.net
 
 
 
std::basic_string
 
Definiert in Header <string>
Vergleiche zwei basic_string Objekte
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 seit C++11)
(constexpr seit 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) (bis C++20)
(noexcept seit 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) (bis C++20)
(noexcept seit 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) (bis C++20)
(noexcept seit 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) (bis C++20)
(noexcept seit 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) (bis C++20)
(noexcept seit 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) (seit C++20)
Vergleiche ein basic_string Objekt und ein nullterminiertes Array von T
template< class CharT, class Traits, class Alloc >
bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(8) (constexpr seit C++20)
template< class CharT, class Traits, class Alloc >
bool operator==( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(9) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(10) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator!=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(11) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(12) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator<( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(13) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(14) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator<=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(15) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
                const CharT* rhs );
(16) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator>( const CharT* lhs,
                const std::basic_string<CharT,Traits,Alloc>& rhs );
(17) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
                 const CharT* rhs );
(18) (bis C++20)
template< class CharT, class Traits, class Alloc >
bool operator>=( const CharT* lhs,
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(19) (bis 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) (seit C++20)

Vergleicht den Inhalt einer Zeichenkette mit einer anderen Zeichenkette oder einem nullterminierten Array von CharT.

Alle Vergleiche werden über die compare()-Mitgliedsfunktion durchgeführt (die selbst mittels Traits::compare() definiert ist):

  • Zwei Zeichenketten sind gleich, wenn sowohl die Größe von lhs als auch rhs gleich sind und jedes Zeichen in lhs ein entsprechendes Zeichen in rhs an derselben Position hat.
  • Die Ordnungsvergleiche werden lexikografisch durchgeführt – Der Vergleich wird durch eine Funktion durchgeführt, die äquivalent zu std::lexicographical_compareoder std::lexicographical_compare_three_way(seit C++20) ist.
1-7) Vergleicht zwei basic_string Objekte.
8-20) Vergleicht ein basic_string Objekt und ein nullterminiertes Array von CharT.

Der Rückgabetyp des Dreivei-Vergleichsoperators (/*comp-cat*/) ist Traits::comparison_category wenn diese qualifizierte-ID existiert und einen Typ bezeichnet, andernfalls std::weak_ordering. Wenn /*comp-cat*/ kein Vergleichskategorietyp ist, ist das Programm fehlerhaft.

Die Operatoren <, <=, >, >= und != werden aus bzw. operator<=>synthetisiertoperator==.

(seit C++20)

Parameter

lhs, rhs - Zeichenketten, deren Inhalte verglichen werden sollen

Rückgabewert

1-6,8-19) true wenn die entsprechende Vergleichsoperation zutrifft, false andernfalls.
7,20) static_cast < /*comp-cat*/ > ( lhs. compare ( rhs ) <=> 0 ) .

Komplexität

Linear in der Größe der Strings.

Hinweise

Wenn mindestens ein Parameter vom Typ std::string , std::wstring , std::u8string , std::u16string oder std::u32string ist, ist der Rückgabetyp von operator<=> std::strong_ordering .

(since C++20)

Beispiel

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
LWG 2064 C++11 ob Überladungen mit zwei basic_string s noexcept sind, war inkonsistent;
Überladungen mit einem CharT* waren noexcept, konnten aber UB auslösen
konsistent gemacht;
noexcept entfernt
LWG 3432 C++20 der Rückgabetyp von operator<=> musste kein Vergleichskategorietyp sein erforderlich gemacht