std::vector::operator=
Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.调整当前容器内容,替换当前内容,并调整当前容器的大小。
从x中依次复制其元素到该vector中,x中的元素依旧有效。
从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的内存分配器,那么将使用其内存分配器去分配及释放内存,如果发生重分配,或者构造,析构需要的时候,就使用该分配器。
#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; }
截图:
#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; }
运行截图:
#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
- A 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
|
|
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