C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

What Is The Stack stdstack In Modern C++

Modern C++ defines four container categories, and one of these is known as Container Adapters. There is a very useful container adapter implemented by the std::stack. In this post, we explain what std::stack is, and how can we add, delete, and print out them. Before that let’s remind ourselves what containers are in C++ programming and what are their types.

What is a container in C++?

Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their powerful methods and properties. A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well.

What are the basic container types C++?

The C++ Standards library defines 4 container types. 

  • Sequence Containers ( vector, array, deque, list, forward_list )
  • Associative Containers ( map, multimap, set, multiset )
  • Unordered Associative Containers ( unordered_map, unordered_multimap, unordered_set, unordered_multiset )
  • Container Adapters  ( stack, queue, priority_queue )

If you want to learn more about these each type, here are some basic details,

What is std::stack in modern C++?

The stack (std::stack) is a container adaptor class defined in <stack> header that adds new member to the top of the stack and deletes the last added member on the top. They have no iterators defined.

Here is the syntax for the std::stack:

Is there a simple example of how to use std::stack in modern C++?

When we push a member to a stack it will push it to the back of the container (this is the top of stack) and when we pop an element it will delete element from the top (that is the last element we added).

Here is a simple example to add 10 ,20 and 30 integer members and deletes the last one (30).

We can illustrate this stack container after every operation as below,

Figure 1 The state of a stack container after push and pop operations

Here members will be listed as 20, 10, because we delete the top of the stack that is the last member we added (30).

How can we print out all elements of a std::stack in modern C++?

We should note that there is no iterator to print out members of a std::stack. But we can list stack members from top to bottom as shown below.

Is there a full example about std::stack in modern C++?

Here is a full std::stack example that adds 3 string members and then one more member, at last deletes it, and lists members after the each of these stages.

and these operations will be listed as below,

As you see, we added string “Four” to the top of the stack by using push() method and when we use pop() method we delete the top of the stack which was “Four”. In most cases, std::list might be better solution instead of using std::stack, in some cases if you need speed in operations std::stack could be very useful.

What Is The Stack stdstack In Modern C++

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?