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 itssizeaccordingly.调整当前容器内容,替换当前内容,并调整当前容器的大小。
Thecopy assignment(1) copies all the elements fromxinto the container (withxpreserving its contents).
从x中依次复制其元素到该vector中,x中的元素依旧有效。
Themove assignment(2) moves the elements ofxinto the container (xis left in an unspecified but valid state).
从x中移动其元素到该vector中,(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 toor
destroyed.
使用该函数之前其元素依旧存储。(??)
Parameters
x
Avectorobject of the same type (i.e., with the same template parameters,TandAlloc).
一个和该vector相同类型(模版参数T和Alloc)的vector
il
Aninitializer_listobject. The compiler will automatically construct such objects
frominitializer listdeclarators.
一个初始化列表对象,编译器将自动从该对象中构造元素。
Member typevalue_typeis the type of the elements in the container, defined invectoras 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 insize.
和大小线性相关。
Iterator validity
All iterators, references and pointers related to this container before the call are invalidated.所以的迭代器,指针已经引用都将失效。
In themove assignment, iterators, pointers and references referring to elements inxare also invalidated.
在move assignment这种情况下,指向x的指针,迭代器,引用也将失效。
Data races
All copied elements are accessed.复制的元素将被访问。
Themove assignment (2)modifiesx.在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.如果抛出异常,容器依旧有效。
Ifallocator_traits::constructis not supported with the appropriate arguments for the element constructions, or ifvalue_typeis
notcopy assignable(ormove
assignablefor(2)), it causesundefined behavior.
如果内存分配器不支持元素的构造,或者值类型不支持复制赋值或者移动赋值(在(2)这种情况下),会导致为定义的行为。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-19
于GDUT
分享到:
相关推荐
vector的详解那篇 一、vector的初始化 1、默认构造函数 2、拷贝构造函数copy区间 3、指定数量和元素值的构造函数 4、指定数量的构造函数 5、拷贝构造函数 二、vector的初始化-赋值 1、.assign(beg, end) 赋值操作 2...
1.介绍一下STL,详细说明STL如何实现vector。 Answer: STL (标准模版库,Standard Template Library.它由容器算法迭代器组成。 STL有以下的一些优点: 可以方便容易地实现搜索数据或对数据排序等一系列的算法; 调试...
永不建立auto_ptr的容器 条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的期待现实一些 vector和string 条款13:...
条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的期待现实一些 vector和string 条款13:尽量使用vector和string来代替动态分配...
STL容器部分主要由头文件<vector>、、、、、和组成。 (2)算法(Algorithms)。包括各种基本算法,如比较、交换、查找、排序、遍历操作、复制、修改、移除、反转、合并等等。 STL算法部分主要由头文件和组成。...
如果将节点变为数组,将会常熟优化。vector链表实现。...拥有iterator,begin,end,rbegin,rend,operator[],push_back,pop_back,push_front,pop_front,size等海量函数以及STL函数支持,也支持RE判断!
effective stl pdf 怎么使用stl 这里几乎都有说明 条款1: 仔细选择你要的容器 条款2: 小心对“容器无关代码”的幻想 条款3: 使容器里对象的拷贝操作轻量而正确 条款4: 用empty来代替检查size是否为0 条款5: ...
》灰《《常好的STL教程Effective STL 目录 容器 条款1: 仔细选择你要的容器 条款2: 小心对“容器无关代码”的幻想 条款3: 使容器里对象的拷贝操作轻量而正确 条款4: 用empty来代替检查size是否为0 条款5: ...
1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...
1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...
STL :目的:提高代码的复用性...仿函数:行为类似函数,相当于运算符重载中的operator(),协助算法完成不同的策略 (5).适配器:修饰容器的接口或者仿函数或者迭代器接口的一种东西 (6).空间配置器:负责空间的配置及管理
vector< bool> 并不是一个STL容器,不是一个STL容器,不是一个STL容器! 首先vector< bool> 并不是一个通常意义上的vector容器,这个源自于历史遗留问题。 早在C++98的时候,就有vector这个类型了,但是因为当时...
前言 起因是这样的,昨天在查C++11的range base for loop相关的东西的时候,看到说vector是一个proxy iterator,非常的特殊,于是就好奇的...因为没有直接去给一个bit来操作,所以用operator[]的时候,正常容器返回的应
1-3-2 在派生类中实现类的基本函数,................... _ ............... 29 1-3-3 内联函数技术,........ ................................... 30 3133 ..... .. .. .. .. .. .. .. .. .. .. .. .. .....
STL提供六大组件,彼此可以组合套用 1、容器(containers):各种数据结构,如vertor,list,deque,set,map.从实现的角度来看,STL容器是一种class template ...从实现的角度来看,仿函数是一种重载了operator()
3.3 STL中的向量和表 3.3.1 迭代器 3.3.2 示例:对表使用erase 3.3.3 const_iterator 3.4 向量的实现 3.5 表的实现 3.6 栈ADT 3.6.1 栈模型 3.6.2 栈的实现 3.6.3 ...
3.3 STL中的向量和表 3.3.1 迭代器 3.3.2 示例:对表使用erase 3.3.3 const_iterator 3.4 向量的实现 3.5 表的实现 3.6 栈ADT 3.6.1 栈模型 3.6.2 栈的实现 ...
这上传的资源中包含一套我工作中常用的模板库,及不需要MFC支持的excel操作接口,导出函数调用栈(dump stack)接口,可以直接用VS2008运行TestCodeLib.sln来根据unit test来了解用法。 ⑴ 需求(requirements) 重量级...
一、stl+math综合 预备知识:map、unordered_map、priority_queue、queue、set、stack、pair、list 1.1 LeetCode533 孤独像素 II 1.2 LeetCode060 第k个排列 vector 删除指定索引的元素 vector nums; auto it = ...