Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Veranschaulicht, wie die Funktion der Standardvorlagenbibliothek (STL) Paare logischen Operators in Visual C++ verwendet.
Hinweis |
|---|
Nur operator< und operator== sind erforderlich, um alle logischen Operatoren zu definieren. |
template<class _T1, class _T2> inline
bool operator==(
const pair<_T1, _T2>& _X,
const pair<_T1, _T2>& _Y
) {return ( _X.first == _Y.first && _X.second == _Y.second ); }
template<class _T1, class _T2> inline
bool operator<(
const pair<_T1, _T2>& _X,
const pair<_T1, _T2>& _Y
) {return (
_X.first < _Y.first || !( _Y.first < _X.first)
&& _X.second < _Y.second
); }
Hinweise
Hinweis |
|---|
Die Klasse/Parameternamen im Prototyp stimmen nicht mit der Version in der Headerdatei ab.Einige wurden geändert, um die Lesbarkeit zu verbessern. |
Die Funktionen werden im Kommentarteil des Beispiels beschrieben.
Beispiel
// paircomp.cpp
// compile with: /EHsc
// Illustrates several comparison operators used to compare two
// pair objects.
//
// Functions:
// operator== - returns true if two pair objects are identical.
//
// operator!= - returns true if two pair objects are not identical.
//
// operator< - returns true for (A < B) if pair object A is less
// than pair object B.
//
// operator<= - returns true for (A <= B) if pair object A is less
// than or equal to pair object B.
//
// operator> - returns true for (A > B) if pair object A is greater
// than pair object B.
//
// operator>= - returns true for (A >= B) if pair object A is greater
// than or equal to pair object B.
/////////////////////////////
#include <iostream>
#include <utility>
using namespace std ;
/* STL pair data type containing int and float */
typedef struct pair<int, float> PAIR_IF;
int main(void)
{
PAIR_IF A(10,3.14f);
PAIR_IF B(18,3.14f);
PAIR_IF C(10,6.28f);
PAIR_IF D(10,3.14f);
/* show pair values */
cout << "A = ( " << A.first << " , " << A.second << " )" << endl;
cout << "B = ( " << B.first << " , " << B.second << " )" << endl;
cout << "C = ( " << C.first << " , " << C.second << " )" << endl;
cout << "D = ( " << D.first << " , " << D.second << " )" << endl;
/* operator== */
if (A==D)
cout << "A and D are equal" << endl;
else
cout << "A and D are not equal" << endl;
/* operator!= */
if (B!=C)
cout << "B and C are not equivalent" << endl;
else
cout << "B and C are equivalent" << endl;
/* operator> */
if (A>C)
cout << "A is greater than C" << endl;
else
cout << "A is not greater than C" << endl;
/* operator>= */
if (A>=C)
cout << "A is greater than or equal to C" << endl;
else
cout << "A is not greater than or equal to C" << endl;
/* operator< */
if (C<D)
cout << "C is less than D" << endl;
else
cout << "C is not less than D" << endl;
/* operator<= */
if (C<D)
cout << "C is less than or equal to D" << endl;
else
cout << "C is not less than or equal to D" << endl;
}
Output
A = ( 10 , 3.14 )
B = ( 18 , 3.14 )
C = ( 10 , 6.28 )
D = ( 10 , 3.14 )
A and D are equal
B and C are not equivalent
A is not greater than C
A is not greater than or equal to C
C is not less than D
C is not less than or equal to D
Anforderungen
Header: <utility>
Hinweis