std::vector<bool,Allocator>::swap
From cppreference.com
< cpp | container | vector bool
| ヘッダー <vector> で定義 |
||
static void swap( reference x, reference y ); |
(C++20 以降 constexpr) | |
x と y の内容を、あたかも bool b = x; x = y; y = b; であるかのように交換します。
目次 |
[edit] パラメータ
| x | - | bool の単一要素への参照を表すプロキシクラス。y と交換される値。 |
| y | - | bool の単一要素への参照を表すプロキシクラス。 x と交換される値。 |
[edit] 戻り値
(なし)
[edit] 計算量
定数。
[edit] 例
このコードを実行
#include <iostream> #include <vector> void println(std::string_view fmt, std::vector<bool> const& vb = {}) { for (std::cout << fmt; bool const e : vb) std::cout << e << ' '; std::cout << '\n'; } int main() { println("swap elements of the same vector:"); std::vector<bool> x{1, 0}; println("before swap, x: ", x); x.swap(x[0], x[1]); // same as std::vector<bool>::swap(x[0], x[1]); println("after swap, x: ", x); println("swap elements of two different vectors:"); std::vector<bool> y{0, 0, 1}; println("before swap, x: ", x); println("before swap, y: ", y); y.swap(x[0], y[2]); // same as std::vector<bool>::swap(x[0], y[2]); println("after swap, x: ", x); println("after swap, y: ", y); }
出力
swap elements of the same vector: before swap, x: 1 0 after swap, x: 0 1 swap elements of two different vectors: before swap, x: 0 1 before swap, y: 0 0 1 after swap, x: 1 1 after swap, y: 0 0 0
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 814 | C++98 | このメンバ関数の説明が欠落していました。 | 追加された |
[edit] 関連項目
| bool の単一要素への参照を表すプロキシクラス。 (クラス) | |
| 内容を交換する ( std::vector<T,Allocator> の public メンバ関数) | |
| std::swap アルゴリズムを特殊化する (関数テンプレート) |