operator==,!=,<,<=,>,>=(std::tuple)
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <tuple> 中定义
|
||
template< class... TTypes, class... UTypes > bool operator==( const tuple<TTypes...>& lhs, |
(1) | (C++11 起) |
template< class... TTypes, class... UTypes > bool operator!=( const tuple<TTypes...>& lhs, |
(2) | (C++11 起) |
template< class... TTypes, class... UTypes > bool operator<( const tuple<TTypes...>& lhs, |
(3) | (C++11 起) |
template< class... TTypes, class... UTypes > bool operator<=( const tuple<TTypes...>& lhs, |
(5) | (C++11 起) |
template< class... TTypes, class... UTypes > bool operator>( const tuple<TTypes...>& lhs, |
(4) | (C++11 起) |
template< class... TTypes, class... UTypes > bool operator>=( const tuple<TTypes...>& lhs, |
(6) | (C++11 起) |
比较每个元素的元组
3-6) lhs
相应的元素的元组rhs
原文:
Compares every element of the tuple
lhs
with the corresponding element of the tuple rhs
.比较
lhs
rhs
字典顺序,也就是,比较第一元素,如果它们是等效的,比较的第二个元素,如果这些是等价的,第三个元素进行比较,并等.原文:
Compares
lhs
and rhs
lexicographically, that is, compares the first elements, if they are equivalent, compares the second elements, if those are equivalent, compares the third elements, and so on.所有比较运算符短路;他们不用访问超出什么是必要的,以确定的比较结果的元组的元素.
原文:
All comparison operators are short-circuited; they do not access tuple elements beyond what is necessary to determine the result of the comparison.
目录 |
[编辑] 参数
lhs, rhs | - | 元组进行比较
|
[编辑] 返回值
1)truestd::get<i>(lhs) == std::get<i>(rhs)
[0, sizeof...(Types))
中的所有i,否则false。两个空的元组返回true.原文:
true if std::get<i>(lhs) == std::get<i>(rhs) for all i in
[0, sizeof...(Types))
, otherwise false. For two empty tuples returns true.2) !(lhs == rhs)
3)(bool)(std::get<0>(lhs) < std::get<0>(rhs)) || (!(bool)(std::get<0>(rhs) < std::get<0>(lhs)) && lhstail < rhstail),
lhstail
LHS没有它的第一个元素,并rhstail
是它的第一个元素的右边没有。对于两空的元组,返回false.原文:
(bool)(std::get<0>(lhs) < std::get<0>(rhs)) || (!(bool)(std::get<0>(rhs) < std::get<0>(lhs)) && lhstail < rhstail), where
lhstail
is lhs without its first element, and rhstail
is rhs without its first element. For two empty tuples, returns false.4) !(lhs < rhs)
5) rhs < lhs
6) !(rhs < lhs)
[编辑] 示例
由于运营商<被定义为元组,容器的元组进行排序.
原文:
Because operator< is defined for tuples, containers of tuples can be sorted.
#include <iostream> #include <tuple> #include <vector> #include <algorithm> int main() { std::vector<std::tuple<int, std::string, float>> v; v.emplace_back(2, "baz", -0.1); v.emplace_back(2, "bar", 3.14); v.emplace_back(1, "foo", 100.1); std::sort(v.begin(), v.end()); for(auto p: v) { std::cout << "(" << std::get<0>(p) << ", " << std::get<1>(p) << ", " << std::get<2>(p) << ")\n"; } }
输出:
(1, foo, 100.1) (2, bar, 3.14) (2, baz, -0.1)