W3cubDocs

/C++

operator==,!=,<,<=,>,>=(std::tuple)

Defined in header <tuple>
template< class... TTypes, class... UTypes >
bool operator==( const tuple<TTypes...>& lhs, 
                 const tuple<UTypes...>& rhs );
(1) (since C++11)
(constexpr since C++14)
template< class... TTypes, class... UTypes >
bool operator!=( const tuple<TTypes...>& lhs,
                 const tuple<UTypes...>& rhs );
(2) (since C++11)
(constexpr since C++14)
template< class... TTypes, class... UTypes >
bool operator<( const tuple<TTypes...>& lhs,
                const tuple<UTypes...>& rhs );
(3) (since C++11)
(constexpr since C++14)
template< class... TTypes, class... UTypes >
bool operator<=( const tuple<TTypes...>& lhs,
                 const tuple<UTypes...>& rhs );
(4) (since C++11)
(constexpr since C++14)
template< class... TTypes, class... UTypes >
bool operator>( const tuple<TTypes...>& lhs,
                const tuple<UTypes...>& rhs );
(5) (since C++11)
(constexpr since C++14)
template< class... TTypes, class... UTypes >
bool operator>=( const tuple<TTypes...>& lhs, 
                 const tuple<UTypes...>& rhs );
(6) (since C++11)
(constexpr since C++14)

1-2) Compares every element of the tuple lhs with the corresponding element of the tuple rhs.

3-6) 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.

Parameters

lhs, rhs - tuples to compare

Return value

1) 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), where lhstail is lhs without its first element, and rhstail is rhs without its first element. For two empty tuples, returns false.

4) !(rhs < lhs).

5) rhs < lhs.

6) !(lhs < rhs).

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;
    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";
    }
}

Output:

(1, foo, 100.1)
(2, bar, 3.14)
(2, baz, -0.1)

See also

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/tuple/operator_cmp