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

C++ Array::operator<() Function



The C++ std::array::operator<() function is used to compare two array objects lexicographically. It checks whether one array is less than the another array by comparing corresponding elements in order. The comparison is based on the values of the elements, starting from the first index.

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 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, 3 > x = {1,2,33};
   std::array < int, 3 > y = {11,4,23};
   if (x < y) {
      std::cout << "x is less than y." << std::endl;
   } else {
      std::cout << "arr1 is not less than y." << std::endl;
   }
   return 0;
}

Output

Output of the above code is as follows −

x is less than 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 < char, 2 > x = {'a','b'};
   std::array < char, 2 > y = {'a','b'};
   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.
array.htm
Advertisements