public member function
<vector>
swap containers (1)
void swap (vector& x);
|
swap elements (2)
static void swap (reference ref1, reference ref2) noexcept;
|
Swap containers or elements
The first signature is the same as described invector::swap(seevector::swapfor
more info).
第一个swap方法的签名和vector::swap是一样的。
A static signature to swap individual elements (bits) is added onvector<bool>.
另一个静态的swap方法交换个别的元素(bits)被添加到vector<bool>中
Parameters
x
Anothervector<bool>container. Sizes may
differ.
另一个vector<bool>容器,大小可能不同。
例子:
<span style="color:#993399;">#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<bool> vb={true,false,false,true};
vector<bool> vb2{false,true};
cout<<"vb =";
for(bool b:vb){
cout<<b<<" ";
}
cout<<endl;
cout<<"vb2=";
for(bool b:vb2){
cout<<b<<" ";
}
cout<<endl;
cout<<"after swap:"<<endl;
</span><span style="color:#ff0000;">vb.swap(vb2);</span><span style="color:#993399;">
cout<<"vb =";
for(bool b:vb){
cout<<b<<" ";
}
cout<<endl;
cout<<"vb2=";
for(bool b:vb2){
cout<<b<<" ";
}
cout<<endl;
}</span>
结果截图:
ref1, ref2
References to elements.
referenceis a member type that accesses individual elements while providing an interface that
simulates a reference tobool(seereferencefor more info).
指向元素的引用。
引用是一个成员类型,使用一个接口来模仿引用来访问单个的bool元素。
例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<bool> vb={true,true,false,false};
cout<<"vb =";
for(bool b:vb){
cout<<b<<" ";
}
cout<<endl;
vb.swap(vb.front(),vb.back());
cout<<"after swap(vb.front(),vb.back())"<<endl;
cout<<"vb =";
for(bool b:vb){
cout<<b<<" ";
}
cout<<endl;
vb.swap(vb[0],vb[1]);
cout<<"after swap(vb[0],vb[1]])"<<endl;
cout<<"vb =";
for(bool b:vb){
cout<<b<<" ";
}
cout<<endl;
}
结果截图:
Return value
none
Example
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;
}
|
|
Output:
foo contains: false true
bar contains: true false false
|
Complexity
Constant.
Data races
For(1), both containers are modified.
在(1)中,所有的容器都被修改。
For(2), elements are modified: inboolvectors there are no guarantees on whether concurrently accessing other elements is safe.
在(2)中,元素被修改。在bool版本的vector中不保证同时访问他们的元素是否是安全的。
Exception safety
For(1), seevector::swap.
For(2), it never throws exceptions (no-throw guarantee).
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-20
于GDUT
分享到:
相关推荐
帮助新学习STL的童鞋们了解容器
在删除选项中仔细选择<br>条款10:注意分配器的协定和约束<br>条款11:理解自定义分配器的正确用法<br>条款12:对STL容器线程安全性的期待现实一些<br>vector和string<br>条款13:尽量使用vector和string来代替动态...
#include <vector> using namespace std; struct student { int id; string name; float height; }; int main(int argc, char* argv[]) { vector<student> onit; cout<<"请输入相关学生数据:"<<endl; ...
在C++标准中,STL被组织为下面的几个头文件:<string>、<vector>、<list>、<deque>、<algorithm>、<iterator>、<set>、<map>、<memory>、<numeric>、<queue>、<functional>、<stack>和<utility>。文件中主要介绍了...
在C++标准中,STL被组织为下面的几个头文件:<string>、<vector>、<list>、<deque>、<algorithm>、<iterator>、<set>、<map>、<memory>、<numeric>、<queue>、<functional>、<stack>和<utility>。文件中主要介绍了...
在C++标准中,STL被组织为下面的几个头文件:<string>、<vector>、<list>、<deque>、<algorithm>、<iterator>、<set>、<map>、<memory>、<numeric>、<queue>、<functional>、<stack>和<utility>。文件中主要介绍了...
STL的代码从广义上讲分为三类...在C++标准中,STL被组织为下面的13个头文件:<algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、 <memory>、<numeric>、<queue>、<set>、<stack>和<utility>。
vector< bool> 并不是一个STL容器,不是一个STL容器,不是一个STL容器! 首先vector< bool> 并不是一个通常意义上的vector容器,这个源自于历史遗留问题。 早在C++98的时候,就有vector< bool>这个类型了,但是因为...
通用工具<br>5.Standard Template Library(STL,标准模板库)<br>6.STL容器(STL Container)<br>7.STL 迭代器(STL Iterators)<br>8.STL 仿函数(functors)(又名函数对象,function objects)<br>9.STL算法...
早在C++98的时候,就有vector< bool>这个类型了,但是因为当时为了考虑到节省空间的想法,所以vector< bool>里面不是一个Byte一个Byte储存的,它是一个bit一个bit储存的! 因为没有直接去给一个bit来操作,所以用...
# std::vector<T> -- via pvector command # std::list<T> -- via plist or plist_member command # std::map<T,T> -- via pmap or pmap_member command # std::multimap<T,T> -- via pmap or pmap_member command #...
STL(Standard Template ...在C++标准中,STL被组织为下面的13个头文 件:<algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、<memory>、<numeric>、<queue>、<set>、<stack> 和<utility>。
在使用STL容器(比如map、list、vector等)的时候,是用放一个对象还是放一个对象指针,即是用vector<int>还是vector<int>,这里的vector可以换成其他的容器,int可以换成其他基本类型,也可以自定义的数据结构或类...
STL(Standard Template ...在C++标准中,STL被组织为下面的13个头文件:<algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、<memory>、<numeric>、<queue>、<set>、<stack>和<utility>。
STL容器部分主要由头文件<vector>、<list>、<deque>、<set>、< map>、<stack>和<queue>组成。 (2)算法(Algorithms)。包括各种基本算法,如比较、交换、查找、排序、遍历操作、复制、修改、移除、反转、合并...
STL Algorithms<br/><br/>9.1 Algorithm Header Files<br/><br/>9.2 Algorithm Overview<br/><br/>9.3 Auxiliary Functions<br/><br/>9.4 The <br/><br/>9.5 Nonmodifying Algorithms<br/><br/>9.6 Modifying ...
VC++精品源码<br>VC 源代码 <br><br>--------------------------------------------------------------------------------<br> <br><br><br>CatListBox_src.zip<br>CatListBox_demo.zip<br><br> 这个程序模仿...
侯捷的<<STL源码剖析>>,不错的书.使用winrar可解压
<exception> 支持异常处理,这是处理程序中可能发生的错误的一种方式 <cstdarg> 支持接受数量可变的参数的函数。即在调用函数时,可以给函数传送数量不等的数据项。它定义了宏va_arg、va_end、va_start以及va_list...