鱼C论坛

 找回密码
 立即注册
查看: 1356|回复: 0

[技术交流] Vector的底层实现,附上可执行代码,欢迎提出优化改进

[复制链接]
发表于 2020-8-10 08:58:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 爱学习520 于 2020-8-10 09:12 编辑

昨天写到现在,实现Vector的底层实现,代码相对而言比较简单,应该都可以看懂,我把注释写的很清楚,结构也很明确


Vector类的实现:


  1. #include<iostream>
  2. #include<string.h>
  3. #define  SUCCESS          1      //成功
  4. #define INDEX_ERROR      -1     // 错误的索引号   
  5. using namespace std;

  6. //定义Vector类
  7. template<class T_ELE>
  8. class Vector
  9. {
  10. private:
  11.         int InitSize;
  12.         int Len;
  13.         int Index;
  14.         int Increment;
  15.         T_ELE* pVector;
  16. private:
  17.         bool expand()
  18.         {
  19.                 int tlen;
  20.                 T_ELE* pNew;
  21.                 //计算扩容后的长度
  22.                 tlen = Len + Increment;
  23.                 //重新申请适应新的需要的空间大小
  24.                 pNew = new T_ELE[tlen];
  25.                 //将原来的空间的数据复制到新申请的空间
  26.                 memcpy(pNew, pVector, Len * sizeof(T_ELE));
  27.                 //释放原先的空间
  28.                 delete[] pVector;
  29.                 pVector = pNew;
  30.                 //修改其他对应的值
  31.                 Len = tlen;
  32.                 return SUCCESS;
  33.         }

  34. public:
  35.         Vector():InitSize(10),Increment(5)
  36.         {
  37.                 //分配空间
  38.                 pVector = new T_ELE[InitSize];
  39.                 //初始化申请的空间
  40.                 memset(pVector, 0, InitSize * sizeof(T_ELE));
  41.                 //初始化其他变量
  42.                 Len = InitSize;
  43.                 Index = 0;
  44.         }
  45.         Vector(int size) :Increment(5)
  46.         {
  47.                 //分配空间
  48.                 pVector = new T_ELE[size];
  49.                 //初始化
  50.                 memset(pVector, 0, size * sizeof(T_ELE));
  51.                 //其他变量初始化
  52.                 Len = size;
  53.                 Index = 0;
  54.         }
  55.         ~Vector()
  56.         {
  57.                 //释放申请的堆空间
  58.                 delete[] pVector;
  59.                 pVector = nullptr;
  60.         }

  61. public:
  62.         void push_back(T_ELE Element)
  63.         {
  64.                 //判断是否需要增容
  65.                 if (Index >= Len)
  66.                         expand();
  67.                 //将新的元素放到最后一个位置
  68.                 pVector[Index] = Element;
  69.                 //修改对应的变化值
  70.                 Index++;
  71.         }
  72.        
  73.         int insert(int index, T_ELE Element)
  74.         {
  75.                 //检测插入的区间是否有误
  76.                 if (index < 1 || index > Index + 1)
  77.                 {
  78.                         cout << index<<"不在区域范围,插入错误!" << endl;
  79.                         return INDEX_ERROR;
  80.                 }               
  81.                 //判断是否需要扩容
  82.                 if (Index >= Len)
  83.                         expand();
  84.                 //插入位置的元素和插入元素之后的所有元素后移
  85.                 memcpy(&pVector[index], &pVector[index - 1], (Index - index+1 ) * sizeof(T_ELE));
  86.                 pVector[index - 1] = Element;

  87.                 //对应的其他成员值的变化
  88.                 Index++;
  89.                 return 0;
  90.         }

  91.         int at(int index)
  92.         {
  93.                 //判断索引是否在合理区间
  94.                 if (index<1 || index>Index)
  95.                 {
  96.                         cout<<index << "不在区域内,查找越界" << endl;
  97.                         return INDEX_ERROR;
  98.                 }
  99.                 //返回该索引对应的值
  100.                 return pVector[index - 1];
  101.         }
  102. };
复制代码


测试案例:
  1. #include "Vector.cpp"

  2. //测试函数
  3. int main()
  4. {
  5.         Vector<int> s(5);
  6.         s.push_back(1);
  7.         s.push_back(2);
  8.         s.push_back(3);
  9.         s.push_back(4);
  10.         s.push_back(5);

  11.         //测试增容
  12.         s.push_back(6);

  13.         //测试插入的临界错误区域
  14.         s.insert(0,520);
  15.         s.insert(8,520);

  16.         //测试插入的临界正确区域
  17.         s.insert(1,520);
  18.         s.insert(3,520);
  19.         s.insert(7,520);

  20.         //显示经上述测试后目前Vector里面的元素,以判断是否正确,或者通过调试查看
  21.         for (int i = 0; i < 9; i++)
  22.                 cout << s.pVector[i] << endl;

  23.         //最后测试at函数
  24.         s.at(0);
  25.         s.at(4);
  26.         s.at(9);
  27.         s.at(10);
  28.        
  29.         return 0;
  30. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 20:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表