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. 网站开发综合技术 三 JavaScript的DOM操作

    第3部分 JavaScript的DOM操作 1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Windows对象操作 ...

  2. 窗体基础WINFORM

    winform 1.窗体: 造窗体界面: 窗体设计界面: 窗体类名不能重复! 属性: acceptbutton:回车是默认点击按钮 cancelbutton:按esc按键式默认的按钮 backcolo ...

  3. Spark性能优化指南-高级篇(spark shuffle)

    Spark性能优化指南-高级篇(spark shuffle) 非常好的讲解

  4. MySQL的主从复制(windows)

    在我们实际的开发中,当系统业务到达一定的程度,可能数据库会到达一定的瓶颈,但实际开发中最容易到达数据库瓶颈的应该是数据库的读性能,一般的业务大多都是读多写少,我们可以通过提高读的性能来提高数据库的整体 ...

  5. CSS垂直居中和水平居中的几种方法

    垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...

  6. Windows 2008 防火墙开放端口

    当我们使用新服务器架设新主机时,经常会遇到网站无法访问的情况,当问及客服时,经常会告知,操作系统默认不打开80端口,请先确定80是否打开并确定没有被占用.那么,我们该如何打开80端口呢? 方法/步骤 ...

  7. 使用WAS寄宿net.tcp WCF服務

    首先添加Windows Features 確保打開以下服務 Net.Tcp Listener Adapter Net.Tcp Port Sharing Service Windows Process ...

  8. 云服务IaaS,PaaS,SaaS

    IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a-service SaaS:软件服务,Software-as-a-serv ...

  9. 【CImg】基本像素操作

    继openCV之后接触的又一个C++视觉库——短小精干的CImg 刚开始接触的时候真的是..几乎无从下手,网上资料比较少,但发现有一篇非常有用的中文手册:http://wenku.baidu.com/ ...

  10. mfc按钮悬停显示文字

    .h CToolTipCtrl m_toopTip; .cpp oninitdialog void CDlgDwgLibMan::InitTooltips(){ EnableToolTips(); m ...