(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::stack| Member functions | ||||
| Element access | ||||
| Capacity | ||||
| Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
(C++11) | ||||
| Non-member functions | ||||
swap(std::stack) (C++11) | ||||
| Helper classes | ||||
(C++11) | ||||
(C++23) | ||||
| Deduction guides(C++17) |
Defined in header <stack> | ||
template<class T,class Container> void swap(std::stack<T, Container>& lhs, | (since C++11) (until C++17) | |
template<class T,class Container> void swap(std::stack<T, Container>& lhs, | (since C++17) (constexpr since C++26) | |
This overload participates in overload resolution only ifstd::is_swappable_v<Container> istrue. | (since C++17) |
Contents |
| lhs, rhs | - | containers whose contents to swap |
Same as swapping the underlying containers.
noexcept specification: noexcept(noexcept(lhs.swap(rhs))) | (since C++17) |
Although the overloads ofstd::swap for container adaptors are introduced in C++11, container adaptors can already be swapped bystd::swap in C++98. Such calls tostd::swap usually have linear time complexity, but better complexity may be provided.
#include <algorithm>#include <iostream>#include <stack> int main(){std::stack<int> alice;std::stack<int> bob; auto print=[](constauto& title,constauto& 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);}
Output:
Alice: size=3 top=3Bobby: size=4 top=10-- SWAPAlice: size=4 top=10Bobby: size=3 top=3
(C++11) | swaps the contents (public member function)[edit] |