• c++day7


     仿照vector手动实现自己的myVector,最主要实现二倍扩容功能

    1. #include
    2. using namespace std;
    3. template <typename T>
    4. class Myvector
    5. {
    6. private:
    7. T *start;//起始指针
    8. T *end;//数组末尾指针
    9. T *last;//数组有效长度的尾指针
    10. public:
    11. //定义无参构造
    12. Myvector(){
    13. start=new T[2];
    14. last=start;
    15. end=start+1;
    16. }
    17. //定义有参构造
    18. Myvector(int num,const T &val)
    19. {
    20. start=new T[num+1];
    21. last=start;
    22. end=start+num;
    23. for(int i=0;i
    24. {
    25. start[i]=val;
    26. last++;
    27. }
    28. }
    29. //定义拷贝构造函数
    30. Myvector(const Myvector *other)
    31. {
    32. this->start=new T[other->end -other->first+1];
    33. this->last=other->last;
    34. this->end=other->end;
    35. for(int i=0;iend-other->start;++i)
    36. {
    37. this->first[i]=other->first[i];
    38. }
    39. }
    40. //定义拷贝赋值函数
    41. Myvector &operator=(const Myvector*other){
    42. if(this!=other)
    43. {
    44. delete []start;
    45. this->first=new T[other->end-other->start+1];
    46. this->last=other->last;
    47. this->end=other->end;
    48. for(int i=0;iend-other->start;i++)
    49. {
    50. this->start[i]=other->start[i];
    51. }
    52. }
    53. return *this;
    54. }
    55. //析构函数
    56. ~Myvector()
    57. {
    58. delete []start;
    59. start=nullptr;
    60. last=nullptr;
    61. end=nullptr;
    62. }
    63. //at()函数
    64. T &at(int pos){
    65. if(pos>end-start)
    66. {
    67. cout<<"越界了"<
    68. }
    69. return start[pos];
    70. }
    71. //判空
    72. bool empty(){
    73. if(last==start)
    74. {
    75. return 1;
    76. }else{return 0;}
    77. }
    78. //front()函数
    79. T &front(){
    80. return *start;
    81. }
    82. //back()函数
    83. T &back(){
    84. return *(end-1);
    85. }
    86. //size()函数
    87. int size()
    88. {
    89. return last-start;
    90. }
    91. //二倍扩容
    92. void erkr(){
    93. if(end-start==1||last==start)
    94. {
    95. int len=end-start;
    96. start=new T[len*2];
    97. }
    98. last+=(end-start)-1;
    99. return;
    100. }
    101. //push_back()
    102. void push_back(const T &val)
    103. {
    104. if(last==end)//容器满了
    105. {
    106. erkr();
    107. }
    108. *last=val;
    109. last++;
    110. }
    111. //pop_back()
    112. void pop_back()//容器是空的
    113. {
    114. if(empty())
    115. {
    116. cout<<"容器空了"<
    117. }
    118. last--;
    119. }
    120. //begin()返回第一个元素的迭代器
    121. T*begin()const{
    122. return start;
    123. }
    124. //end()
    125. T*pend(){
    126. return last;
    127. }
    128. };
    129. int main()
    130. {
    131. Myvector<int>m(2,5);
    132. cout<at(1)<
    133. cout<size()<//大小
    134. m.push_back(6);
    135. cout<size()<//大小
    136. Myvector<int>n(m);
    137. n.pop_back();
    138. cout<at(1)<
    139. return 0;
    140. }

    思维导图

  • 相关阅读:
    MySQL8窗口函数应用
    基于python的图像识别
    vue3 如何国际化
    使用 JMeter 和 Docker 进行服务存根
    设计模式——命令模式
    内网安全【2】——域防火墙/入站出站规则/不出网隧道上线/组策略对象同步
    笛卡尔树—简介
    跨域资源共享CORS详解及常见错误解决方案
    护眼灯真的可以保护眼睛吗?2022买什么护眼灯不伤孩子眼睛
    安卓ro.serialno产生的整个流程
  • 原文地址:https://blog.csdn.net/HYL1234511/article/details/132890509