C++ Forward_list Library - operator< Function



Description

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

Declaration

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

C++11

template <class T, class Alloc>
bool operator< (const forward_list<T,Alloc>& first, const forward_list<T,Alloc>& second);

Parameters

  • first − First forward_list object.

  • second − Second forward_list object of same type.

Return value

Returns true if first forward_list is less than second otherwise false.

Exceptions

This member function never throws exception.

Time complexity

Linear i.e. O(n)

Example

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4};
   forward_list<int> fl2 = {1, 2, 3, 4, 5};

   if (fl1 < fl2)
      cout << "First list is less than second." << endl;

   fl1.push_front(5);
   fl1.push_front(6);
   fl1.push_front(7);

   if (!(fl1 < fl2))
      cout << "First list is not less than second." << endl;

   return 0;
}

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

First list is less than second.
First list is not less than second.
forward_list.htm
Advertisements