C++ Tuple::relational operators (tuple)



In C++, relational operators for tuples enable comparison between tuples based on their elements values. Relational operators, such as ==, !=, <, <=, >, and >=, enable comparison between tuples, allowing for convenient sorting and logical operations, facilitating comparisons based on lexicographical order.

Comparison of two tuples, begin with the first elements of each tuple. If they are equal , the comparison proceeds to the next elements until a difference is found or the tuple end. If all the elements are equal, the tuples are considered as equal. If the tuples are of different size, the comparison is performed up to the size of the smaller tuple.

Syntax

Following is the syntax for std::tuple::relational operators.

bool operator== ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);
or
bool operator!= ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);
or
bool operator< ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);
or
bool operator> ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);
or
bool operator>= ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);
or
bool operator<= ( const tuple<TTypes...>& lhs, const tuple<UTypes...>& rhs);

Parameters

  • lhs, rhs − It indicates the tuples to compare.

Return Value

It returns true if the condition holds, and false otherwise.

Example

Let's look at the following example, where we are going to check whether the tuples equal or not.

#include <tuple>
#include <iostream>
int main()
{
    std::tuple<int, char> x = std::make_tuple(1, 'A');
    std::tuple<int, char> y = std::make_tuple(1, 'A');
    if (x == y)
        std::cout << "They Are Equal." << std::endl;
    else
        std::cout << "They Are Not Equal." << std::endl;
    return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

They Are Equal.

Example

Consider the following example, where we are going to check whether the tuple1 is lessthan tuple2.

#include <tuple>
#include <iostream>
int main()
{
    std::tuple<int, char> x = std::make_tuple(12, 'A');
    std::tuple<int, char> y = std::make_tuple(2, 'A');
    if (x < y)
        std::cout << "x is less than y" << std::endl;
    else
        std::cout << "x is not less than y" << std::endl;
    return 0;
}

Output

Following is the output of the above code −

x is not less than y

Example

In the following example, we are going to check whether the tuple1 is greaterthan or euqal to tuple2.

#include <tuple>
#include <iostream>
int main()
{
    std::tuple<int, char> x = std::make_tuple(20, 'A');
    std::tuple<int, char> y = std::make_tuple(2, 'B');
    if (x >= y)
        std::cout << "x is greater than or equal to y" << std::endl;
    else
        std::cout << "x is not greater than or equal to y" << std::endl;
    return 0;
}

Output

Output of the above code is as follows −

x is greater than or equal to y

Example

Following is the example, where we are going to use the '!=' operator and observing the output.

#include <tuple>
#include <iostream>
int main() {
    std::tuple<int, char> x = std::make_tuple(1, 'A');
    std::tuple<int, char> y = std::make_tuple(2, 'B');
    if (x != y)
        std::cout << "True" << std::endl;
    else
        std::cout << "False" << std::endl;
    return 0;
}

Output

Following is the output of the above code −

True
Advertisements