Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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

Syntax

template <class T, class Alloc>
bool operator< (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator< (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);

Parameters

lhs First vector.
rhs Second vector.

Return Value

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

Time Complexity

Linear i.e, Θ(n).

Example:

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

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> vec1 {10, 20, 30};
  vector<int> vec2 {10, 20, 30};
  vector<int> vec3 {40, 50, 60};

  if (vec1 < vec2)
    cout<<"vec1 is less than vec2.\n";
  else
    cout<<"vec1 is not less than vec2.\n";

  if (vec1 < vec3)
    cout<<"vec1 is less than vec3.\n";
  else
    cout<<"vec1 is not less than vec3.\n";
    
  return 0;
}

The output of the above code will be:

vec1 is not less than vec2.
vec1 is less than vec3.

❮ C++ - Vector