typedef int size_type;
/*
vector维护的是一个连续线性空间,提供的迭代器是Random Access Iterators即普通指针
*/
template <class T,class Alloc=alloc>
class vector
{
protected:
iterator start;//目前使用空间的头
iterator finish;//目前使用空间的尾
iterator end_of_storage;//目前可用空间的尾
public:
void push_back(const T& x);
void insert_aux(iterator position, const T& x);
int size()const{ return int(end() - begin()); }
}; /*
vector动态增加大小,并不是在原空间之后开辟新空间,因为无法保证原空间之后尚有可供配置的空间,而是以
原大小的两倍另外配置一块较大空间,然后将原内容拷贝过来,然后才开始在原内容之后构造新元素,并释放原空间
*/
template<class T,class Alloc=alloc>
void vector::push_back(const T& x)
{
if (finish != end_of_storage)
{
construct(finish, x);
++finish;
}
else//已无备用空间
insert_aux(end(), x);
} template<class T, class Alloc = alloc>
void vector<T, Alloc>::insert_aux(iterator position, const T& x)
{
const size_type old_size = size();
const size_type len = old_size != ? * old_size : ;
/*
配置原则:若原空间大小为0,则配置一个元素大小,否则配置原大小的两倍
前半段用来放置原数据,后半段用来放置新数据
*/
//data_allocator为vector专有的空间配置器
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
//将原vector的内容拷贝到新vector
new_finish = uninitialized_copy(start, position, new_start);
//为新元素设定初值x
construct(new_finish, x);
++new_finish; //析构并释放原vector
destroy(begin(), end());
deallocate(); //调整迭代器
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}

vector实现的更多相关文章

  1. c++ vector 使用

    1. 包含一个头文件: 1 #include <vector> 2. 申明及初始化: std::vector<int> first; // empty vector of in ...

  2. Vector Tile

    Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...

  3. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  4. ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量

    当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...

  5. Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  6. C++使用vector

    #include <iostream> #include <string> #include <vector> using namespace std; void ...

  7. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. C++ 数组array与vector的比较

    转:http://blog.csdn.net/yukin_xue/article/details/7391897 1. array 定义的时候必须定义数组的元素个数;而vector 不需要: 且只能包 ...

  9. vector定义初始化

    头文件 #include<vector> using std::vector; vector<T> v1; vector<T> v2(v1); vector< ...

  10. vector迭代器用法

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> ...

随机推荐

  1. MySQL 四种事务隔离级别详解及对比--转

    http://www.jb51.net/article/100183.htm 接的隔离级别.它的语法如下: ? 1 SET [SESSION | GLOBAL] TRANSACTION ISOLATI ...

  2. Tomcat无法clean,无法remove部署的项目

    错误: 对部署在Tomcat下的项目进行clean操作,总是提示Could not load the Tomcat server configuration,错误信息如图: 解决: 原来是将Serve ...

  3. 联想 Z5 Pro(L78031)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖ZUI 10.0.355

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  4. text-shadow的用法详解

    1.兼容性:text-shadow 和 box-shadow 这两个属性在主流现代浏览器上得到了很好的支持( > Chrome 4.0, > Firefox 3.5, > Safar ...

  5. 3星|《OKR:源于英特热和谷歌的目标管理利器》:OKR原理、实施手册、实施过的公司的访谈

    OKR原理与实施手册,另外附了几家实施过OKR的公司的访谈. 书中表述的OKR思想,我认为是这两点: 1:始终聚焦在最重要的目标上: 2:不以OKR为考核员工的指标: Kindle电子版排版有小缺陷, ...

  6. linux nohup & 简单使用

    线上通常nohup配合&启动程序,同时免疫SIGINT和SIGHUP信号,从而保证程序在后台稳定运行 & 1.后台运行,输出默认到屏幕 2.免疫SIGINT信号,比如Ctrl+c不会杀 ...

  7. (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版

    http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...

  8. codeforces_333B_水过

    B. Chips time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. Java中XML数据

    Java中XML数据 XML解析——Java中XML的四种解析方式 XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解 ...

  10. 【转载】jxl的使用总结(java操作excel)

    jxl.jar是通过java操作excel表格的工具类库: 链接:https://pan.baidu.com/s/1AAT_eA_Q47zFeQohap6eQg 提取码:777b 1:通过模拟实现创建 ...