一、vector定义摘要:

template <class T, class Alloc = alloc>
class vector {
public:typedef T                   value_type;typedef value_type*         pointer;typedef const value_type*   const_pointer;typedef value_type*         iterator;typedef const value_type*   const_iterator;typedef value_type&         reference;typedef const value_type&   const_reference;typedef size_t              size_type;typedef ptrdiff_t           difference_type;
protected:typedef simple_alloc<value_type, Alloc> data_allocator;iterator start;iterator finish;iterator end_of_storage;
public:iterator begin() { return start; }const_iterator begin() const { return start; }iterator end() { return finish; }const_iterator end() const { return finish; }size_type size() const { return size_type(end() - begin()); }size_type max_size() const { return size_type(-1) / sizeof(T); }size_type capacity() const { return size_type(end_of_storage - begin()); }bool empty() const { return begin() == end(); }reference operator[](size_type n) { return *(begin() + n); }const_reference operator[](size_type n) const { return *(begin() + n); }vector() : start(0), finish(0), end_of_storage(0) {}vector(size_type n, const T & value) { fill_initialize(n, value); }vector(int n, const T & value) { fill_initialize(n, value); }vector(long n, const T & value) { fill_initialize(n, value); }explicit vector(size_type n) { fill_initialize(n, T()); }
};

二、构造函数 :

vector() : start(0), finish(0), end_of_storage(0) {}
vector(size_type n, const T& value) { fill_initialize(n, value); }template <class T, class Alloc>
void vector<T, Alloc>::::fill_initialize(size_type n, const T& value)
{start = allocate_and_fill(n, value);finish = start + n;end_of_storage = finish;
}template <class T, class Alloc>
iterator vector<T, Alloc>::allocate_and_fill(size_type n, const T& x)
{iterator result = data_allocator::allocate(n);uninitialized_fill_n(result, n, x);return result;
}vector(const vector<T, Alloc>& x)
{start = allocate_and_copy(x.end() - x.begin(), x.begin(), x.end());finish = start + (x.end() - x.begin());end_of_storage = finish;
}iterator allocate_and_copy(size_type n, const_iterator first, const_iterator last)
{iterator result = data_allocator::allocate(n);uninitialized_copy(first, last, result);return result;
}

三、析构函数:

template <class T, class Alloc>
vector<T, Alloc>::~vector()
{destroy(start, finish);deallocate();
}template <class T, class Alloc>
void vector<T, Alloc>::deallocate()
{if (start)data_allocator::deallocate(start, end_of_storage - start);
}

四、push_back

template <class T, class Alloc>
void vector<T>::push_back(const T& x) //重点函数
{if (finish != end_of_storage) //还有备用空间{construct(finish, x);  //全局函数++finish;}elseinsert_aux(end(), x); //已经没有备用空间
}template <class T, class Alloc>
void vector<T>::insert_aux(iterator position, const T& x)
{if (finish != end_of_storage) //还有备用空间{construct(finish, *(finish - 1));++finish;T x_copy = x;std::copy_backward(position, finish - 2, finish - 1);*position = x_copy;}else //没有备用空间了{const size_type old_size = size();const size_type len = old_size != 0 ? 2 * old_size : 1;//以上配置原则:如果原大小为0,则配置1//如果原大小不为0,则配置原大小2倍//前半段用来放置原数据,后半段放置新数据iterator new_start = data_allocator::allocate(len);iterator new_finish = new_start;new_finish = uninitialized_copy(start, position, new_start);construct(new_finish, x);++new_finish;new_finish = uninitialized_copy(position, finish, new_finish);destroy(begin(), end());deallocate();start = new_start;finish = new_finish;end_of_storage = new_start + len;}
}

vetor的元素操作:pop_back、erase、clear、insert

void pop_back()
{--finish;         //将尾标记往前移动一格,表示将放弃尾端元素destroy(finish); //析构函数
}iterator erase(iterator first, iterator last)
{iterator i = copy(last, finish, first);destroy(i, finish);finish = finish - (last - first);return first;
}iterator erase(iterator position)
{if (position + 1 != end())copy(position + 1, finish, position);--finish;destroy(finish);return position;
}void clear() { erase(begin(), end()); }template <class T, class Alloc>
void vector<T, Alloc>::insert(iterator position, size_type n, const T & x)
{if (n != 0) //当n!= 0才进行以下所有操作{if (size_type(end_of_storage - finish) >= n) //备用空间大于等于新增元素{T x_copy = x;const size_type elems_after = finish - position; //计算插入点之后的现有元素个数iterator old_finish = finish;if (elems_after > n) //"插入点之后的现有元素个数"大于"新增元素个数"{uninitialized_copy(finish - n, finish, finish);finish += n;copy_backward(position, old_finish - n, old_finish);fill(position, position + n, x_copy);}else //"插入点之后的现有元素个数"小于等于"新增元素个数"{uninitialized_fill_n(finish, n - elems_after, x_copy);finish += n - elems_after;uninitialized_copy(position, old_finish, finish);finish += elems_after;fill(position, old_finish, x_copy);}}else{//备用空间小于"新增元素个数"(那就必须配置额外的内存)//首先决定新长度:旧长度的两倍,或旧长度+新增元素个数const size_type old_size = size();const size_type len = old_size + max(old_size, n);iterator new_start = data_allocator::allocate(len);iterator new_finish = new_start;new_finish = uninitialized_copy(start, position, new_start);new_finish = uninitialized_fill_n(new_finish, n, x);new_finish = uninitialized_copy(position, finish, new_finish);destroy(start, finish);deallocate();start = new_start;finish = new_finish;end_of_storage = new_start + len;}}
}

五、resize、reverse

void resize(size_type new_size, const T& x)
{if (new_size < size())erase(begin() + new_size, end());elseinsert(end(), new_size - size(), x);
}
void resize(size_type new_size) { resize(new_size, T()); }void reserve(size_type n)
{if (capacity() < n) {const size_type old_size = size();iterator tmp = allocate_and_copy(n, start, finish);destroy(start, finish);deallocate();start = tmp;finish = tmp + old_size;end_of_storage = start + n;}
}iterator allocate_and_copy(size_type n, const_iterator first, const_iterator last)
{iterator result = data_allocator::allocate(n);uninitialized_copy(first, last, result);return result;
}

六、 swap

 void swap(vector<T, Alloc>& x)
{__STD::swap(start, x.start);__STD::swap(finish, x.finish);__STD::swap(end_of_storage, x.end_of_storage);}

vector源码剖析相关推荐

  1. c++ vector 赋值_Vector 源码剖析

    喜欢请点关注 Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长. Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是相对 ...

  2. boost源码剖析之:多重回调机制signal(下)

    boost源码剖析之:多重回调机制signal(下) 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba) 在本文的上篇中,我们大刀阔斧的剖析了signal的架构.不过还有 ...

  3. boost源码剖析之:多重回调机制signal(上)

    boost源码剖析之:多重回调机制signal(上) 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba) boost库固然是技术的宝库,却更是思想的宝库.大多数程序员都知 ...

  4. boost源码剖析之:boost::multi_array

    boost源码剖析之:boost::multi_array 谢轩 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba) Note: 并非新作,是以前和老朋友谢轩写的,也可以 ...

  5. STL源码剖析---deque

    一.deque的中控器       deque是连续空间(至少逻辑上看来如此),连续线性空间总令我们联想到array或vector.array无法成长,vector虽可成长,却只能向尾端成长,而且其所 ...

  6. STL源码剖析---list

    相较于vector的连续线性空间,list就显得复杂许多,它的好处是每次插入或删除一个元素,就配置或释放一个元素空间.因此,list对于空间的运用有绝对的精准,一点也不浪费.而且,对于任何位置的元素插 ...

  7. sarscape 将dem文件转化成stl_STL源码剖析 阅读笔记(一)介绍

    一.学习动机 对C++的理解:最近因为工作原因需要重新对C++进行学习,而上一次系统.全局的学习C++已经是在本科时期了,然后是读研期间的第一年学过一点皮毛,后来对C++的学习都是边用边学.纵然这样已 ...

  8. STL源码剖析面试问题

    当vector的内存用完了,它是如何动态扩展内存的?它是怎么释放内存的?用clear可以释放掉内存吗?是不是线程安全的? vector内存用完了,会以当前size大小重新申请2* size的内存,然后 ...

  9. STL源码剖析 数值算法 copy 算法

    copy复制操作,其操作通过使用assignment operator .针对使用trivial assignment operator的元素型别可以直接使用内存直接复制行为(使用C函数 memove ...

最新文章

  1. 卓越领袖的的32条经营管理思想
  2. R语言ggplot2可视化为长文本轴标签自动换行美化可视化结构实战:Wrap long text axis labels
  3. Linux下的编译(环境是centos6.8 gcc 4.4.7)
  4. OneHotEncoder独热编码和 LabelEncoder标签编码——机器学习
  5. Spring-RedisTemplate原理
  6. 富文本NSMutableAttributedString用法大全
  7. FPGA笔记(1)-逻辑代数与逻辑电路基础
  8. boost::mpi模块实现scan集合的测试
  9. 如何理解卷积神经网络(CNN)中的卷积和池化?
  10. javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
  11. ionic3 自动创建启动背景splash以及图标icon
  12. 字典 学生成绩等级_python-列表及字典进阶
  13. 黑客马拉松 招募_举办第一次黑客马拉松的提示
  14. Spring-Data-JPA--增删改查2——自定义接口查询
  15. POJ3264Balanced Lineup(最基础的线段树)
  16. pytorch学习笔记(三十三):梯度下降和随机梯度下降
  17. 请写一个java程序实现线程连接池功能_请写一个java程序实现线程连接池功能
  18. 一步一步使用标c编写跨平台图像处理库_让一个图像变成反向图像
  19. 微信支付将为O2O画上句号
  20. java set retainall_Java的Set集合中的retainAll()方法

热门文章

  1. Linux SPI框架
  2. android ImageView 之 android:scaleTye=
  3. PowerShell_9_零基础自学课程_9_高级主题:静态类和类的操作
  4. php 获取每年的节假日,shell获取每年农历节日的日期
  5. 华大单片机m4内核的滴答定时器_微处理器、单片机及其外设,处理还是控制?...
  6. 荣耀鸿蒙系统开机动画,荣耀赵明:鸿蒙系统首发设备欲屏蔽开机广告
  7. 台达b3伺服modbus通讯_【数控系统】台达伺服压机控制灵活 精准压合满足各种工序需求...
  8. python随机数生成的方法_python生成随机数的方法
  9. windows 文件对话框
  10. python3 Connection aborted.', RemoteDisconnected('Remote end closed connection without response'