• C++ day7


    简单vector

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

     

  • 相关阅读:
    GoLong的学习之路(二十一)进阶,语法之并发(go最重要的特点)(协程的主要用法)
    【每日一题】2304. 网格中的最小路径代价-2023.11.22
    Springboot利用Security做OAuth2授权验证
    Java CC 解析 SQL 语法示例
    Numpy(二) 元素与数组的操作
    听GPT 讲Rust源代码--library/std(15)
    Sass 基础教程——预处理器
    Git 详细安装教程
    【论文阅读】Dense Passage Retrieval for Open-Domain Question Answering
    【生成式网络】入门篇(五):Pix2Pix 的 代码和结果记录
  • 原文地址:https://blog.csdn.net/2301_77665369/article/details/132890995