operator==,!=,<,<=,>,>=,<=>(std::pair)
| Definiert in Header <utility>
|
||
template< class T1, class T2, class U1, class U2 >
bool operator==( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(1) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator==( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) | |
template< class T1, class T2, class U1, class U2 >
bool operator!=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(2) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator!=( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) (bis C++20) |
|
template< class T1, class T2, class U1, class U2 >
bool operator<( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(3) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator<( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) (bis C++20) |
|
template< class T1, class T2, class U1, class U2 >
bool operator<=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(4) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator<=( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) (bis C++20) |
|
template< class T1, class T2, class U1, class U2 >
bool operator>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(5) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator>( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) (bis C++20) |
|
template< class T1, class T2, class U1, class U2 >
bool operator>=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(6) | (bis C++14) |
template< class T1, class T2, class U1, class U2 >
constexpr bool operator>=( const std::pair<T1, T2>& lhs,
const std::pair<U1, U2>& rhs );
|
(seit C++14) (bis C++20) |
|
template< class T1, class T2, class U1, class U2 >
constexpr std::common_comparison_category_t<synth-three-way-result<T1, U1>,
synth-three-way-result<T2, U2>>
operator<=>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
|
(7) | (seit C++20) |
lhs und rhs gleich sind, d.h. vergleicht lhs.first mit rhs.first und lhs.second mit rhs.second.|
Das Verhalten ist undefiniert, wenn der Typ und die Wertkategorie von |
(bis C++26) |
|
Diese Überladung nimmt nur an der Überladungsauflösung teil, wenn sowohl |
(seit C++26) |
lhs und rhs lexikografisch mittels operator<, d.h. vergleicht die ersten Elemente und nur wenn diese äquivalent sind, die zweiten Elemente. Das Verhalten ist undefiniert, wenn der Typ und die Wertkategorie von lhs.first < rhs.first, rhs.first < lhs.first oder lhs.second < rhs.second die Anforderungen von BooleanTestable nicht erfüllen.lhs und rhs lexikografisch mittels synth-three-way, d.h. vergleicht die ersten Elemente und nur wenn diese äquivalent sind, die zweiten Elemente. synth-three-way-result ist der Rückgabetyp von synth-three-way.|
Die Operatoren |
(seit C++20) |
Parameter
| lhs, rhs | - | zu vergleichende Paare |
Rückgabewert
Hinweise
|
Die relationalen Operatoren werden anhand des operator < jedes Elements definiert. |
(bis C++20) |
|
Die relationalen Operatoren werden anhand von synth-three-way definiert, das operator <=> verwendet, falls möglich, andernfalls operator < . Insbesondere, wenn ein Elementtyp selbst keinen operator <=> bereitstellt, aber implizit in einen dreiweg-vergleichbaren Typ konvertierbar ist, wird diese Konvertierung anstelle von operator < verwendet. |
(seit C++20) |
| Feature-Test Makro | Wert | Std | Funktion |
|---|---|---|---|
__cpp_lib_constrained_equality
|
202403L
|
(C++26) | Eingeschränkter operator == für std::pair |
Beispiel
Da operator < für Paare definiert ist, können Container von Paaren sortiert werden.
#include <algorithm> #include <iomanip> #include <iostream> #include <string> #include <utility> #include <vector> int main() { std::vector<std::pair<int, std::string>> v = {{2, "baz"}, {2, "bar"}, {1, "foo"}}; std::sort(v.begin(), v.end()); for (auto p : v) std::cout << '{' << p.first << ", " << std::quoted(p.second) << "}\n"; }
Ausgabe:
{1, "foo"}
{2, "bar"}
{2, "baz"}
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrektes Verhalten |
|---|---|---|---|
| LWG 296 | C++98 |
die Beschreibungen der Operatoren außer
==
und
<
fehlten
|
hinzugefügt |
|
LWG 2114
( P2167R3 ) |
C++98 | Typ-Vorbedingungen für boolesche Operationen fehlten | hinzugefügt |
| LWG 3865 | C++98 |
Vergleichsoperatoren akzeptierten nur
pair
s desselben Typs
|
akzeptieren
pair
s verschiedener Typen
|
Siehe auch
(entfernt in C++20)(entfernt in C++20)(entfernt in C++20)(entfernt in C++20)(entfernt in C++20)(C++20) |
vergleicht lexikografisch die Werte im Tupel (Funktionsschablone) |