C++ <stack> - operator== Function
The C++ <stack> operator== function is used to check whether two stacks are equal or not. It returns true if two stacks are 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 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 equal 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 equal.\n"; else cout<<"stk1 and stk2 are not equal.\n"; stk1.pop(); cout<<"After deleting top element of stk1.\n"; if (stk1 == stk2) cout<<"stk1 and stk2 are equal.\n"; else cout<<"stk1 and stk2 are not 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