vector源码2(参考STL源码--侯捷):空间分配、push_back
vector源码2(参考STL源码--侯捷)
vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效
vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert
vector的构造和内存管理
vector所采用的数据结构非常简单:线性连续空间,它是由两个迭代器start和finish分别指向配置得来的连续空间中目前已被使用的范围,并以迭代器end_of_storage指向整块连续空间(含备用空间)的尾端:
class vector //详细源码可见
{
..........
protected:
typedef simple_alloc<value_type,Alloc> data_allocator; //simple_alloc是SGI STL的空间配置器
iterator start; //表示目前使用空间的头
iterator finish; //表示目前使用空间的尾
iterator end_of_storage; //表示目前可用空间的尾
..........
为了降低空间速配成本,vector的实际配置大小是原来容器大小的2倍,见下图:
#include<bits/stdc++.h>
using namespace std; int main(){
vector<int> v(,);
cout<<v.size()<<" "<<v.capacity()<<endl; //3 3
v.push_back();
cout<<v.size()<<" "<<v.capacity()<<endl; //4 6
v.push_back();v.push_back();v.push_back();
cout<<v.size()<<" "<<v.capacity()<<endl; //7 12 for(int i=;i<v.size();i++){ //3 3 3 5 6 7 8
cout<<v[i]<<' ';
}
cout<<endl; v.pop_back();v.pop_back();
cout<<v.size()<<" "<<v.capacity()<<endl; //5 12
v.pop_back();
cout<<v.size()<<" "<<v.capacity()<<endl; //4 12 vector<int>::iterator it=find(v.begin(),v.end(),);
if(it!=v.end())
v.erase(it);
cout<<v.size()<<" "<<v.capacity()<<endl; //3 12 it=find(v.begin(),v.end(),);
if(it!=v.end())
v.insert(it,,);
cout<<v.size()<<" "<<v.capacity()<<endl; //7 12 for(int i=;i<v.size();i++){ //7 7 7 7 3 3 3
cout<<v[i]<<' ';
}
cout<<endl; v.clear();
cout<<v.size()<<" "<<v.capacity()<<endl; //0 12
return ;
}vector缺省使用alloc作为空间配置器,并据此另外定义了一个data_allocator,为的是更方便以元素大小为配置单位,data_allocator::deallocate(n)表示配置n个元素空间,vector提供许多constructors一个允许我们指定空间大小及初值。
/构造函数,允许指定vector大小n和初值value
vector(size_type n,const T& value){fill_initialize(n,value);}
//填充并初始化
void fill_initialize(size_type n,const T& value){ //用于vector初始赋值
start=allocate_and_fill(n,value);
finish=start+n;
end_of_storage=finish;
}
//配置空间,并填满内存
iterator allocate_and_fill(size_type n,const T& x){
iterator result=data_allocator::allocate(n)
/*全局函数,uninitialized_fill_n()有3个参数:
迭代器first指向欲初始化空间的地址的起始处
*初始化空间的大小n
*初始化的值x*/
uninitialized_fill_n(result,n,x);
return result;
}
uninitialled_fill_n()会更根据第一参数类型来决定是使用算法fill_n()或反复调用construct();fill_n()多用于初始化多个相同数据,construct()多用于插入一个数据。
当我们利用push_back()插入元素是,vector会先检查备用空间是否充足,如果充足,尾部插入数据,否则,先扩充空间,再插入数据,这里就要考虑到重新分配、移动数据、释放空间的问题。
void push_back(const T& x){//添加元素
if(finish !=end_of_storage)//是否超出最大可容纳空间{
/*全局函数,construct()接收一个指针p和一个初值value,该函数的用途就是将
初值value设定到指针锁指的空间上。
*/
construct(finish,x);
++finish;
}
else {
insert_aux(end(),x); //vector的成员函数
}
}
insert_aux ()的具体实现如下:
template <class T,class Alloc>
void vector<T,Alloc>::insert_aux(iterator position, const T &x){
if(finish!=end_of_storage){//还有备用空间
//在备用空间起始处构造一个元素,并在vector最后一个元素值为其初值
construct(finish,*(finish-));
++finish;
T x_copy=x;
copy_backward(position,finish-,finish-);//将position到finish-2位置的元素后移到finish-1位置
*position=x_copy;
}
else{//已无备用空间
const size_type old_size=size();
const size_type len=old_size !=? *old_size:;
//以上配置原则:如果为0,则配置1,否则配置原来大小的2倍
iterator new_start=data_allocator::allocate(len);//实际配置
iterator new_finish=new_start;
try{
//将原vector元素拷贝过来
new_finish=uninitialized_copy(start,position,new_start);//迭代器start指向欲初始化空间的起始位置、迭代器position指向输入端的位置结束(前闭后开)、迭代器new_start指向输出端的起始位置
//为新元素设置初值
construct(new_finish,x);
++finish;
//将原vector的备用空间中的内容也从新拷贝过来
new_finish=uninitialized_copy(position,finish,new_finish);
}
catch(...){
destory(new_start,new_finish);
data_allocator::deallocate(new_start,len);
throw;
}
//析构释放原vector
destory(begin(),end());
deallocate();
//调整迭代器,指向新的vector
start=new_start;
finish=new_finish;
end_of_storage=new_start+len;
}
}可以看到,动态增加是直接开辟新的2倍的空间,进行数据的复制,而不是直接在原来vector后面开辟空间,因为无法保证其后面是否有足够空间,这也就说明指向原vector的所有迭代器就都失效了(查看示例)。
vector源码2(参考STL源码--侯捷):空间分配、push_back的更多相关文章
- vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...
- vector源码1(参考STL源码--侯捷):源码
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- list源码2(参考STL源码--侯捷):constructor、push_back、insert
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- vector源码(参考STL源码--侯捷):空间分配导致迭代器失效
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- STL 源码分析 (SGI版本, 侯捷著)
前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...
- STL源码剖析之序列式容器
最近由于找工作需要,准备深入学习一下STL源码,我看的是侯捷所著的<STL源码剖析>.之所以看这本书主要是由于我过去曾经接触过一些台湾人,我一直觉得台湾人非常不错(这里不涉及任何政治,仅限 ...
随机推荐
- maven打包某个分支的包
maven打某个分支的包命令: mvn clean install -Dmaven.test.skip=true -Pdevelop
- 学以致用五----centos7+python3.6.2+django2.1.1
目的,在python 3.6的基础上搭建 django 2.x 一.使用pip安装django ,但是使用pip命令的时候报错,解决方法,做软连接 ln -s /usr/local/python/bi ...
- 10-padding(内边距)
padding padding:就是内边距的意思,它是边框到内容之间的距离 另外padding的区域是有背景颜色的.并且背景颜色和内容的颜色一样.也就是说background-color这个属性将填充 ...
- dir命令详解
dir命令:DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] ...
- (转)WAMP多站点配置
转自:http://wislab.net/archives/43.html Wamp正在被广泛使用,其傻瓜式的安装配置,使得我们可以得心应手地完成以往较为烦琐的服务器环境搭建过程,直接进入到网页程序的 ...
- android 屏幕旋转 不重新加载oncreate
当手机设定了使用横屏或者竖屏的时候,还想要使用重力感应,可以设置activity属性 android:screenOrientation="sensor" 但是每次翻转屏幕,都会重 ...
- POJ2061 Subsequence 2017-05-25 19:49 83人阅读 评论(0) 收藏
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14709 Accepted: 6210 Desc ...
- sentiwordnet的简单使用
# Example line: # POS ID PosS NegS SynsetTerm#sentimentscore Desc # a 00009618 0.5 ...
- C#调用接口注意要点
在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存cookie值,再进行其他接 ...
- Duolingo 提高用户留存率的6个手段
翻译 :马玉洁 欢迎访问网易云社区,了解更多网易技术产品运营经验. 如果你用过"Duolingo"(Duolingo)这个语言教育应用程序,你就会知道它就像一款游戏. 这当然不是巧 ...
