C++ Standard Library C++ STL Library

C++ <stack> - operator> Function



The C++ <stack> operator> function is used to check whether the first stack is greater than the second stack or not. It returns true if the first stack is greater than the second stack, 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 lexicographically greater than 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 the first stack is greater than the second stack 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 is greater than stk2.\n";
  else
    cout<<"stk1 is not greater than stk2.\n";

  stk2.pop();
  cout<<"After deleting top element of stk2.\n";
  if (stk1 > stk2)
    cout<<"stk1 is greater than stk2.\n";
  else
    cout<<"stk1 is not greater than stk2.\n";
    
  return 0;
}

The output of the above code will be:

stk1 is not greater than stk2.
After deleting top element of stk2.
stk1 is greater than stk2.

❮ C++ <stack> Library