STL vector中的operator=方法(30)

public member function
<vector>

std::vector::operator=

copy (1)
vector& operator= (const vector& x);
move (2)
vector& operator= (vector&& x);
initializer list (3)
vector& operator= (initializer_list<value_type> il);
Assign content
Assigns new contents to the container, replacing its current contents, and modifying its size accordingly. 调整当前容器内容,替换当前内容,并调整当前容器的大小。
The  copy assignment (1) copies all the elements from  x into the container (with  x preserving its contents).
从x中依次复制其元素到该vector中,x中的元素依旧有效。

The  move assignment (2) moves the elements of  x into the container ( x is left in an unspecified but valid state).
从x中移动其元素到该vector中,(x依旧有效但不指定是否离开?)


The  initializer list assignment (3) copies the elements of  il into the container.
从初始化列表中复制元素到容器中。

The container preserves its  current allocator, except if the  allocator traits indicate that  x's allocator should  propagate. This  allocator is used (through its  traits) to  allocate and  deallocate storage if a reallocation happens, and to  construct or  destroy elements, if needed.
容器保持现有的内存分配器,除非分配器的特性指明了应该继承x的内存分配器,那么将使用其内存分配器去分配及释放内存,如果发生重分配,或者构造,析构需要的时候,就使用该分配器。

相应例子及结果截图:
(1)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> v1={1,2,3,4};
	vector<int> v2={11,12,13,14};
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;
	cout<<"v2=";
	for_each(v2.begin(),v2.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;
	v1=v2;
	cout<<"after v1=v2:"<<endl;
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;
	cout<<"v2=";
	for_each(v2.begin(),v2.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;


}
截图:


(2)
#include <iostream>
#include <vector>
#include <algorithm>
#include <initializer_list>
using namespace std;

vector<int> returnVector(){
	vector<int> v2={88,99,111};
	cout<<"v2.data="<<v2.data()<<endl;
	return v2;
}

int main()
{
	vector<int> v1={1,2,3,4};
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;	
	cout<<"v1.data="<<v1.data()<<endl;
	v1=returnVector();
	cout<<"after v1=returnVector():"<<endl;
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;
	cout<<"v1.data="<<v1.data()<<endl;
	


}
运行截图:


(3)
#include <iostream>
#include <vector>
#include <algorithm>
#include <initializer_list>
using namespace std;
int main()
{
	vector<int> v1={1,2,3,4};
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;	
	v1={999,888,777,666};
	cout<<"after v1={999,888,777,666}:"<<endl;
	cout<<"v1=";
	for_each(v1.begin(),v1.end(),[](const int &m){cout<<m<<" ";});
	cout<<endl;
	


}
结果截图:



Any elements held in the container before the call are either  assigned to or  destroyed.
使用该函数之前其元素依旧存储。(??)

Parameters

x
vector object of the same type (i.e., with the same template parameters, T and Alloc).
一个和该vector相同类型(模版参数T和Alloc)的vector
il
An initializer_list object. The compiler will automatically construct such objects from initializer list declarators.
一个初始化列表对象,编译器将自动从该对象中构造元素。
Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template parameter (T).

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// vector assignment
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> foo (3,0);
  std::vector<int> bar (5,0);

  bar = foo;
  foo = std::vector<int>();

  std::cout << "Size of foo: " << int(foo.size()) << '\n';
  std::cout << "Size of bar: " << int(bar.size()) << '\n';
  return 0;
}


Output:
Size of foo: 0
Size of bar: 3

Complexity

Linear in size.
和大小线性相关。

Iterator validity

All iterators, references and pointers related to this container before the call are invalidated. 所以的迭代器,指针已经引用都将失效。

In the move assignment, iterators, pointers and references referring to elements in x are also invalidated.
在move assignment这种情况下,指向x的指针,迭代器,引用也将失效。

Data races

All copied elements are accessed. 复制的元素将被访问。
The move assignment (2) modifies x.
move assignment (2) 中x将被修改.
The container and all its elements are modified.
容器及所有元素都将被修改。

Exception safety

Basic guarantee: if an exception is thrown, the container is in a valid state. 如果抛出异常,容器依旧有效。
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if value_type is not copy assignable (or move assignable for (2)), it causes undefined behavior.
如果内存分配器不支持元素的构造,或者值类型不支持复制赋值或者移动赋值(在(2)这种情况下),会导致为定义的行为。

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

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

2014-8-19

于GDUT





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值