C++ std::stack::push() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

In C++, push() adds one new element on top of a std::stack. The new value becomes the top immediately. Older values stay below it until you pop().

The order you push in matters. Push 10, then 50, then 20, and top() gives 20 first. That is last in, first out (LIFO). A queue would serve 10 first instead, and a priority_queue would serve the largest first.

This function is a member function of std::stack<T>, declared in the <stack> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example st.push(...).

Syntax

Call push() with the value you want to add. Two overloads exist: one copies the value, the other moves it.

C++
std::stack<int> st;

st.push(10);
st.push(50);   // 50 becomes the top
st.push(20);   // 20 becomes the top

int newest = st.top();   // 20
CallWhat it does
st.push(value)Copies value onto the top of the stack.
st.push(std::move(value))Moves value onto the top. It is faster for big objects such as long strings.

push() returns nothing, so you cannot use the result. Each call is typically constant time for the default deque backing. If you want to build the object right inside the stack, see emplace().

Library declarations
C++
void push(const value_type& value);
void push(value_type&& value);   // C++11

// stack<T, Container> from <stack>
// It calls c.push_back(value) on the underlying container.

Parameters

ParameterDescription
valueRequired. The element to add. It must have the type T of the stack, or be convertible to it. It becomes the new top.

Return Value

Return ValueDescription
Nothingpush() returns void. The stack is changed in place, and its size grows by one.

C++ Examples

Example 1 — Push Numbers in Order

Push three numbers and then read them back. They come out in reverse order.

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

int main() {
    std::stack<int> st;
    st.push(10);
    st.push(40);
    st.push(20);

    std::cout << "Size: " << st.size() << '\n';
    std::cout << "Order out:";
    while (!st.empty()) {
        std::cout << ' ' << st.top();
        st.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Each push() adds one number on top. After three calls size() reports 3.
  2. The numbers were pushed as 10, 40, 20, and they come back as 20, 40, 10. The last push leaves first.
  3. The loop calls top() to read and pop() to remove, until empty() says there is nothing left.

Example 2 — Push Strings for a Browser History

Each page visit is a push. The current page is always the top.

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

int main() {
    std::stack<std::string> history;
    history.push("home");
    history.push("docs");
    history.push("api");

    std::cout << "Current: " << history.top() << '\n';
    while (!history.empty()) {
        std::cout << history.top() << '\n';
        history.pop();
    }
    return 0;
}

How It Works

  1. The stack holds std::string values. Each page name is pushed on top of the previous one.
  2. top() shows the newest page, "api", without removing it.
  3. Draining the stack prints the pages from newest to oldest: api, docs, home.

Example 3 — Build an Undo List with push()

Save every edit. Later you can pop to undo the last one.

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

int main() {
    std::stack<std::string> undo;
    undo.push("type A");
    undo.push("type B");
    undo.push("delete B");

    std::cout << "Last edit: " << undo.top() << '\n';
    undo.pop();
    std::cout << "After undo: " << undo.top() << '\n';
    std::cout << "Edits left: " << undo.size() << '\n';
    return 0;
}

How It Works

  1. Three edits are pushed. The newest edit, "delete B", sits on top.
  2. pop() removes that last edit. The previous edit, "type B", becomes the new top.
  3. Two edits remain. This is the classic undo pattern: push to record, pop to undo.

Frequently Asked Questions

It adds one element on top of the stack. That new element becomes the value returned by top().
No. For a stack, push() always places the element on top. A queue adds at the back and removes from the front.
With the default deque backing, push() is typically O(1). If you use a vector as the underlying container, occasional reallocations can make a push slower.
push() takes a ready-made object. emplace() takes constructor arguments and builds the object inside the stack, which can avoid an extra copy. Both place the new element on top.
No. A stack only supports operations at the top. To change lower elements you must pop until you reach them.

Next: push_range()

Add every element of a range on top (needs C++23) with std::stack.

push_range() 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.

10 people found this page helpful