STL vector中的swap方法(33)

原文地址:http://www.cplusplus.com/reference/vector/vector/swap/
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>
运行结果:

STL vector中的swap方法(33)_第1张图片

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


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,优化重写了该方法,行为也相似。


  • C++98
  • C++11
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; }
Edit & Run


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




你可能感兴趣的:(vector,STL,STL)