![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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