How to Use STL Stack Container in C++
-
Use
std::stack
to Declare Stack Container Object in C++ -
Use the
top()
Function to Access the Most Recently Added Element in C++ -
Use the
swap()
Function to Exchange Contents of Two Stacks in C++
This article will demonstrate multiple methods about how to use STL stack
container in C++.
Use std::stack
to Declare Stack Container Object in C++
std::stack
is called a container adapter, which can act as wrappers of standard containers but provide limited and specialized functionality. As an example, std::stack
class provides LIFO (last-in, firs-out) data structure and it can be mapped to a std::vector
or std::deque
container underneath. std::stack
can be initialized with another stack
object or compatible sequence containers like deque
, vector
and list
. Note, though, the default container used to store the stack
elements is deque
. Also, there is no constructor to support passing the initializer list or elements directly. We need to use the push
method once the stack
object has been declared.
Notice that, stack
object can not be iterated through using the range-based loop. Thus, we implement a special while
loop to print each element to the cout
stream. printStack
function takes stack
argument, but st1
that was initialized by the vector
object can’t be passed to it, as types of these two objects differ and compiler throws the error.
#include <iostream>
#include <stack>
#include <vector>
using std::cout;
using std::endl;
using std::stack;
using std::vector;
template <typename T>
void printStack(stack<T> s) {
while (!s.empty()) {
cout << s.top() << "; ";
s.pop();
}
cout << endl;
}
int main() {
vector<int> vec1 = {1, 2, 3, 4, 11};
stack st1{vec1};
stack<int> st2;
for (int i = 1; i <= 10; ++i) {
st2.push(i * 12);
}
// printStack(st1); Error - no matching function
printStack(st2);
return EXIT_SUCCESS;
}
Use the top()
Function to Access the Most Recently Added Element in C++
The top()
function is a member function that returns the top element in the stack. Note that this function does not automatically remove a returned element from the stack
object. One should call the pop
member function to remove it.
#include <iostream>
#include <stack>
using std::cout;
using std::endl;
using std::stack;
int main() {
stack<int> st2;
for (int i = 1; i <= 10; ++i) {
st2.push(i * 12);
}
cout << "top of the stack st2: ";
cout << st2.top() << endl;
return EXIT_SUCCESS;
}
Output:
top of the stack st2: 120
Use the swap()
Function to Exchange Contents of Two Stacks in C++
The swap()
function is a member function of the stack
container. It takes a reference to the stack
object and swaps elements from these stacks. Notice that, st1
object, which is initialized using the vector
object, can’t call the swap
function or be an argument to it. In order to enable the container initialized stack
to be used with the swap
function, it needs to be created using the std::move
call, as the st3
object is initialized in the following code example. The latter one also can be passed to the printStack
function without any issues. Finally, we swap elements of st2
/st3
stack objects and print the results to the console.
#include <deque>
#include <iostream>
#include <stack>
#include <vector>
using std::cout;
using std::deque;
using std::endl;
using std::stack;
using std::vector;
template <typename T>
void printStack(stack<T> s) {
while (!s.empty()) {
cout << s.top() << "; ";
s.pop();
}
cout << endl;
}
int main() {
deque<int> deq1 = {11, 12, 13, 14};
vector<int> vec1 = {1, 2, 3, 4, 11};
stack st1{vec1};
stack<int> st2;
for (int i = 1; i <= 10; ++i) {
st2.push(i * 12);
}
// st2.swap(st1); Error
stack<int> st3{std::move(deq1)};
printStack(st2);
printStack(st3);
st2.swap(st3);
printStack(st2);
printStack(st3);
return EXIT_SUCCESS;
}
Output:
120; 108; 96; 84; 72; 60; 48; 36; 24; 12;
14; 13; 12; 11;
14; 13; 12; 11;
120; 108; 96; 84; 72; 60; 48; 36; 24; 12;
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook