std::vector<bool,Allocator>::swap
來自 cppreference.com
< cpp | 容器 | vector bool
在標頭檔案 <vector> 中定義 |
||
static void swap( reference x, reference y ); |
(C++20 起為 constexpr) | |
Swaps the contents of x and y as if by bool b = x; x = y; y = b;.
目錄 |
[edit] 引數
x | - | std::vector <bool>::reference 要與 y 交換的值 |
y | - | std::vector <bool>::reference 要與 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 814 | C++98 | 此成員函式的描述缺失 | 已新增 |
[edit] 另請參閱
表示單個 bool 的代理類 (類) | |
交換內容 ( std::vector<T,Allocator> 的公共成員函式) | |
特化 std::swap 演算法 (函式模板) |