operators (std::pair)
运算符=,%21=,<,<=,>,>=%28std::偶数%29
Defined in header | | |
---|---|---|
template< class T1, class T2 > bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (1) | (until C++14) |
template< class T1, class T2 > constexpr bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (1) | (since C++14) |
template< class T1, class T2 > bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (2) | (until C++14) |
template< class T1, class T2 > constexpr bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (2) | (since C++14) |
template< class T1, class T2 > bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (3) | (until C++14) |
template< class T1, class T2 > constexpr bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (3) | (since C++14) |
template< class T1, class T2 > bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (4) | (until C++14) |
template< class T1, class T2 > constexpr bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (4) | (since C++14) |
template< class T1, class T2 > bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (5) | (until C++14) |
template< class T1, class T2 > constexpr bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (5) | (since C++14) |
template< class T1, class T2 > bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (6) | (until C++14) |
template< class T1, class T2 > constexpr bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs | (6) | (since C++14) |
1-2%29测试如果lhs和rhs的两个元素相等,即比较。lhs.first
带着rhs.first
和lhs.second
带着rhs.second
3-6%29比较lhs
和rhs
词汇学上,也就是比较第一个元素,只有当它们是等价的,比较第二个元素。
参数
lhs, rhs | - | pairs to compare |
---|
返回值
1%29true
如果两者都是lhs.first == rhs.first
和lhs.second == rhs.second
,否则false
...
2%29true
如果lhs.first != rhs.first
或lhs.second != rhs.second
,否则false
...
3%29lhs.first<rhs.first、回报true.否则,如果rhs.first<lhs.first、回报false.否则,如果lhs.second<rhs.second、回报true.否则,返回false...
4%29!(rhs < lhs)...
5%29rhs < lhs...
6%29!(lhs < rhs)...
例
因为操作符<是为对定义的,所以可以对成对的容器进行排序。
二次
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
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 << "," << p.second << ")\n";
}
}
二次
产出:
二次
(1,foo)
(2,bar)
(2,baz)
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。