C++ Vector Library - operator< Function


The C++ function std::vector::operator< tests whether first vector is less than other or not.

Operator < compares element sequentially and comparison stops at first mismatch.


Following is the declaration for std::vector::operator< function form std::vector header.

template <class T, class Alloc>
bool operator<  (const vector<T,Alloc>& v1, const vector<T,Alloc>& v2);


  • v1 − First vector.

  • v2 − Second vector.

Return value

Returns true if first vector is less than second otherswise false.


This function never throws exception.

Time complexity

Linear i.e. O(n)


The following example shows the usage of std::vector::operator< function.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v1 = {1, 2};
   vector<int> v2 = {1, 2, 3, 4, 5};

   if (v1 < v2)
      cout << "v1 is less than v2" << endl;

   if (!(v1 < v2))
      cout << "v1 is greater than v2" << endl;

   return 0;

Let us compile and run the above program, this will produce the following result −

v1 is less than v2
v1 is greater than v2
Kickstart Your Career

Get certified by completing the course

Get Started