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

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

Definition

In C++11, swap() exchanges everything inside two std::stack objects. After a.swap(b), stack a holds what b had, and stack b holds what a had.

The swap is fast, because the stacks just trade their inner containers. No element is copied one by one. Both stacks must have the same type, which includes the element type and the underlying container. You can also write std::swap(a, b).

This function is a member function of std::stack<T>, declared in the <stack> header. It needs C++11 or newer (-std=c++11). You call it with a dot, for example st.swap(...).

Syntax

Call swap() on one stack and pass the other stack. Or call the non-member std::swap with both.

C++
std::stack<int> a;
std::stack<int> b;
a.push(3);
b.push(20);

a.swap(b);          // a now has 20, b now has 3
std::swap(a, b);    // swaps them back
CallWhat it does
a.swap(b)Exchanges the contents of a and b. Nothing is returned.
std::swap(a, b)The non-member version. It does the same thing and calls a.swap(b) inside.

Swapping does not change the LIFO order inside each stack, and it does not call top() or pop(). stack has no clear(), so swapping with an empty stack is a common way to empty it.

Library declarations
C++
void swap(stack& other) noexcept(/* see below */);   // C++11

// Non-member overload (C++11):
template<class T, class Container>
void swap(stack<T, Container>& lhs, stack<T, Container>& rhs);

// stack<T, Container> from <stack>
// It swaps the underlying container c.

Parameters

ParameterDescription
otherRequired. Another stack of the same type. It trades its contents with the stack you call swap() on.

Return Value

Return ValueDescription
Nothingswap() returns void. Both stacks are changed in place.

C++ Examples

Example 1 — Swap Two Stacks

Fill two stacks with different numbers. After swap(), the tops and sizes are exchanged.

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

int main() {
    std::stack<int> a;
    a.push(1);
    a.push(2);
    a.push(3);

    std::stack<int> b;
    b.push(10);
    b.push(20);

    a.swap(b);

    std::cout << "a: top " << a.top() << ", size " << a.size() << '\n';
    std::cout << "b: top " << b.top() << ", size " << b.size() << '\n';
    return 0;
}

How It Works

  1. Before the swap, stack a has three numbers with top 3. Stack b has two numbers with top 20.
  2. a.swap(b) exchanges the whole contents. No element is copied one by one, so it is fast.
  3. Now a has the old data of b: top 20 and size 2. Stack b has top 3 and size 3.

Example 2 — Empty a Stack by Swapping

stack has no clear(). Swap with a new empty stack to throw all the elements away.

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

int main() {
    std::stack<int> st;
    st.push(7);
    st.push(14);
    st.push(21);
    std::cout << "Before: " << st.size() << '\n';

    std::stack<int> empty_stack;
    st.swap(empty_stack);

    std::cout << "After st: " << st.size() << '\n';
    std::cout << "Old data now in empty_stack: " << empty_stack.size() << '\n';
    return 0;
}

How It Works

  1. st holds three numbers, so the first line prints 3.
  2. empty_stack is a new stack with nothing inside. After st.swap(empty_stack), st has no elements.
  3. The old elements did not disappear. They moved to empty_stack, which now has size 3. They are freed when that variable goes out of scope.

Example 3 — Use std::swap on Two Stacks

The non-member std::swap(a, b) works too. Both stacks must have the same type.

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

int main() {
    std::stack<std::string> a;
    a.push("red");
    a.push("blue");

    std::stack<std::string> b;
    b.push("green");

    std::swap(a, b);

    std::cout << "a: top " << a.top() << ", size " << a.size() << '\n';
    std::cout << "b: top " << b.top() << ", size " << b.size() << '\n';
    return 0;
}

How It Works

  1. Both stacks store std::string with the default container, so they have the same type and can be swapped.
  2. std::swap(a, b) gives the same result as a.swap(b). It is the non-member way to write the same call.
  3. Stack a now holds "green" only. Stack b holds "red" and "blue", and its top is "blue".

Frequently Asked Questions

It exchanges the contents of two stacks. After a.swap(b), a has the elements of b and b has the elements of a.
Yes. The element type and the underlying container must both be the same. stack<int> cannot be swapped with stack<int, vector<int>>.
No. It only swaps the inner containers, so no element is copied one by one. This is why it is fast.
Yes. A stack has no clear(), so swap it with an empty stack, or assign a new one: st = std::stack<int>();.
The member swap() and the non-member std::swap for stack were added in C++11. Compile with -std=c++11 or newer.

Next: top()

Read (or assign) the top element without removing it with std::stack.

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

13 people found this page helpful