std::swap(std::stack)
Min standard notice:
Header: <stack>
# Declarations
template< class T, class Container >
void swap( std::stack<T, Container>& lhs,
std::stack<T, Container>& rhs );
(since C++11) (until C++17)
template< class T, class Container >
void swap( std::stack<T, Container>& lhs,
std::stack<T, Container>& rhs )
noexcept(/* see below */);
(since C++17)
# Parameters
lhs, rhs: containers whose contents to swap
# Return value
(none)
# Notes
Although the overloads of std::swap for container adaptors are introduced in C++11, container adaptors can already be swapped by std::swap in C++98. Such calls to std::swap usually have linear time complexity, but better complexity may be provided.
# Example
#include <algorithm>
#include <iostream>
#include <stack>
int main()
{
std::stack<int> alice;
std::stack<int> bob;
auto print = [](const auto& title, const auto& cont)
{
std::cout << title << " size=" << cont.size();
std::cout << " top=" << cont.top() << '\n';
};
for (int i = 1; i < 4; ++i)
alice.push(i);
for (int i = 7; i < 11; ++i)
bob.push(i);
// Print state before swap
print("Alice:", alice);
print("Bobby:", bob);
std::cout << "-- SWAP\n";
std::swap(alice, bob);
// Print state after swap
print("Alice:", alice);
print("Bobby:", bob);
}