C++ Standard Library C++ STL Library

C++ <list> - operator!= Function



The C++ <list> operator!= function is used to check whether two lists are unequal or not. It returns true if two lists are not equal, else returns false. operator!= compares elements of lists sequentially and stops comparison after first mismatch.

Syntax

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

Parameters

lhs First list.
rhs Second list.

Return Value

Returns true if the contents of lhs are not 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 lists are unequal or not.

#include <iostream>
#include <list>
using namespace std;
 
int main (){
  list<int> list1 {10, 20, 30};
  list<int> list2 {10, 20, 30};
  list<int> list3 {10, 20};

  if (list1 != list2)
    cout<<"list1 and list2 are not equal.\n";
  else
    cout<<"list1 and list2 are equal.\n";

  if (list1 != list3)
    cout<<"list1 and list3 are not equal.\n";
  else
    cout<<"list1 and list3 are equal.\n";
    
  return 0;
}

The output of the above code will be:

list1 and list2 are equal.
list1 and list3 are not equal.

❮ C++ <list> Library