STL vector中的swap方法(33)

public member function
<vector>

std::vector::swap

void swap (vector& x);
Swap content
Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.

与x交换其内容,x的类型要与该vector一样,大小可以不同。


After the call to this member function, the elements in this container are those which were in x before the call, and the elements of are those which were in this. All iterators, references and pointers remain valid for the swapped objects.

调用该函数之后,存储在该容器内的元素是那些本来在x中的元素,x中的元素是原本存储在该容器中的元素(即交换内容),所有的迭代器,引用以及指针在交换后依旧有效。

例子:

<span style="color:#993399;">#include <iostream>
#include <vector>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> v1={1,2,3,4};
	vector<int> v2={111,222,333};
	cout<<"v1.data="<<v1.data()<<endl;
	cout<<"v1:";
	for(auto it=v1.begin();it!=v1.end();++it)
		cout<<*it<<" ";
	cout<<endl;

	cout<<"v2.data="<<v2.data()<<endl;
	cout<<"v2:";
	for(auto it=v2.begin();it!=v2.end();++it)
		cout<<*it<<" ";
	cout<<endl;
	
	auto i1=v1.begin();
	auto i2=v2.begin();
	
	cout<<"i1="<<*i1<<endl<<"i2="<<*i2<<endl;
	
	v1.swap(v2);
	cout<<"after v1.swap(v2)"<<endl;
	
	cout<<"v1.data="<<v1.data()<<endl;
	cout<<"v1:";
	for(auto it=v1.begin();it!=v1.end();++it)
		cout<<*it<<" ";
	cout<<endl;

	cout<<"v2.data="<<v2.data()<<endl;
	cout<<"v2:";
	for(auto it=v2.begin();it!=v2.end();++it)
		cout<<*it<<" ";
	cout<<endl;
		
	cout<<"i1="<<*i1<<endl<<"i2="<<*i2<<endl;



}
</span>
运行结果:


居然还真的都是保持有效,真是少见啊,以前都是失效,难得有个保持有效的。


Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function.

需要注意的是在algorithm中,有一个非成员方法swap,优化重写了该方法,行为也相似。


Whether the container allocators are also swapped is not defined, unless in the case the appropriate allocator traits indicate explicitly that they shall propagate.
不肯定容器的内存分配器是否也会交换,除非内存分配器的特性特别指明其内存分配器应该被继承。
The  bool  specialization of  vector  provides an additional overload for this function (see  vector<bool>::swap ).
vector<bool>::swap特别重写了该方法。

Parameters

x

Another vector container of the same type (i.e., instantiated with the same template parameters, T and Alloc) whose content is swapped with that of this container.

x是另一个将被交换的同类型的vector容器。


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
// swap vectors
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> foo (3,100);   // three ints with a value of 100
  std::vector<int> bar (5,200);   // five ints with a value of 200

  foo.swap(bar);

  std::cout << "foo contains:";
  for (unsigned i=0; i<foo.size(); i++)
    std::cout << ' ' << foo[i];
  std::cout << '\n';

  std::cout << "bar contains:";
  for (unsigned i=0; i<bar.size(); i++)
    std::cout << ' ' << bar[i];
  std::cout << '\n';

  return 0;
}


Output:
foo contains: 200 200 200 200 200 
bar contains: 100 100 100 

Complexity

Constant.

Iterator validity

All iterators, pointers and references referring to elements in both containers remain valid, and are now referring to the same elements they referred to before the call, but in the other container, where they now iterate.

两个容器中所有的迭代器,指针以及引用都保持有效。


Note that the end iterators do not refer to elements and may be invalidated.

需要注意的是end得到的迭代器是不关联到实际的元素的,这可能会失效。


Data races

Both the container and x are modified.
No contained elements are accessed by the call (although see iterator validity above).

x以及该容器都将被修改。

容器的元素不会被访问。


Exception safety

If the allocators in both vectors compare equal, or if their allocator traits indicate that the allocators shall propagate, the function never throws exceptions (no-throw guarantee).


Otherwise, it causes undefined behavior.


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

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

2014-8-19

于GDUT




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值