<utility>
運算子
注意
使用 Type&
的運算子包含在底下 namespace rel_ops
。
operator!=
測試成對運算子左側的物件是否不等於右側的物件。
template <class Type>
constexpr bool operator!=(const Type& left, const Type& right);
template <class T, class U>
constexpr bool operator!=(const pair<T, U>& left, const pair<T, U>& right);
參數
left
pair
類型的物件。
right
pair
類型的物件。
傳回值
true
如果配對不相等則為 ; false
如果配對相等,則為 。
備註
如果配對的每個項目都相等,則其中一對等於另一對。 如果其中一對的第一個或第二個項目不等於另一對的對應項目,則這兩對不相等。
範例
// utility_op_ne.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3;
p1 = make_pair ( 10, 1.11e-1 );
p2 = make_pair ( 1000, 1.11e-3 );
p3 = make_pair ( 10, 1.11e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl << endl;
if ( p1 != p2 )
cout << "The pairs p1 and p2 are not equal." << endl;
else
cout << "The pairs p1 and p2 are equal." << endl;
if ( p1 != p3 )
cout << "The pairs p1 and p3 are not equal." << endl;
else
cout << "The pairs p1 and p3 are equal." << endl;
}
The pair p1 is: ( 10, 0.111 ).
The pair p2 is: ( 1000, 0.00111 ).
The pair p3 is: ( 10, 0.111 ).
The pairs p1 and p2 are not equal.
The pairs p1 and p3 are equal.
operator==
測試成對運算子左側的物件是否等於右側的物件。
template <class T, class U>
constexpr bool operator==(const pair<T, U>& left, const pair<T, U>& right);
參數
left
pair
類型的物件。
right
pair
類型的物件。
傳回值
true
如果配對相等則為 ; false
如果 s 不相等,則 pair
為 。
備註
如果配對的每個項目都相等,則其中一對等於另一對。 函式會傳回 left
。 first == right
。 first & & left
。 second == right
。 second。 如果其中一對的第一個或第二個項目不等於另一對的對應項目,則這兩對不相等。
範例
// utility_op_eq.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3;
p1 = make_pair ( 10, 1.11e-1 );
p2 = make_pair ( 1000, 1.11e-3 );
p3 = make_pair ( 10, 1.11e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl << endl;
if ( p1 == p2 )
cout << "The pairs p1 and p2 are equal." << endl;
else
cout << "The pairs p1 and p2 are not equal." << endl;
if ( p1 == p3 )
cout << "The pairs p1 and p3 are equal." << endl;
else
cout << "The pairs p1 and p3 are not equal." << endl;
}
operator<
測試成對運算子左側的物件是否小於右側的物件。
template <class T, class U>
constexpr bool operator<(const pair<T, U>& left, const pair<T, U>& right);
參數
left
運算子左側 pair
類型的物件。
right
運算子右側 pair
類型的物件。
傳回值
true
pair
如果運算子左邊的 嚴格小於 pair
運算子右邊的 ,則為 ,否則false
為 。
備註
如果向左小於且不等於右,pair
right
則left
pair
物件會嚴格小於物件。
在比較配對時,兩對中第一個項目的值,優先權最高。 如果值不同,則會將其比較結果視為配對比較的結果。 如果第一個項目的值並無不同,則會比較第二個項目的值,並將其比較結果視為配對比較的結果。
範例
// utility_op_lt.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3;
p1 = make_pair ( 10, 2.22e-1 );
p2 = make_pair ( 100, 1.11e-1 );
p3 = make_pair ( 10, 1.11e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl << endl;
if ( p1 < p2 )
cout << "The pair p1 is less than the pair p2." << endl;
else
cout << "The pair p1 is not less than the pair p2." << endl;
if ( p1 < p3 )
cout << "The pair p1 is less than the pair p3." << endl;
else
cout << "The pair p1 is not less than the pair p3." << endl;
}
The pair p1 is: ( 10, 0.222 ).
The pair p2 is: ( 100, 0.111 ).
The pair p3 is: ( 10, 0.111 ).
The pair p1 is less than the pair p2.
The pair p1 is not less than the pair p3.
operator<=
測試成對運算子左側的物件是否小於或等於右側的物件。
template <class Type>
constexpr bool operator<=(const Type& left, const Type& right);
template <class T, class U>
constexpr bool operator<=(const pair<T, U>& left, const pair<T, U>& right);
參數
left
運算子左側 pair
類型的物件。
right
運算子右側 pair
類型的物件。
傳回值
true
pair
如果運算子左邊的 小於或等於pair
運算子右邊的 ,則為 ,否則false
為 。
備註
在比較配對時,兩對中第一個項目的值,優先權最高。 如果值不同,則會將其比較結果視為配對比較的結果。 如果第一個項目的值並無不同,則會比較第二個項目的值,並將其比較結果視為配對比較的結果。
範例
// utility_op_le.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3, p4;
p1 = make_pair ( 10, 2.22e-1 );
p2 = make_pair ( 100, 1.11e-1 );
p3 = make_pair ( 10, 1.11e-1 );
p4 = make_pair ( 10, 2.22e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl;
cout << "The pair p4 is: ( " << p4.first << ", "
<< p4.second << " )." << endl << endl;
if ( p1 <= p2 )
cout << "The pair p1 is less than or equal to the pair p2." << endl;
else
cout << "The pair p1 is greater than the pair p2." << endl;
if ( p1 <= p3 )
cout << "The pair p1 is less than or equal to the pair p3." << endl;
else
cout << "The pair p1 is greater than the pair p3." << endl;
if ( p1 <= p4 )
cout << "The pair p1 is less than or equal to the pair p4." << endl;
else
cout << "The pair p1 is greater than the pair p4." << endl;
}
The pair p1 is: ( 10, 0.222 ).
The pair p2 is: ( 100, 0.111 ).
The pair p3 is: ( 10, 0.111 ).
The pair p4 is: ( 10, 0.222 ).
The pair p1 is less than or equal to the pair p2.
The pair p1 is greater than the pair p3.
The pair p1 is less than or equal to the pair p4.
operator>
測試成對運算子左側的物件是否大於右側的物件。
template <class Type>
constexpr bool operator>(const Type& left, const Type& right);
template <class T, class U>
constexpr bool operator>(const pair<T, U>& left, const pair<T, U>& right);
參數
left
運算子左側 pair
類型的物件。
right
運算子右側 pair
類型的物件。
傳回值
true
pair
如果運算子左邊的 嚴格大於pair
運算子右邊的 ,則為 ,否則false
為 。
備註
如果左邊大於且不等於右邊,pair
right
則物件left
pair
會嚴格大於物件。
在比較配對時,兩對中第一個項目的值,優先權最高。 如果值不同,則會將其比較結果視為配對比較的結果。 如果第一個項目的值並無不同,則會比較第二個項目的值,並將其比較結果視為配對比較的結果。
範例
// utility_op_gt.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3, p4;
p1 = make_pair ( 10, 2.22e-1 );
p2 = make_pair ( 100, 1.11e-1 );
p3 = make_pair ( 10, 1.11e-1 );
p4 = make_pair ( 10, 2.22e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl;
cout << "The pair p4 is: ( " << p4.first << ", "
<< p4.second << " )." << endl << endl;
if ( p1 > p2 )
cout << "The pair p1 is greater than the pair p2." << endl;
else
cout << "The pair p1 is not greater than the pair p2." << endl;
if ( p1 > p3 )
cout << "The pair p1 is greater than the pair p3." << endl;
else
cout << "The pair p1 is not greater than the pair p3." << endl;
if ( p1 > p4 )
cout << "The pair p1 is greater than the pair p4." << endl;
else
cout << "The pair p1 is not greater than the pair p4." << endl;
}
The pair p1 is: ( 10, 0.222 ).
The pair p2 is: ( 100, 0.111 ).
The pair p3 is: ( 10, 0.111 ).
The pair p4 is: ( 10, 0.222 ).
The pair p1 is not greater than the pair p2.
The pair p1 is greater than the pair p3.
The pair p1 is not greater than the pair p4.
operator>=
測試成寺運算子左側的物件是否大於或等於右側的物件。
template <class Type>
constexpr bool operator>=(const Type& left, const Type& right);
template <class T, class U>
constexpr bool operator>=(const pair<T, U>& left, const pair<T, U>& right);
參數
left
運算子左側 pair
類型的物件。
right
運算子右側 pair
類型的物件。
傳回值
true
pair
如果運算子左邊的 大於或等於pair
運算子右邊的 ,則為 ,否則false
為 。
備註
在比較配對時,兩對中第一個項目的值,優先權最高。 如果值不同,則會將其比較結果視為配對比較的結果。 如果第一個項目的值並無不同,則會比較第二個項目的值,並將其比較結果視為配對比較的結果。
範例
// utility_op_ge.cpp
// compile with: /EHsc
#include <utility>
#include <iomanip>
#include <iostream>
int main( )
{
using namespace std;
pair <int, double> p1, p2, p3, p4;
p1 = make_pair ( 10, 2.22e-1 );
p2 = make_pair ( 100, 1.11e-1 );
p3 = make_pair ( 10, 1.11e-1 );
p4 = make_pair ( 10, 2.22e-1 );
cout.precision ( 3 );
cout << "The pair p1 is: ( " << p1.first << ", "
<< p1.second << " )." << endl;
cout << "The pair p2 is: ( " << p2.first << ", "
<< p2.second << " )." << endl;
cout << "The pair p3 is: ( " << p3.first << ", "
<< p3.second << " )." << endl;
cout << "The pair p4 is: ( " << p4.first << ", "
<< p4.second << " )." << endl << endl;
if ( p1 >= p2 )
cout << "Pair p1 is greater than or equal to pair p2." << endl;
else
cout << "The pair p1 is less than the pair p2." << endl;
if ( p1 >= p3 )
cout << "Pair p1 is greater than or equal to pair p3." << endl;
else
cout << "The pair p1 is less than the pair p3." << endl;
if ( p1 >= p4 )
cout << "Pair p1 is greater than or equal to pair p4." << endl;
else
cout << "The pair p1 is less than the pair p4." << endl;
}
The pair p1 is: ( 10, 0.222 ).
The pair p2 is: ( 100, 0.111 ).
The pair p3 is: ( 10, 0.111 ).
The pair p4 is: ( 10, 0.222 ).
The pair p1 is less than the pair p2.
Pair p1 is greater than or equal to pair p3.
Pair p1 is greater than or equal to pair p4.