C++ std::stack Functions

Beginner
8 min read
Updated: Nov 2026
8 function guides
Function index

About This Index

This page is the C++ std::stack Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.

Learn what a stack is on /cpp/stack. Use this page when you need a specific function such as push(), top(), or pop().

Member functions of a LIFO adaptor.

std::stack lives in the <stack> header. You call its functions with a dot, such as st.push(5). It is last in, first out: the newest element sits on top. There is no iterator and no []. The 8 functions are listed below with the C++ version each one needs.

How to Call a stack Function

Include <stack>, create a stack, and call the functions on it:

C++
#include <stack>
#include <iostream>
#include <vector>

std::stack<int> st;                              // LIFO: newest on top

st.push(10);                                      // add on top
st.push(40);
st.emplace(20);                                   // build in place (C++11)
int newest = st.top();                            // 20, read (or assign) the top
std::size_t n = st.size();                        // 3
bool none = st.empty();                           // false
st.pop();                                         // remove 20, returns nothing

std::vector<int> more = {5, 6};
st.push_range(more);                              // C++23 only

C++ std::stack Functions Table

All function guides in this C++ tutorial series (8 tutorials), grouped by purpose.

Capacity

2 functions

Check if the stack is empty and how many elements it holds.

FunctionDescriptionSinceTutorial
empty()Check if the stack has no elements. It returns true or false.C++98Try it
size()Count how many elements are in the stack.C++98Try it

Element access

1 function

Look at the one element you can see: the newest one on top (LIFO).

FunctionDescriptionSinceTutorial
top()Read (or assign) the top element without removing it.C++98Try it

Modifiers

5 functions

Add elements on top, remove the top element, or swap two stacks.

FunctionDescriptionSinceTutorial
push()Add one element on top of the stack.C++98Try it
push_range()Add every element of a range on top (needs C++23).C++23Try it
emplace()Build a new element on top from constructor arguments.C++11Try it
pop()Remove the top element. It does not return the value.C++98Try it
swap()Swap the contents of two stacks in one step.C++11Try it

Frequently Asked Questions

std::stack has a small set of member functions: top() to read the top element, empty() and size() to check capacity, and push(), push_range() (C++23), emplace() (C++11), pop(), and swap() (C++11) to change the stack.
This series covers 8 functions, grouped as capacity (empty, size), element access (top), and modifiers (push, push_range, emplace, pop, swap).
emplace() and swap() need C++11. push_range() needs C++23. The functions top(), empty(), size(), push(), and pop() work in every C++ standard.
A stack is a container adaptor that only lets you see the top element. It has no iterators and no random access. To visit every element you must use top() and pop() in a loop until empty() is true.
Read the C++ stack page first. Then open push(), top(), and pop() from the tables below, because those three are used in almost every program that has a stack.

Start with emplace()

Build a new element right inside the stack, the first function in the alphabetical list.

emplace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful