• C++ STL 顺序结构 vector


    仿照系统的vector,手动实现一个my_vector。

    1. #include
    2. using namespace std;
    3. template<typename T> // 模板类: template
    4. class my_vector
    5. {
    6. private:
    7. T *first;
    8. T *end; // 大小
    9. T *last; //当前
    10. public:
    11. my_vector(int size = 2)// 构造函数
    12. {
    13. first = new T[size]; //第一次申请空间
    14. last = first; // 说明为空(当前指针)
    15. end = first+size; // 尾指针
    16. cout<<"构造函数"<
    17. }
    18. ~my_vector()// 析构函数
    19. {
    20. delete []first; // 数组的空间释放
    21. first = last = end = nullptr;
    22. cout<<"析构函数"<
    23. }
    24. my_vector(const my_vector& R)// 拷贝构造函数
    25. {
    26. //计算出原空间的尺寸
    27. int len = R.last - R.first; // 当前尺寸
    28. int size = R.end - R.first; // 尾尺寸
    29. this->first = new T[size]; // 申请空间(尾)
    30. memcpy(this->fist, R.first, len*sizeof(T)); // 需要拷贝的尺寸(当前)
    31. // 更新指针指向
    32. this->last = this->first+len; // 当前
    33. this->end = this->first+size; // 尾
    34. cout<<"拷贝构造函数"<
    35. }
    36. bool empty() // 判空
    37. {
    38. return this->first == this->last;
    39. cout<<"判空"<
    40. }
    41. bool full() // 判满
    42. {
    43. return this->end == this->last;
    44. cout<<"判满"<
    45. }
    46. void greater() //扩容
    47. {
    48. int size = this->end - this->first; //当前尺寸
    49. T* temp = new T [2*size]; // 更新空间尺寸
    50. memcpy(temp, this->first, size*sizeof(T));// 更新内容(既然扩容,肯定满的)
    51. delete []first; //释放旧空间
    52. first = temp; // 更新指针指向
    53. last = first+size; // 当前指针指向尾部
    54. end = first+2*size;
    55. cout<<"扩容"<
    56. }
    57. void push_back(const T val) // 尾插
    58. {
    59. if(this->full())
    60. {
    61. this->greater(); // 二倍扩容
    62. }
    63. *last = val;
    64. last++;
    65. //cout<<"尾插"<
    66. }
    67. void pop_back() // 尾删
    68. {
    69. if(this->empty())
    70. {
    71. return;
    72. }
    73. --last;
    74. cout<<"尾删"<
    75. }
    76. T front()const // 第一个元素
    77. {
    78. return *first;
    79. cout<<"第一个元素"<
    80. }
    81. int size_vector()
    82. {
    83. return end - first;
    84. cout<<"尺寸"<
    85. }
    86. int len_vector()
    87. {
    88. return last - first;
    89. cout<<"长度"<
    90. }
    91. T at(int pos)
    92. {
    93. if(pos<0 || pos>this->len_vector())
    94. {
    95. cout<<"at fail.";
    96. }
    97. cout<<" at:";
    98. return first[pos];
    99. }
    100. };
    101. int main()
    102. {
    103. my_vector<int> v1;
    104. for(int i=0; i<20; i++)
    105. {
    106. v1.push_back(i);
    107. // cout<
    108. cout<size_vector()<
    109. }
    110. for(int i=0; i<20; i++)
    111. {
    112. cout<at(i)<<"";
    113. }
    114. cout<
    115. return 0;
    116. }

    测试结果:

     

  • 相关阅读:
    DVWA文件上传漏洞低级_中级_高级
    Quartz,更优雅地管理你的定时任务
    java(面向对象)的23种设计模式(10)——模板方法模式
    安装mysql
    成集云 | 多维表自动查询快递100信息 | 解决方案
    【无标题】
    Elasticsearch(ES)简介及基本使用
    【MySQL】MySQL执行计划与SQL调优提高查询效率(优化篇)(实战篇)(MySQL专栏启动)
    批量解决opencv cv2.imread读取32位抠图png图像后,出现隐藏背景无法去除的问题
    全栈开发性能优化基础第四单元日考技能
  • 原文地址:https://blog.csdn.net/weixin_60354809/article/details/127825409