
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Stack::swap() Function
The C++ function std::stack::swap() exchanges all the elements of one stack with all the elements of another stack. The data type of both the stacks should be same in order to use this function, although the size of the stacks may differ (can be modified if necessary).
The swap() function is a simple and efficient tool for working with stack data structures in C++. It can be used to reverse the order of elements in a stack, clear the contents of a stack, and sort the elements of a stack.
Syntax
Following is the syntax for std::stack::swap() function −
void swap (stack& x) noexcept;
Parameters
x − Another stack object of same type.
Return value
This method has no return value.
Example 1
The following example shows the usage of the std::stack::swap() function with 'different sizes'.
First, we are creating two stacks 's1' and 's2'. We are then inserting values from '1 - 6' into 's1' and values from '101 - 103' into 's2' using the push() function. After that we are using the swap() function to exchange the elements of 's1' with 's2'. Finally, we are retrieving and removing the elements from both the stacks using the top() and pop() functions −
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; for (int i = 0; i < 5; ++i) s1.push(i + 1); for (int i = 0; i < 3; ++i) s2.push(100 + i); s1.swap(s2); cout << "Contents of stack s1 after swap operation" << endl; while (!s1.empty()) { cout << s1.top() << endl; s1.pop(); } cout << endl; cout << "Contents of stack s2 after swap operation" << endl; while (!s2.empty()) { cout << s2.top() << endl; s2.pop(); } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Contents of stack s1 after swap operation 102 101 100 Contents of stack s2 after swap operation 5 4 3 2 1
Example 2
Now we are trying to swap two stacks 'stack1' and 'stack2' having the same size −
#include <iostream> #include <stack> int main() { std::stack<int> stack1; std::stack<int> stack2; stack1.push(1); stack1.push(2); stack1.push(3); stack2.push(4); stack2.push(5); stack2.push(6); // Swap the two stacks stack1.swap(stack2); std::cout << "Stack 1 after swap:" << std::endl; while (!stack1.empty()) { std::cout << stack1.top() << std::endl; stack1.pop(); } std::cout << "Stack 2 after swap:" << std::endl; while (!stack2.empty()) { std::cout << stack2.top() << std::endl; stack2.pop(); } return 0; }
Output
Output of the above code is as follows −
Stack 1 after swap: 6 5 4 Stack 2 after swap: 3 2 1
Example 3
Here, we are using the swap() function on char data type to exchange the elements of two stacks 's1' and 's2' −
#include <iostream> #include <stack> using namespace std; int main(void) { stack<char> s1; stack<char> s2; s1.push('c'); s1.push('a'); s1.push('r'); s2.push('b'); s2.push('i'); s2.push('k'); s2.push('e'); s1.swap(s2); cout << "Contents of stack s1 after swap operation" << endl; while (!s1.empty()) { cout << s1.top() << endl; s1.pop(); } cout << endl; cout << "Contents of stack s2 after swap operation" << endl; while (!s2.empty()) { cout << s2.top() << endl; s2.pop(); } return 0; }
Output
If we run the above code it will generate the following output
Contents of stack s1 after swap operation e k i b Contents of stack s2 after swap operation r a c
Example 4
Now, we are trying to swap the stack 'stack1' containing 2 elements into it with the empty stack 'stack2' −
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> stack1; stack<int> stack2; stack1.push(1); stack1.push(2); stack1.swap(stack2); cout << "Contents of stack1 after swap operation:" << endl; while (!stack1.empty()) { cout << stack1.top() << endl; stack1.pop(); } cout << endl; cout << "Contents of stack2 after swap operation:" << endl; while (!stack2.empty()) { cout << stack2.top() << endl; stack2.pop(); } return 0; }
Output
Following is an output of the above code −
Contents of stack1 after swap operation: Contents of stack2 after swap operation: 2 1