C++ Standard Library C++ STL Library

C++ <forward_list> - operator== Function



The C++ <forward_list> operator== function is used to check whether two forward_lists are equal or not. It returns true if two forward_lists are equal, 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 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 two forward_lists are equal 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, 30};
  forward_list<int> flist3 {10, 20};

  if (flist1 == flist2)
    cout<<"flist1 and flist2 are equal.\n";
  else
    cout<<"flist1 and flist2 are not equal.\n";

  if (flist1 == flist3)
    cout<<"flist1 and flist3 are equal.\n";
  else
    cout<<"flist1 and flist3 are not equal.\n";
    
  return 0;
}

The output of the above code will be:

flist1 and flist2 are equal.
flist1 and flist3 are not equal.

❮ C++ <forward_list> Library

5