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


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

Syntax

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

Parameters

lhs First forward_list.
rhs Second forward_list.

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 forward_list::operator< function is used to check whether the first forward_list is less than the second forward_list or not.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> f_list1 {10, 20, 30};
  forward_list<int> f_list2 {10, 20, 30};
  forward_list<int> f_list3 {40, 50, 60};

  if (f_list1 < f_list2)
    cout<<"f_list1 is less than f_list2.\n";
  else
    cout<<"f_list1 is not less than f_list2.\n";

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

The output of the above code will be:

f_list1 is not less than f_list2.
f_list1 is less than f_list3.

❮ C++ - Forward List