zoukankan      html  css  js  c++  java
  • vector的简易实现

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

      具体实现代码如下:

      1 #ifndef VECTOR_H
      2 #define VECTOR_H
      3 
      4 #include <iostream>
      5 using namespace std;
      6 
      7 template<class T>
      8 class Vector
      9 {
     10 private:
     11     int theSize;
     12     int theCapacity;
     13     T *objects;
     14 
     15 public:
     16     typedef T* iterator;
     17     typedef const T* const_iterator;
     18     enum{ SPARE_CAPACITY = 16 };
     19 
     20 public:
     21     explicit Vector(int initSize = 0);    //用explicit避免隐式类型转换
     22     ~Vector();
     23     Vector(const Vector<T>& rhs);
     24     const Vector<T>& operator=(const Vector<T>& rhs);
     25 
     26     void resize(int newSize);
     27     void reserve(int newCapacity);
     28 
     29     T& operator[](int index);
     30     const T& operator[](int index) const;
     31 
     32     bool empty() const;
     33     int size() const;
     34     int capacity() const;
     35 
     36     void push_back(const T& x);
     37     void pop_back();
     38     const T& back() const;
     39 
     40     iterator begin();
     41     const_iterator begin() const;
     42 
     43     iterator end();
     44     const_iterator end() const;
     45 
     46 };
     47 
     48 template<class T>
     49 Vector<T>::Vector(int initSize = 0) : theSize(initSize), theCapacity(initSize + SPARE_CAPACITY)
     50 {
     51     objects = new T[theCapacity];
     52 }
     53 
     54 template<class T>
     55 Vector<T>::~Vector()
     56 {
     57     delete []objects;
     58 }
     59 
     60 template<class T>
     61 Vector<T>::Vector(const Vector<T>& rhs) : objects(nullptr)
     62 {
     63     operator = (rhs);
     64 }
     65 
     66 template<class T>
     67 const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
     68 {
     69     if (this != &rhs)
     70     {
     71         delete[]objects;
     72         theSize = rhs.size();
     73         theCapcity = rhs.theCapacity;
     74 
     75         // 注意是深拷贝
     76         objects = new T[capacity()];
     77         for (int k = 0; k < size(); k++)
     78             objects[k] = rhs.objects[k];
     79     }
     80 
     81     return *this;
     82 }
     83 
     84 template<class T>
     85 void Vector<T>::resize(int newSize)
     86 {
     87     if (newSize > theCapacity)
     88         reserve(newSize * 2 + 1);    //每次空间不够的时候,就重新获得2倍于当前容量的空间,+1是为了防止0的情况
     89     theSize = newSize;
     90 }
     91 
     92 template<class T>
     93 void Vector<T>::reserve(int newCapacity)
     94 {
     95     if (newCapacity < theSize)
     96         return;
     97 
     98     T *oldArray = objects;
     99 
    100     // 之所以需要将老的数组复制到新的数组,是因为要保证Vector整块内存的连续性
    101     objects = new T[newCapacity];
    102     for (int k = 0; k < theSize; k++)
    103         objects[k] = oldArray[k];
    104 
    105     theCapacity = newCapacity;
    106 
    107     delete []oldArray;
    108 }
    109 
    110 template<class T>
    111 T& Vector<T>::operator[](int index)
    112 {
    113     return objects[index];
    114 }
    115 
    116 template<class T>
    117 const T& Vector<T>::operator[](int index) const
    118 {
    119     return objects[index];
    120 }
    121 
    122 template<class T>
    123 bool Vector<T>::empty() const
    124 {
    125     return size() == 0;
    126 }
    127 
    128 template<class T>
    129 int Vector<T>::size() const
    130 {
    131     return theSize;
    132 }
    133 
    134 template<class T>
    135 int Vector<T>::capacity() const
    136 {
    137     return theCapacity;
    138 }
    139 
    140 template<class T>
    141 void Vector<T>::push_back(const T& x)
    142 {
    143     if (theSize == theCapacity)
    144         reserve(theCapacity * 2 + 1);
    145     objects[theSize++] = x;
    146 }
    147 
    148 template<class T>
    149 void Vector<T>::pop_back()
    150 {
    151     theSize--;
    152 }
    153 
    154 template<class T>
    155 const T& Vector<T>::back() const
    156 {
    157     return objects[theSize - 1];
    158 }
    159 
    160 template<class T>
    161 T* Vector<T>::begin()
    162 {
    163     return &objects[0];
    164 }
    165 
    166 template<class T>
    167 const T* Vector<T>::begin() const
    168 {
    169     return &objects[0];
    170 }
    171 
    172 template<class T>
    173 T* Vector<T>::end()
    174 {
    175     return &objects[size() - 1];
    176 }
    177 
    178 template<class T>
    179 const T* Vector<T>::end() const
    180 {
    181     return &objects[size() - 1];
    182 }
    183 
    184 #endif

       

  • 相关阅读:
    Tensorflow中实现BN为什么需要加入这个额外依赖?见CS231N作业源码
    为何神经网络权重初始化要随机初始化,不能以0为初始化
    Batch Normalization&Dropout浅析
    Git版本回退和撤销修改的区别
    linux下安装git提示”无法打开锁文件 /var/lib/dpkg/lock
    数据特征选择法
    深度学习笔记整理
    全面掌握IO(输入/输出流)
    startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
    LitePal——Android数据库框架完整使用手册
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4706193.html
Copyright © 2011-2022 走看看