zoukankan      html  css  js  c++  java
  • vector内部的实现1

    写vector的内部方法

     1 #include<vector>
     2 using std::vector;
     3 //写一个动态内存
     4 
     5 class CA{
     6     int a;
     7 public:
     8     CA(int i){ i = a; }
     9     CA();
    10 };
    11 template <class T>
    12 class CStack
    13 {
    14     T pBuff[10];
    15 public:
    16     CStack();
    17     ~CStack();
    18     void push(T const& val);//入栈
    19     void pop();//出栈
    20     T GetTop();//得到栈顶的元素
    21 };
    22 template <class T>
    23 void CStack<T>::pop()
    24 {
    25     top--;
    26 }
    27 
    28 template <class T>
    29 CStack<T>::CStack()
    30 {
    31     top = 0;
    32 }
    33 
    34 template <class T>
    35 void CStack<T>::push(T const& val)
    36 {
    37     pBuff[top++] = val;
    38 }
    39 int main()
    40 {
    41     CA a(1);//调用带参构造
    42     CA pa[10];//调用默认构造  数组不可以用带参构造来构造
    43     CA *pa1 = new CA;//调用默认构造
    44 
    45     return 0;
    46 }

    写一个字节的类对自己的类进行加工封装函数 实现vector内部封装的效果 进而实现所需要的内容

      1 #pragma once
      2 template<typename T>
      3 class Myvector
      4 {
      5     T *pBuff;//给一个指针用来指向以恶个动态内存
      6     unsigned int len;//给一个长度 表示这个内存中的元素个数
      7     size_t maxSize;//存放这个动态数组的内存空间的大小
      8 public:
      9     Myvector();//默认构造
     10     Myvector(int n);//有n个T的默认构造对象 构造进这个容器
     11     Myvector(int n,T const& elem);//用n个elem对象 构造进这个容器
     12     Myvector(Myvector const& other);//深拷贝
     13     //Myvector();
     14 
     15     ~Myvector();//析构函数 释放所有的对象 也会将变量删除(堆内存和栈内存)
     16     void clear();//清除 (删除堆内存里数据)
     17 public:
     18     size_t size() const;
     19     size_t capacit() const;//返回容器代下
     20     bool empty() const;//判断容器是否为空
     21 public:
     22     bool operator==(Myvector const& srcVector) const;//运算符重载==
     23     bool operator!=(Myvector const& srcVector) const; //重载运算符!=
     24 public:
     25     void assgin(int n, T const& elem);//赋值
     26     void swap(Myvector & srcVerctor);//交换
     27 public:
     28     T at(int index);//返回动态数组下标为index的元素 是c++中唯一会主动抛异常的函数
     29     T operator[](int index);//重载[]运算符
     30     T front();//得到容器中的第一个元素,不管容器为不为空
     31     T back();
     32 public:
     33     void push_back(T const& elem);//往动态数组的 尾部添加数字
     34 };
     35 template<typename T>
     36 void Myvector<T>::Myvector()//无参构造
     37 {
     38     pBuff = nullptr;
     39     len = 0;
     40     maxSize = 0;
     41 }
     42 
     43 template<typename T>
     44 void Myvector<T>::~Myvector()//析构函数 清除对象
     45 {
     46     clear();//调用clear函数 重复代码通过封装函数来提高代码的效率
     47 }
     48 template<typename T>
     49 void Myvector<T>::clear
     50 {
     51     if (pBuff != nullptr)
     52     {
     53         delete[] pBuff;
     54         pBuff = nullptr;
     55         len = 0;
     56         maxSize = 0;
     57     }
     58 }
     59 template<typename T>
     60 void Myvector<T>::Myvector(int n)//n个有T的默认构造的构造对象 构造进这个容器
     61 {//n个对象构造这个容器
     62     if (n < 0)
     63     {
     64         pBuff = nullptr;
     65         len = 0;
     66         maxSize = 0;
     67     }
     68     else
     69     {
     70         len = maxSize = n;
     71         pBuff = new T[maxSize];
     72     }
     73 }
     74 
     75 template<typename T>
     76 void Myvector<T>::Myvector(int n, T const& elem)//用n个elem对象来构造这个容器  &来增加传递效率
     77 {
     78     if (n < 0)
     79     {
     80         pBuff = nullptr;
     81         len = 0;
     82         maxSize = 0;
     83     }
     84     else
     85     {
     86         len = maxSize = n;
     87         pBuff[i] = new T[maxSize];
     88         for (size_t i = 0; i < len; ++i)//size_t无符号的int
     89         {
     90             pBuff[i] = elem;
     91         }
     92     }
     93 }
     94 template<typename T>
     95 void Myvector<T>::Myvector(Myvector const& other)
     96 {
     97     len = other.len;
     98     maxSize = other.maxSize;
     99     pBuff = nullptr;
    100     if (len > 0)
    101     {
    102         pBuff = new T[maxSize];
    103         for (int i = 0; i < len; ++i)
    104             pBuff[i] = other.pBuff[i];
    105     }
    106 }
    107 template<typename T>
    108 size_t Myvector<T>::size() const//返回容器的长度
    109 {
    110     return len;
    111 }
    112 template<typename T>
    113 size_t Myvector<T>::capacity() const//返回容器大小
    114 {
    115     return maxSize;
    116 }
    117 template<typename T>
    118 bool Myvector<T>::empty() const//判断容器是否为空
    119 {
    120     //return pBuff == nullptr;泵使用这一种 这一种判定存不存在
    121     return len == 0;//是否为0 0是空的
    122 }
    123 template<typename T>
    124 bool Myvector<T>::operator==(Myvector const& srcVector) const//判断容器是不是相等
    125 {
    126     if (len != srcVector.len)//长度不等
    127         return false;
    128     for (size_t i = 0; i < len; ++i)//遍历判定内容是否相等
    129     {
    130         if (pBuff[i] != srcVector.pBuff[i])
    131             return false;
    132     }
    133     return true;
    134 }
    135 template<typename T>
    136 bool Myvector<T>::operator==(Myvector const& srcVector) const//判断容器是不是相等
    137 {
    138     return !(*this == srcVector);
    139 }
    140 template<typename T>
    141 void Myvector<T>::assign(int n, T const& elem)//和带参构造类似 对容器进行赋值
    142 {
    143     clear();//重点:清除自身可能原有的动态内存
    144     if (n > 0)
    145     {
    146         len = maxSize = n;
    147         pBuff = new T[maxSize];
    148         for (size_t i = 0; i < len; ++i)
    149             pBuff[i] = elem;
    150     }
    151 }
    152 
    153 void Myvector<T>::swap(Myvector & srcVector)
    154 {
    155     T *tempBuff = pBuff;
    156     size_t tempLen = len;
    157     size_t tempMaxSize = maxSize;//定义三个变量
    158 
    159     pBuff = srcVector.pBuff;
    160     len = srcVector.len;
    161     maxSize = srcVector.maxSize;//数据的交换
    162 
    163     srcVector.pBuff = tempBuff;
    164     srcVector.len = tempLen;
    165     srcVector.maxSize = tempMaxSize;
    166 }
    167 template<typename T>
    168 T CMyVector<T>::at(int index)//唯一的会主动抛异常的函数
    169 {
    170     if (index < 0 || index >= len)
    171         throw "out_of_range";//防止越界 抛异常
    172     return pBuff[index];
    173 }
    174 
    175 template<typename T>
    176 T CMyVector<T>::operator[](int index)//这个应该出错就出错 无法出来 按照给的要求来
    177 {
    178     return pBuff[index];
    179 }
    180 template<typename T>
    181 T Myvector<T>::back()
    182 {
    183     return pBuff[len - 1];
    184 }
    185 
    186 template<typename T>
    187 T Myvector<T>::front()
    188 {
    189     return pBuff[0];
    190 }
    191 
    192 template<typename T>
    193 T Myvector<T>::operator[](int index)
    194 {
    195     return pBuff[index];
    196 }
    197 template<typename T>
    198 void Myvector<T>::push_back(T const& elem)
    199 {
    200     //1 2 3 4 6 9 13 19
    201     if (len >= maxSize)
    202     {
    203         maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1);
    204         T *tempBuff = new T[maxSize];
    205         for (size_t i = 0; i < len; ++i)
    206             tempBuff[i] = pBuff[i];
    207         if (pBuff != nullptr)
    208             delete[] pBuff;
    209         pBuff = tempBuff;
    210     }
    211     pBuff[len++] = elem;
    212 }
  • 相关阅读:
    (转)Apache与Tomcat 区别联系
    (转)JAVA排序汇总
    (转)Java线程:大总结
    (转)Java线程:新特征-原子量,障碍器
    (转)Java线程:新特征-条件变量
    oracle中的not in和not exists注意事项
    oracle字符乱码的解决方法
    线刷和卡刷的区别
    nexus5刷机
    linux上复制行到另一个文件
  • 原文地址:https://www.cnblogs.com/liugangjiayou/p/11406312.html
Copyright © 2011-2022 走看看