C++ Standard Library C++ STL Library

C++ <array> - operator<= Function



The C++ <array> operator<= function is used to check whether the first array is less than or equal to the second array or not. It returns true if the first array is less than or equal to the second array, else returns false. operator<= compares elements of arrays sequentially and stops comparison after first mismatch.

Syntax

template <class T, size_T N>
bool operator<= (const array<T,N>& lhs, const array<T,N>& rhs);

Parameters

lhs First array.
rhs Second array.

Return Value

Returns true if the contents of lhs are lexicographically less than or equal to the contents of rhs, else returns false.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the operator<= function is used to check whether the first array is less than or equal to the second array or not.

#include <iostream>
#include <array>
using namespace std;
 
int main (){
  array<int, 3> arr1 {10, 20, 30};
  array<int, 3> arr2 {0, 10, 20};
  array<int, 3> arr3 {40, 50, 60};

  if (arr1 <= arr2)
    cout<<"arr1 is less than or equal to arr2.\n";
  else
    cout<<"arr1 is not less than or equal to arr2.\n";

  if (arr1 <= arr3)
    cout<<"arr1 is less than or equal to arr3.\n";
  else
    cout<<"arr1 is not less than or equal to arr3.\n";
    
  return 0;
}

The output of the above code will be:

arr1 is not less than or equal to arr2.
arr1 is less than or equal to arr3.

❮ C++ <array> Library

5