public member function
<vector>
swap containers (1) | void swap (vector& x); |
---|
swap elements (2) | static void swap (reference ref1, reference ref2); |
---|
swap containers (1) | void swap (vector& x); |
---|
swap elements (2) | static void swap (reference ref1, reference ref2) noexcept; |
---|
Swap containers or elements
参数
- x
- Another vector<bool> container. Sizes may differ.
- ref1, ref2
- References to elements.
reference is a member type that accesses individual elements while providing an interface that simulates a reference tobool(see reference for more info).
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
// vector<bool>::swap
#include <iostream>
#include <vector>
int main ()
{
std::vector<bool> foo;
std::vector<bool> bar;
foo.push_back(false);
foo.push_back(true);
foo.push_back(false);
bar.push_back(true);
bar.push_back(false);
foo.swap (foo[0], foo[1]);
bar.swap (bar.front(), bar.back());
foo.swap(bar);
std::cout << std::boolalpha;
std::cout << "foo contains:";
for (unsigned i=0; i<foo.size(); i++) std::cout << ' ' << foo[i];
std::cout << "\nbar contains:";
for (unsigned i=0; i<bar.size(); i++) std::cout << ' ' << bar[i];
std::cout << '\n';
return 0;
}
|
输出
foo contains: false true
bar contains: true false false
|
数据竞争
For (1), both containers are modified.
For (2), elements are modified: inboolvectors there are no guarantees on whether concurrently accessing other elements is safe.