STLvectorbool中的swap方法(4)


STLvectorbool中的swap方法(4)

public member function
<vector>

std::vector<bool>::swap

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 in vector::swap (see vector::swap for
more info).

第一个swap方法的签名和vector::swap是一样的。


A static signature to swap individual elements (bits) is added on vector<bool>.

另一个静态的swap方法交换个别的元素(bits)被添加到vector<bool>中


Parameters

x

Another vector<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.

reference is a member type that accesses individual elements while providing an interface that
simulates a reference to bool(see reference for 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: in bool vectors there are no guarantees on whether concurrently accessing other elements is safe.

在(2)中,元素被修改。在bool版本的vector中不保证同时访问他们的元素是否是安全的。


Exception safety

For (1), see vector::swap.

For (2), it never throws exceptions (no-throw guarantee).

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-20

于GDUT



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注