STL vector<bool>中的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




在C语言,使用结构体定义的vector是一种数据结构,它可以存储不同类型的元素,包括int、double、string等。结构体vector的定义需要注意,它应该是全局的,否则可能会出错。下面是一个简短的程序代码示例: ```c #include <stdio.h> #include <algorithm> #include <vector> #include <iostream> using namespace std; typedef struct rect { int id; int length; int width; // 对于向量元素是结构体的,可以在结构体内部定义比较函数,按照id、length和width升序排序。 bool operator< (const rect &a) const { if(id != a.id) return id < a.id; else { if(length != a.length) return length < a.length; else return width < a.width; } } } Rect; int main() { vector<Rect> vec; Rect rect; rect.id = 1; rect.length = 2; rect.width = 3; vec.push_back(rect); vector<Rect>::iterator it = vec.begin(); cout << (*it).id << ' ' << (*it).length << ' ' << (*it).width << endl; return 0; } ``` 此代码定义了一个名为rect的结构体,其包含id、length和width三个成员变量。在main函数,创建了一个vector容器vec,并向其添加一个rect结构体。可以通过迭代器访问vector的元素,并打印出id、length和width的值。 另外,如果想要强制释放STL Vector所占用的内存,可以使用swap方法。下面是一个ClearVector函数的模板示例: ```c template <class T> void ClearVector(vector<T>& v) { vector<T> vtTemp; vtTemp.swap(v); } ``` 可以使用类似以下的代码使用ClearVector函数: ```c vector<int> v; v.push_back(1); v.push_back(3); v.push_back(2); v.push_back(4); ClearVector(v); // 或者v.swap(vector<int>()); ``` 这样就可以强制释放v所占用的内存。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [c++vector的用法详解](https://blog.csdn.net/qq_33263769/article/details/88714711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值