operator==,!=,<,<=,>,>=,<=>(std::tuple)
Defined in header <tuple> |
||
---|---|---|
(1) | ||
template< class... TTypes, class... UTypes > bool operator==( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator==( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) | |
(2) | ||
template< class... TTypes, class... UTypes > bool operator!=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator!=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) (until C++20) |
|
(3) | ||
template< class... TTypes, class... UTypes > bool operator<( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator<( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) (until C++20) |
|
(4) | ||
template< class... TTypes, class... UTypes > bool operator<=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator<=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) (until C++20) |
|
(5) | ||
template< class... TTypes, class... UTypes > bool operator>( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator>( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) (until C++20) |
|
(6) | ||
template< class... TTypes, class... UTypes > bool operator>=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator>=( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(since C++14) (until C++20) |
|
template< class... TTypes, class... UTypes > constexpr /* see below */ operator<=>( const std::tuple<TTypes...>& lhs, const std::tuple<UTypes...>& rhs ); |
(7) | (since C++20) |
lhs
with the corresponding element of the tuple
rhs
.
lhs
and
rhs
lexicographically by
operator<
, 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.
For non-empty tuples, (3) is equivalent to.
if (std::get<0>(lhs) < std::get<0>(rhs)) return true; if (std::get<0>(rhs) < std::get<0>(lhs)) return false; if (std::get<1>(lhs) < std::get<1>(rhs)) return true; if (std::get<1>(rhs) < std::get<1>(lhs)) return false; ... return std::get<N - 1>(lhs) < std::get<N - 1>(rhs);
lhs
and
rhs
lexicographically by
synthesized three-way comparison (see below), 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.
The return type is the common comparison category type of results of synthesized three-way comparison on every pair of element in lhs
and rhs
. For empty tuples, the return type is std::strong_ordering
.
For non-empty tuples, (7) is equivalent to.
if (auto c = synth_three_way(std::get<0>(lhs), std::get<0>(rhs)); c != 0) return c; if (auto c = synth_three_way(std::get<1>(lhs), std::get<1>(rhs)); c != 0) return c; ... return synth_three_way(std::get<N - 1>(lhs), std::get<N - 1>(rhs));
synth_three_way
is an exposition-only function object performing synthesized three-way comparison.
sizeof...(TTypes)
and sizeof...(UTypes)
must be equal, otherwise the program is ill-formed or for operator<=>
, the operator function does not participate in overload resolution (since C++20). N
in above code is equal to both.
All comparison operators are short-circuited; they do not access tuple elements beyond what is necessary to determine the result of the comparison.
The |
(since C++20) |
Synthesized three-way comparisonGiven two object types
t < u ? std::weak_ordering::less : u < t ? std::weak_ordering::greater : std::weak_ordering::equivalent
The behavior of |
(since C++20) |
Parameters
lhs, rhs | - | tuples to compare |
Return value
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
.
!(lhs == rhs)
true
if the first non-equivalent element in
lhs
is less than the one in
rhs
,
false
if the first non-equivalent element in
rhs
is less than the one in
lhs
or there is no non-equivalent element. For two empty tuples, returns
false
.
!(rhs < lhs)
rhs < lhs
!(lhs < rhs)
std::strong_ordering::equal
otherwise. For two empty tuples, returns
std::strong_ordering::equal
.
Example
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{ {2, "baz", -0.1}, {2, "bar", 3.14}, {1, "foo", 10.1}, {2, "baz", -1.1}, }; std::sort(v.begin(), v.end()); for(const auto& p: v) { std::cout << "{" << std::get<0>(p) << ", " << std::get<1>(p) << ", " << std::get<2>(p) << "}\n"; } }
Output:
{1, foo, 10.1} {2, bar, 3.14} {2, baz, -1.1} {2, baz, -0.1}
See also
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
|
lexicographically compares the values in the pair (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/tuple/operator_cmp