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


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. 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 equal to 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 two forward_lists are equal 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 {10, 20};

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

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

The output of the above code will be:

f_list1 and f_list2 are equal.
f_list1 and f_list3 are not equal.

❮ C++ - Forward List