vector源码1(参考STL源码--侯捷)

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的更多相关文章

  1. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  2. vector源码1(参考STL源码--侯捷):源码

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  3. list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  4. list源码2(参考STL源码--侯捷):constructor、push_back、insert

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  5. list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  6. 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 ...

  7. vector源码(参考STL源码--侯捷):空间分配导致迭代器失效

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  8. STL 源码分析 (SGI版本, 侯捷著)

    前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...

  9. STL源码剖析之序列式容器

    最近由于找工作需要,准备深入学习一下STL源码,我看的是侯捷所著的<STL源码剖析>.之所以看这本书主要是由于我过去曾经接触过一些台湾人,我一直觉得台湾人非常不错(这里不涉及任何政治,仅限 ...

随机推荐

  1. AngularJS封装UEditor

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  2. ACtiveMQ中间件-消息的接收和发送

    一.发送消息基本步骤 (1)启动ActiveMQ的的activemq.bat批处理文件或BrokerService节点 (2)创建连接使用的工厂类ActiveMQConnectionFactory通过 ...

  3. 个人理解的int数组和char数组

    char数组中不论是一维还是二维的,在程序执行时每一块的分离依据都是以提供的起始地址到'\0'为一个处理的字符串.所以关于char[]的函数都是只提供相应起始地址作为形参就可以. char[]互相交换 ...

  4. openresty + lua 3、openresty http 调用

    http 的话,openresty 已经集成,ng 的话,自己引入即可. github 地址:https://github.com/pintsized/lua-resty-http github 里提 ...

  5. hdu 5093 放置战舰 二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=5093 给定一个MxN大小的图,有3种点,冰山.浮冰.海.现在希望能在图中放置尽可能多的船.船的四个方向上不能有其 ...

  6. Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力

    http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...

  7. bootstrap2.1相关文档

    本节课我们主要学习一下 Bootstrap表格和按钮功能,通过内置的 CSS定义,显示各种丰富的效果. 一.表格 Bootstrap提供了一些丰富的表格样式供开发者使用. 1.基本格式 //实现基本的 ...

  8. Python自动化开发 - AJAX

    一  AJAX预备知识:json进阶 1.1 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. JSON是用字符串来表示Javascript对象 json ...

  9. ceph osd 批量删除

    ceph osd 批量删除,注意删除的是当前节点正在使用的osd,数据删除有风险,所以最后一步没有去format磁盘,给你留下一剂后悔药. #!/bin/bash osd_list=`mount|gr ...

  10. Windows远程桌面连接ubuntu 16

    一.安装Xrdp Windows远程桌面使用的是RDP协议,所以ubuntu上就要先安装Xrdp,在ubuntu软件中心搜索xrdp安装. 安装xrdp的同时会自动安装vnc4server,xbase ...