std:: swap (std::priority_queue)
|
ヘッダーで定義
<queue>
|
||
|
template
<
class
T,
class
Container,
class
Compare
>
void
swap
(
std::
priority_queue
<
T, Container, Compare
>
&
lhs,
|
(C++11以降)
(C++17まで) |
|
|
template
<
class
T,
class
Container,
class
Compare
>
void
swap
(
std::
priority_queue
<
T, Container, Compare
>
&
lhs,
|
(C++17以降)
(constexpr C++26以降) |
|
|
このオーバーロードは、 std:: is_swappable_v < Container > および std:: is_swappable_v < Compare > がともに 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 <queue> int main() { std::priority_queue<int> alice; std::priority_queue<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)
|
内容を交換する
(公開メンバ関数) |