std::swap(std::stack)
From cppreference.com
| ヘッダ <stack> で定義 |
||
template< class T, class Container > void swap( std::stack<T, Container>& lhs, |
(C++11以降) (C++17まで) |
|
| template< class T, class Container > void swap( std::stack<T, Container>& lhs, |
(C++17以降) | |
|
このオーバーロードは、 std::is_swappable_v<Container> が true の場合にのみ、オーバーロード解決に参加します。 |
(C++17以降) |
目次 |
[編集] パラメータ
| lhs, rhs | - | 交換するコンテナ |
[編集] 戻り値
(なし)
[編集] 計算量
基になるコンテナを交換するのと同じです。
例外
|
noexcept 指定:
noexcept(noexcept(lhs.swap(rhs))) |
(C++17以降) |
注釈
コンテナアダプタの std::swap 用オーバーロードは C++11 で導入されましたが、 C++98 でもコンテナアダプタは std::swap によって交換可能でした。そのような std::swap への呼び出しは通常、線形時間計算量ですが、より良い計算量が提供される場合もあります。
[編集] 例
このコードを実行
#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); }
出力
Alice: size=3 top=3 Bobby: size=4 top=10 -- SWAP Alice: size=4 top=10 Bobby: size=3 top=3
[編集] 関連項目
| (C++11) |
内容を交換する (public メンバ関数) |