C++ Standard Library C++ STL Library

C++ <stack> - operator!= Function



The C++ <stack> operator!= function is used to check whether two stacks are unequal or not. It returns true if two stacks are not equal, else returns false.

Syntax

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

Parameters

lhs First stack.
rhs Second stack.

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 stacks are unequal or not.

#include <iostream>
#include <stack>
using namespace std;
 
int main (){
  stack<int> stk1;
  stack<int> stk2;
  
  for(int i = 0; i<3; i++) {
    stk1.push(i);
    stk2.push(i);
  }

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

  stk1.pop();
  cout<<"After deleting top element of stk1.\n";
  if (stk1 != stk2)
    cout<<"stk1 and stk2 are not equal.\n";
  else
    cout<<"stk1 and stk2 are equal.\n";
    
  return 0;
}

The output of the above code will be:

stk1 and stk2 are equal.
After deleting top element of stk1.
stk1 and stk2 are not equal.

❮ C++ <stack> Library