Difference between revisions of "cpp/container/stack/swap2"
From cppreference.com
(template has been moved) |
m (langlinks) |
||
(3 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/container/swap2|stack}} | + | {{include page|cpp/container/swap2|stack}} |
+ | |||
+ | {{langlinks|cs|de|es|fr|it|ja|pl|pt|ru|tr|zh}} |
Latest revision as of 07:10, 20 November 2021
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) | |
This overload participates in overload resolution only if std::is_swappable_v<Container> is true. |
(since C++17) |
Contents |
[edit] Parameters
lhs, rhs | - | containers whose contents to swap |
[edit] Return value
(none)
[edit] Complexity
Same as swapping the underlying containers.
Exceptions
noexcept specification:
noexcept(noexcept(lhs.swap(rhs))) |
(since C++17) |
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.
[edit] Example
Run this code
#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); }
Output:
Alice: size=3 top=3 Bobby: size=4 top=10 -- SWAP Alice: size=4 top=10 Bobby: size=3 top=3
[edit] See also
(C++11) |
swaps the contents (public member function) |