std::priority_queue<T,Container,Compare>:: swap
From cppreference.net
<
cpp
|
container
|
priority queue
|
void
swap
(
priority_queue
&
other
)
noexcept
(
/* see below */
)
;
|
(C++11以降) | |
other
. Effectively calls
using std::swap; swap(c, other.c); swap(comp, other.comp);
目次 |
パラメータ
| other | - | コンテンツを交換するためのコンテナアダプタ |
戻り値
(なし)
例外
|
noexcept
指定:
noexcept
(
noexcept
(
swap
(
c, other.
c
)
)
&&
noexcept
(
swap
(
comp, other.
comp
)
)
)
上記の式において、識別子
|
(C++11以降)
(C++17まで) |
|
noexcept
指定:
noexcept
(
std::
is_nothrow_swappable_v
<
Container
>
&&
std:: is_nothrow_swappable_v < Compare > ) |
(C++17以降) |
計算量
基となるコンテナと同じ(通常は定数)。
注記
一部の実装(例: libc++)では、C++11以前のモードでも拡張機能として
swap
メンバー関数を提供しています。
例
このコードを実行
#include <iostream> #include <concepts> #include <functional> #include <queue> #include <string> #include <string_view> #include <vector> template<typename Adaptor> requires (std::ranges::input_range<typename Adaptor::container_type>) void print(std::string_view name, const Adaptor& adaptor) { struct Printer : Adaptor // to use protected Adaptor::Container c; { void print(std::string_view name) const { std::cout << name << " [" << std::size(this->c) << "]: "; for (auto const& elem : this->c) std::cout << elem << ' '; std::cout << '\n'; } }; static_cast<Printer const&>(adaptor).print(name); } int main() { std::vector<std::string> v1{"1","2","3","4"}, v2{"Ɐ","B","Ɔ","D","Ǝ"}; std::priority_queue s1(std::less<>(), std::move(v1)); std::priority_queue s2(std::less<>(), std::move(v2)); print("s1", s1); print("s2", s2); s1.swap(s2); print("s1", s1); print("s2", s2); }
出力:
s1 [4]: 4 3 2 1 s2 [5]: Ǝ D Ɔ B Ɐ s1 [5]: Ǝ D Ɔ B Ɐ s2 [4]: 4 3 2 1
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2456 | C++11 |
the
noexcept
specification is ill-formed
|
made to work |
関連項目
|
(C++11)
|
std::swap
アルゴリズムを特殊化する
(関数テンプレート) |