Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Array::operator>() Function



The C++ std::array::operator>() function is a comparison operator used to check whether one array is greater than another. The comparison starts with the first element and proceeds sequentially until it find a difference. It returns true if first array is greater than second array otherwise it returns false.

Syntax

Following is the syntax for std::array::operator>() function.

bool operator>  ( const array<T,N>& lhs, const array<T,N>& rhs );

Parameters

  • lhs, rhs − It indicates the array containers.

Return Value

It returns true if first array container is greater than second otherwise false.

Exceptions

This function never throws exception.

Time complexity

Linear i.e. O(n)

Example 1

In the following example, we are going to consider the basic usage of the operator>() function.

#include <iostream>
#include <array>
int main() {
   std::array < int, 2 > x = {1,22};
   std::array < int, 2 > y = {11,32};
   if (x > y) {
      std::cout << "x is greater than y." << std::endl;
   } else {
      std::cout << "x is not greater than y." << std::endl;
   }
   return 0;
}

Output

Output of the above code is as follows −

x is not greater than y.

Example 2

Consider the following example, where we are going to use the operator>() on the array of different size and observing the output.

#include <iostream>
#include <array>
int main() {
   std::array < int, 2 > x = {1,2};
   std::array < int, 3 > y = {1,2,5};
   if (x > y) {
      std::cout << "x is greater than y." << std::endl;
   } else {
      std::cout << "x is not greater than y." << std::endl;
   }
   return 0;
}

Output

Following is the output of the above code −

main.cpp: In function 'int main()':
main.cpp:6:11: error: no match for 'operator>' (operand types are 'std::array<int, 2>' and 'std::array<int, 3>')
    6 |     if (x > y) {
      |         ~ ^ ~
      |         |   |
      |         |   array<[...],3>
      |         array<[...],2>

Example 3

Let's look at the following example, where we are going to consider the identical arrays and applying the operator>().

#include <iostream>
#include <array>
int main() {
   std::array < char, 2 > a = {'x','y'};
   std::array < char, 2 > b = {'x','y'};
   if (a > b) {
      std::cout << "a is greater than b." << std::endl;
   } else {
      std::cout << "a is not greater than b." << std::endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

a is not greater than b.
array.htm
Advertisements