• 9月14日作业


    实现myVector

    1. #include
    2. using namespace std;
    3. template <typename T>
    4. class myVector {
    5. private:
    6. T* data;
    7. int size;
    8. int capacity;
    9. public:
    10. // 构造函数
    11. myVector() : data(nullptr), size(0), capacity(0) {}
    12. //拷贝构造函数
    13. myVector(const myVector& other):size(other.size), capacity(other.capacity){
    14. data = new T[other.capacity];
    15. for (int i = 0; i < size; i++) {
    16. data[i] = other.data[i];
    17. }
    18. }
    19. // 拷贝赋值函数
    20. myVector& operator=(const myVector& other) {
    21. if (this != &other) {
    22. // 释放旧内存
    23. delete[] data;
    24. // 复制大小和容量
    25. this->size = other.size;
    26. this->capacity = other.capacity;
    27. // 为新对象分配内存
    28. data = new T[capacity];
    29. // 复制元素到新对象
    30. for (int i = 0; i < size; i++) {
    31. data[i] = other.data[i];
    32. }
    33. }
    34. return *this;
    35. }
    36. // 析构函数
    37. ~myVector() {
    38. delete[] data;
    39. }
    40. // 末尾添加元素
    41. void push_back(const T& element) {
    42. if (size == capacity) { // 容量不足时,分配更多空间
    43. if (capacity == 0) {
    44. capacity = 1;
    45. } else {
    46. capacity *= 2;
    47. }
    48. //重新创建一个空间
    49. T* newData = new T[capacity];
    50. // 复制元素到新空间
    51. for (int i = 0; i < size; ++i) {
    52. newData[i] = data[i];
    53. }
    54. delete[] data;
    55. data = newData;
    56. }
    57. data[size++] = element;
    58. }
    59. //末尾删除元素
    60. void pop_back(){
    61. if(empty())
    62. {
    63. throw string("is empty");
    64. }
    65. size--;
    66. }
    67. //判断Vector是否为空
    68. bool empty(){
    69. return size==0;
    70. }
    71. // 获取元素数量
    72. int getSize_() const {
    73. return size;
    74. }
    75. // 获取容器容量
    76. int getCapacity_() const {
    77. return capacity;
    78. }
    79. // 访问元素
    80. T& operator[](int index) {
    81. if (index < size) {
    82. return data[index];
    83. } else {
    84. throw string("Index out of range");
    85. }
    86. }
    87. // 迭代器
    88. class iterator {
    89. private:
    90. T* ptr;
    91. public:
    92. //构造函数
    93. iterator(T* p) : ptr(p) {}
    94. //*运算符重载
    95. T& operator*() {
    96. return *ptr;
    97. }
    98. //++it运算符重载
    99. iterator& operator++() {
    100. ++ptr;
    101. return *this;
    102. }
    103. //it++运算符重载
    104. iterator operator++(int) {
    105. iterator temp = *this;
    106. ++ptr;
    107. return temp;
    108. }
    109. //!=运算符重载
    110. bool operator!=(const iterator& other) const {
    111. return ptr != other.ptr;
    112. }
    113. };
    114. iterator begin() {
    115. return iterator(data);
    116. }
    117. iterator end() {
    118. return iterator(data + size);
    119. }
    120. };
    121. int main() {
    122. myVector<int> v1; //无参构造
    123. for (int i=0; i<10; i++) { //添加元素
    124. v1.push_back(i);
    125. }
    126. myVector<int>::iterator it1 = v1.begin(); //v1迭代器
    127. cout<<"v1>>";
    128. for (; it1 != v1.end(); it1++) { //show元素
    129. cout << *it1 << " ";
    130. }
    131. cout<
    132. myVector<int> v2 = v1; //拷贝构造
    133. myVector<int>::iterator it2 = v2.begin(); //v2迭代器
    134. cout<<"v2>>";
    135. for (; it2 != v2.end(); it2++) { //show元素
    136. cout << *it2 << " ";
    137. }
    138. cout<
    139. myVector<int> v3; //无参构造
    140. v3 = v1; //拷贝赋值
    141. cout<<"is empty? = "<empty()<
    142. v3.pop_back(); // 队尾删除元素
    143. cout<<"size = "<getSize_()<
    144. cout<<"capacity = "<getCapacity_()<
    145. myVector<int>::iterator it3 = v3.begin(); //v3迭代器
    146. cout<<"v3>>";
    147. for (; it3 != v3.end(); it3++) { //show元素
    148. cout << *it3 << " ";
    149. }
    150. cout<
    151. return 0;
    152. }

    运行截图

    思维导图

  • 相关阅读:
    从零学习 InfiniBand-network架构(六)—IB协议链路层QoS如何实现
    「Qt中文教程指南」如何创建基于Qt Widget的应用程序(一)
    世界杯中隐藏的IoT物联网黑科技
    供应商关系管理的重要性
    【老生谈算法】matlab实现LCMV和LCEC算法抗干扰性能比较研究——LCEC算法
    【EI会议】第三届信息控制、电气工程及轨道交通国际学术会议(ICEERT 2023)
    0531作业 链表
    如何快速为团队打造自己的组件库(下)—— 基于 element-ui 为团队打造自己的组件库
    ChatGPT怎么运用在文学分析和文化研究中?
    教你注册chrome开发者账号,并发布chrome浏览器插件。
  • 原文地址:https://blog.csdn.net/mcslll/article/details/132889426