Namespaces
Variants

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

From cppreference.net
std::basic_string
Constants
Non-member functions
I/O
Comparison
operator== operator!= operator< operator> operator<= operator>= operator<=>
(until C++20) (until C++20) (until C++20) (until C++20) (until C++20) (C++20)
Numeric conversions
(C++11) (C++11) (C++11)
(C++11) (C++11)
(C++11) (C++11) (C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt gemäß den Anforderungen unverändert.)
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) (seit C++11 noexcept)
(seit C++20 constexpr)
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 eine nullterminierte Folge 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 eines Strings mit einem anderen String oder einer nullterminierten Folge von CharT .

Alle Vergleiche werden über die compare() Memberfunktion durchgeführt (die selbst definiert ist durch Traits::compare() ):

  • Zwei Zeichenketten sind gleich, wenn sowohl die Größe von lhs als auch von rhs gleich sind und jedes Zeichen in lhs ein entsprechendes Zeichen in rhs an derselben Position hat.
1-7) Vergleicht zwei basic_string -Objekte.
8-20) Vergleicht ein basic_string -Objekt und ein nullterminiertes Array von CharT .

Der Rückgabetyp der Drei-Wege-Vergleichsoperatoren ( /*comp-cat*/ ) ist Traits :: comparison_category , falls dieser qualifizierte Bezeichner existiert und einen Typ bezeichnet, andernfalls std::weak_ordering . Wenn /*comp-cat*/ kein Vergleichskategorietyp ist, ist das Programm fehlerhaft.

Die Operatoren < , <= , > , >= und != werden jeweils aus operator <=> und operator == synthetisiert .

(seit C++20)

Inhaltsverzeichnis

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