std::vector<bool,Allocator>::swap
来自 cppreference.cn
< 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 算法 (函数模板) |