Understand how std::stack works in C++

To spice up your code with proper tools!


Understand how std::stack works in C++


std::stack is a container library class (of which there are several classes) implemented as template .

In English stack means: stack, and that’s exactly what it does, it stores the data in a stacked way.

That is, a stack has the data structure LIFO(Last-In, First-Out).

A stack is different from a list, the stack has the idea as a “stack of dishes”, if you STACK the dishes and try to get the first one (which is at the bottom of the pile, down there!) , all above fall! In no list, you can take from any position depending on the subset of the type of list you create in structuring the data.

So, on the stack, you can only remove the last element you added.

Some member functions of std::stack are similar to or implemented from other classes in the Containers library, such as:

  • push()
  • pop()
  • empty()

And among others.

Let’s see how to use them!


Usage and implementation

To use std::stack you first need to add the <stack> library:

#include <stack>

Declare the stack:

std::stack<int> stack;

Add items/elements to stack:

stack.push( 2 );
stack.push( 4 );
stack.push( 12 );

Know who is on top of the stack, last to be added:

std::cout << stack.top() << '\n';

To delete the last element (the top one)

stack.pop();
std::cout << stack.top() << '\n';

Check if it is empty, remove and print:

while(true){
  if( !stack.empty() ){
    std::cout << stack.top() << '\n';
    stack.pop();
  }else{
    std::cout << "The stack is empty!" << '\n';
    break;
  }
}

The complete code:

#include <iostream>
#include <stack>

int main(){
  std::stack<int> stack;

  stack.push( 2 );
  stack.push( 4 );
  stack.push( 12 );

  stack.pop();
  std::cout << stack.top() << '\n';

  stack.push( 11 );
  stack.push( 13 );
  stack.push( 17 );
  stack.push( 8 );

  while(true){
    if( !pile.empty() ){
      std::cout << stack.top() << '\n';
      stack.pop();
    }else{
      std::cout << "The stack is empty!" << '\n';
      break;
    }
  }

  return 0;
}

For more tips on std::stack go to here .


cpp cppdaily


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles