C++ Standard Library C++ STL Library

C++ <forward_list> - operator<= Function



The C++ <forward_list> operator<= function is used to check whether the first forward_list is less than or equal to the second forward_list or not. It returns true if the first forward_list is less than or equal to the second forward_list, else returns false. 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 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 forward_list is less than or equal to the second forward_list or not.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist1 {10, 20, 30};
  forward_list<int> flist2 {10, 20};
  forward_list<int> flist3 {40, 50, 60};

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

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

The output of the above code will be:

flist1 is not less than or equal to flist2.
flist1 is less than or equal to flist3.

❮ C++ <forward_list> Library