C++ Array::operator<=() Function



The C++ std::array::operator<=() function is a comparison oeprator used to check if one array is less than or equal to another array. It compare corresponding elements in both arrays sequentially, from the first to the last. If the first unequal element is found, the comparison result is determined based on that result.

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 less or equal to the second container 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 = {11,2};
   std::array < int, 2 > y = {11,3};
   if (x <= y) {
      std::cout << "x is less than or equal to y." << std::endl;
   } else {
      std::cout << "x is greater than y." << std::endl;
   }
   return 0;
}

Output

Output of the above code is as follows −

x is less than or equal to y.

Example 2

Consider the following example, where we are going to take the identical arrays and applying the operator<=().

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

Output

Following is the output of the above code −

x is less than or equal to y.
array.htm
Advertisements