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.
Table of Contents
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
:
1 2 3 4 5 6 |
template< class Tx, class Container = std::deque<Tx> > class 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).
1 2 3 4 5 6 7 8 9 |
std::stack<int> myst; myst.push(10); myst.push(20); myst.push(30); myst.pop(); |
We can illustrate this stack container after every operation as below,
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.
1 2 3 4 5 6 7 8 9 10 11 |
void list_stack(std::stack<std::string> st) { while(!st.empty()) { std::cout << st.top() << ","; st.pop(); } std::cout << std::endl; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <stack> #include <string> void list_stack(std::stack<std::string> st) { while(!st.empty()) { std::cout << st.top() << ","; st.pop(); } std::cout << std::endl; } int main() { std::stack<std::string> myst; //-------- Add Some New Members -------- myst.push("One"); myst.push("Two"); myst.push("Three"); list_stack(myst); //-------- Add A New Member -------- myst.push("Four"); list_stack(myst); //-------- Delete The Last Member Added -------- myst.pop(); list_stack(myst); system("pause"); return 0; } |
and these operations will be listed as below,
1 2 3 4 5 |
Three,Two,One, Four,Three,Two,One, Three,Two,One, |
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.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition