vector的简易实现整理自《数据结构与算法分析–C++描述(第3版)》3.4节“向量的实现”。详细可参考《STL源码分析》4.2节。

  具体实现代码如下:

 #ifndef VECTOR_H
#define VECTOR_H #include <iostream>
using namespace std; template<class T>
class Vector
{
private:
int theSize;
int theCapacity;
T *objects; public:
typedef T* iterator;
typedef const T* const_iterator;
enum{ SPARE_CAPACITY = }; public:
explicit Vector(int initSize = ); //用explicit避免隐式类型转换
~Vector();
Vector(const Vector<T>& rhs);
const Vector<T>& operator=(const Vector<T>& rhs); void resize(int newSize);
void reserve(int newCapacity); T& operator[](int index);
const T& operator[](int index) const; bool empty() const;
int size() const;
int capacity() const; void push_back(const T& x);
void pop_back();
const T& back() const; iterator begin();
const_iterator begin() const; iterator end();
const_iterator end() const; }; template<class T>
Vector<T>::Vector(int initSize = ) : theSize(initSize), theCapacity(initSize + SPARE_CAPACITY)
{
objects = new T[theCapacity];
} template<class T>
Vector<T>::~Vector()
{
delete []objects;
} template<class T>
Vector<T>::Vector(const Vector<T>& rhs) : objects(nullptr)
{
operator = (rhs);
} template<class T>
const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
{
if (this != &rhs)
{
delete[]objects;
theSize = rhs.size();
theCapcity = rhs.theCapacity; // 注意是深拷贝
objects = new T[capacity()];
for (int k = ; k < size(); k++)
objects[k] = rhs.objects[k];
} return *this;
} template<class T>
void Vector<T>::resize(int newSize)
{
if (newSize > theCapacity)
reserve(newSize * + ); //每次空间不够的时候,就重新获得2倍于当前容量的空间,+1是为了防止0的情况
theSize = newSize;
} template<class T>
void Vector<T>::reserve(int newCapacity)
{
if (newCapacity < theSize)
return; T *oldArray = objects; // 之所以需要将老的数组复制到新的数组,是因为要保证Vector整块内存的连续性
objects = new T[newCapacity];
for (int k = ; k < theSize; k++)
objects[k] = oldArray[k]; theCapacity = newCapacity; delete []oldArray;
} template<class T>
T& Vector<T>::operator[](int index)
{
return objects[index];
} template<class T>
const T& Vector<T>::operator[](int index) const
{
return objects[index];
} template<class T>
bool Vector<T>::empty() const
{
return size() == ;
} template<class T>
int Vector<T>::size() const
{
return theSize;
} template<class T>
int Vector<T>::capacity() const
{
return theCapacity;
} template<class T>
void Vector<T>::push_back(const T& x)
{
if (theSize == theCapacity)
reserve(theCapacity * + );
objects[theSize++] = x;
} template<class T>
void Vector<T>::pop_back()
{
theSize--;
} template<class T>
const T& Vector<T>::back() const
{
return objects[theSize - ];
} template<class T>
T* Vector<T>::begin()
{
return &objects[];
} template<class T>
const T* Vector<T>::begin() const
{
return &objects[];
} template<class T>
T* Vector<T>::end()
{
return &objects[size() - ];
} template<class T>
const T* Vector<T>::end() const
{
return &objects[size() - ];
} #endif

  

vector的简易实现的更多相关文章

  1. 现代C++之理解decltype

     现代C++之理解decltype decltype用于生成变量名或者表达式的类型,其生成的结果有的是显而易见的,可以预测的,容易理解,有些则不容易理解.大多数情况下,与使用模板和auto时进行的类型 ...

  2. plist解析, 简易实现.

    源码 class Xml { public: typedef std::pair<std::wstring, std::wstring> NodeT; static std::vector ...

  3. 基于Win32 SDK实现的一个简易线程池

    利用C++实现了一个简易的线程池模型(基于Win32 SDK),方便使用多线程处理任务.共包含Thread.h.Thread.cpp.ThreadPool.h.ThreadPool.cpp四个源文件. ...

  4. STL—vector

    前面介绍了STL对象的构造与析构以及内存的配置与释放,那具体的容器是怎么应用STL的空间配置器的呢?这篇先介绍STL的容器vector. vector的数据成员 vector只有4个数据成员:3个迭代 ...

  5. STL—vector空间的动态增长

    vector空间的动态增长     当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vect ...

  6. Opencv探索之路(二十):制作一个简易手动图像配准工具

    近日在做基于sift特征点的图像配准时遇到匹配失败的情况,失败的原因在于两幅图像分辨率相差有点大,而且这两幅图是不同时间段的同一场景的图片,所以基于sift点的匹配已经找不到匹配点了.然后老师叫我尝试 ...

  7. DirectX11 With Windows SDK--13 动手实现一个简易Effects框架、阴影效果绘制

    前言 到现在为止,所有的教程项目都没有使用Effects11框架类来管理资源.因为在D3DCompile API (#47)版本中,如果你尝试编译fx_5_0的效果文件,会收到这样的警告: X4717 ...

  8. 简易版AC自动机

    为什么说是简易版? 因为复杂度大概是\(O(M*\overline N)\),而似乎还有另一种大概是\(O(M+\sum N)\)的. 不过据说比赛不会卡前一种做法,因为模式串一般不会很长. 那么步入 ...

  9. 基于OpenGL编写一个简易的2D渲染框架-06 编写一个粒子系统

    在这篇文章中,我将详细说明如何编写一个简易的粒子系统. 粒子系统可以模拟许多效果,下图便是这次的粒子系统的显示效果.为了方便演示,就弄成了一个动图. 图中,同时显示了 7 种不同粒子效果,看上去效果挺 ...

随机推荐

  1. x264源代码简单分析:熵编码(Entropy Encoding)部分

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  2. iOS9中如何在日历App中创建一个任意时间之前开始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 四.创建任意时间之前开始的提醒 现在我们找到了指定源中的指定日 ...

  3. ScheduledExecutorService和timer的异同

    先来个传统的Timer的例子: package com.jerry.concurrency; import java.text.ParseException; import java.text.Sim ...

  4. 自己写一个网页版的Markdown实时编辑器

    这几天忙着使用Python+Django+sqlite 搭建自己的博客系统,但是单纯的使用H5的TextArea,简直太挫了有木有.所以,就想模仿一下人家内嵌到网页上的Markdown编辑器,从而让自 ...

  5. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  6. mac OS X 从无法同步互联网时间想到的

    最近在mac OS X 巨浪 :)上执行 ntpdate time.nist.gov 失败,提示 13 Jan 19:41:53 ntpdate[1374]: the NTP socket is in ...

  7. 使用jquery获取radio的值

     使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项: ...

  8. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. StarUML中InteractionOperation的画法

    StarUML画InteractionOperation的方法:http://stackoverflow.com/questions/16152278/using-alt-in-sequence-di ...

  10. 精通CSS+DIV网页样式与布局--CSS段落效果

    在上一篇博文中,小编主要详细的介绍了CSS是如何控制文字的显示效果,随着需求的不断变更,那么我们如何对段落进行相关操作,以达到我们想要的效果呢,接下来,为了需要,小编继续来完善CSS对段落的控制的显示 ...