std:: swap (std::stack)
|
ヘッダーで定義
<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以降)
(constexprはC++26以降) |
|
|
このオーバーロードは、 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("Alice:", alice); print("Bobby:", bob); std::cout << "-- SWAP\n"; std::swap(alice, bob); // 交換後の状態を表示 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)
|
内容を交換する
(公開メンバ関数) |