当前位置:   article > 正文

「数组」实现动态数组的功能 / 手撕数据结构(C++)_动态数组c++

动态数组c++

概述

动态数组,顾名思议即可变长度的数组。数组这种数据结构的实现是在栈空间或堆空间申请一段连续的可操作区域。

实现可变长度的动态数组结构,应该有以下操作:申请一段足够长的空间,如果数据的存入导致空间已满,则申请一段更长的空间,将原有数据复制过去后加入新数据,同时释放原空间。

  1. //栈空间(在栈上原地生成长度为len的数组空间)
  2. int arr[len];
  3. //堆空间(在堆上申请长度为len的空间,获得首地址交给指针arr)
  4. int *arr=new int[len];
  5. //在堆上实现动态数组
  6. int *temp=new int[len*2];//申请更长的空间
  7. strcpy(temp,arr,sizeof(int)*len)//strcpy以字节为单位将arr指向的数据复制到temp中
  8. delete[]arr;//释放原空间
  9. arr=temp;//temp赋值给arr

接下来我们通过封装array类,实现动态数组的一些基本功能 。(Code和测试案例附后)

命名空间

C++有自己的std命名空间下的array,为了进行区分,封装一个自己的动态数组命名空间custom_dynamic_array

使用namespace关键字封装,使用时可以声明using namespace custom_dynamic_array;全局声明,或custom_dynamic_array::局部声明。

  1. namespace custom_dynamic_array{
  2. ...
  3. }
  4. //全局声明
  5. using namespace custom_dynamic_array;
  6. //局部声明
  7. custom_dynamic_array::...

成员变量

定义class类array,封装三个成员变量:T*val; size_t val_size; size_t val_capacity;

size_t 是C/C++标准在stddef.h中定义的(这个头文件通常不需要#include),size_t 类型专门用于表示长度,它是无符号整数。)

template <typename T>泛型,作为数组这种数据结构,他的数据单位应该是任意一种类型(int,double,class等),template <typename T>的作用区域是它随后的一个{}内,意为T将代表某种类型,至于某种类型到底是那种类型,将在class实例化时告知(如arrar<double> arr,这句话的意思是创建一个储存double类型的动态数组类)

T* val表示指向array维护的存放数据的空间的指针val。

size_t size表示数组的当前存入内容的长度(数据长度)。

size_t val_capacity是数组的空间大小(真实长度)。

为什么维护了size和capacity两种长度?试想:每次申请长度都只是当前长度+1,则会在内存空间中反复向后利用函数申请新空间来维护数据,这同时耗费了时间和空间。

那么如果每次都申请真实长度size的两倍空间capacity,则每次申请后都有一段空闲区域容纳新元素,此时不需要申请新空间,只有当再次填满时才申请新空间,则减少了时间成本,提高了空间利用率。

  1. namespace custom_dynamic_array{
  2. template <typename T>
  3. class array {
  4. private:
  5. T* val;
  6. int val_size;
  7. int val_capacity;
  8. public:
  9. ...
  10. }
  11. }

创建销毁

我们提供七种构造函数(创建动态数组)和一个析构函数(销毁动态数组)。

无参构造arary(),初始化val_size=0,val_capaciy=1(如果等于0的话,后续申请两倍capacity会失效),val=nullptr(空指针)。

(初始化列表array():成员变量(x),...{}在构造函数后加上:即为初始化列表,表示为成员变量赋为括号内的值x,而无须写入{}中)

提供初始空间的构造array(const int num),表示申请初始空间为num的动态数组。

(断言assert(num>0),定义在assert.h头文件中,在运行时会判断括号内的条件为真,否则抛错,用于保护程序运行的安全)

提供初始长度和初值的构造array(const int size,T target_val),为val指针申请两倍size的空间,利用for循环填充长度为size的初始值。

提供源数据地址的构造array(const T* begin,const T* end),从定长度数组中获取数据进行初始化,定义int size=end-begin(指针减法获得两指针的间隔长度(以指向的数据类型为单位))

(内存拷贝函数memcpy(指针1,指针2,以字节为单位的内存长度len),将指针2指向的长度为len的内容以字节为单位复制到指针1指向的空间)

拷贝构造array(const array& another),将another整体赋值给新array。

(const array&表示这个函数保证接受一个array类型的常量引用(即该array自身而非他的拷贝),常量const保证本函数不修改another)

*注意*:以下两个构造时C++11提供的构造,这属于我们的扩展内容。

移动构造:array(array&& another) noexcept,新array直接窃取another的所有数据。

这是由于array接受了一个&&右值引用,而右值往往即将死亡,他不如直接把他的数据偷过来,直接获得他的维护的底层数组的指针,然后把他的指针置为nullptr,防止他死亡时释放掉我偷来的内存。

noexcept表明这个函数是合理的,不会抛出异常(如果内部因为别的原因抛出异常那么程序将直接被杀掉)。

此为移动构造,避免了复制操作的时间和空间浪费。

初始化列表构造array(std::initializer_list<T> ini_list)

我们的自定义数组类型应该尽量在行为上靠近基本类型的数组。

作为基本属组,它的行为是int a[]={1,2,3,4};为了让我们的数组拥有这种行为,应该使用初始化列表。initializer_list,即初始化列表,是C++11提供的新类型,使用{}进行初始化。

(注意这个初始化列表是一种类型,与上文无参构造处解释的初始化列表语法有所不同。)

我们在开头#include <initializer_list>,在对array初始化时,隐式构造一个无名initializer_list,然后将它作为初始化参数赋给array,就实现了字面意义上的array<int>a={1,2,3};

其中{1,2,3}隐式构造了一张无名initializer_list,然后它被作为参数传入a的初始化构造中。

函数体中使用const T*指针遍历初始化列表并依次赋给array

(std::initializer_list<T>表示initializer_list内部的数据类型是T,未使用using namespace std时std::用于声明initializer_list的命名空间)

析构函数~array(),销毁数组释放空间,在主函数结束时自动调用,但在堆上申请的空间需要写入函数体否则无法释放。

  1. array() : val_size(0), val_capacity(1), val(nullptr) {};//无参构造
  2. array(const int num) : val_size(0), val_capacity(num) {//提供初始空间的构造
  3. assert(num > 0);
  4. val = new T[num];
  5. }
  6. array(const int size, T target_val) : val_size(size), val_capacity(size * 2) {//提供初始长度和初值的构造
  7. assert(size > 0);
  8. val = new T[size * 2];
  9. for (int i = 0; i < size; i++)val[i] = target_val;
  10. }
  11. array(const T* begin, const T* end) {//从固定长度数组中获取数据进行构造
  12. assert(begin != nullptr && end != nullptr);
  13. int size = end - begin;
  14. val = new T[size * 2];
  15. val_size = size;
  16. val_capacity = size * 2;
  17. memcpy(val, begin, sizeof(T) * size);
  18. }
  19. array(const array& another) : val_size(another.val_size), val_capacity(another.val_capacity) {//拷贝构造
  20. val = new T[another.val_capacity];
  21. memcpy(val, another.val, sizeof(T) * another.val_size);
  22. }
  23. array(array&& another) noexcept : val_size(another.val_size), val_capacity(another.val_capacity) {//移动构造
  24. val = another.val;
  25. another.val = nullptr;
  26. }
  27. array(const std::initializer_list<T> ini_list) {//初始化列表构造
  28. int size = ini_list.size();
  29. val = new T[size * 2];
  30. int i = 0;
  31. for (const T* it = ini_list.begin(); it != ini_list.end(); it++)val[i++] = *it;
  32. val_size = size;
  33. val_capacity = size * 2;
  34. }
  35. ~array() {
  36. delete[]val;
  37. }
  38. array& operator=(const array& another) {//重载等于号(它也可以作为构造函数在构造时使用)
  39. delete[]val;
  40. val = new T[another.val_capacity];
  41. memcpy(val, another.val, sizeof(T) * another.val_size);
  42. val_size = another.val_size;
  43. val_capacity = another.val_capacity;
  44. return *this;
  45. }

整体赋值

作为class类,array应该同时拥有类的性质的数组的性质,那么他就应该可以被视为整体来进行操控。
我们提供三种整体赋值的函数。

重载等于号(复制赋值)array& operator=(const array& another),等于号作为一个的成员函数被我们重新定义,接收另一个array another的常量引用并整体赋值并返回一个自身的常量引用(这是为连等=...=...=...服务的)。

(this指针:返回这个array类的对象本身)

另有函数重载实现移动赋值,与移动构造类似。详见Code。

提供新长度和填充值的内存分配void assign(const int size, T target_val),效果类似于提供长度和初始值的构造。

数据交换void swap(array& another),交换两个array的值。

  1. array& operator=(const array& another) {//重载等于号(它也可以作为构造函数在构造时使用)
  2. delete[]val;
  3. val = new T[another.val_capacity];
  4. memcpy(val, another.val, sizeof(T) * another.val_size);
  5. val_size = another.val_size;
  6. val_capacity = another.val_capacity;
  7. return *this;
  8. }
  9. array& operator=(array&& another)noexcept {//移动赋值运算符
  10. if (this == &another)return *this;
  11. delete[]val;
  12. val = another.val;
  13. val_size = another.val_size;
  14. val_capacity = another.val_capacity;
  15. another.val=nullptr;
  16. return *this;
  17. }
  18. void assign(const int size, T target_val) {
  19. assert(size > 0);
  20. delete[]val;
  21. val = new T[size * 2];
  22. for (int i = 0; i < size; i++)val[i] = target_val;
  23. val_size = size;
  24. val_capacity = size * 2;
  25. }
  26. void swap(array& another) {
  27. T* tempval=another.val;
  28. size_t tempsize = another.val_size,tempcapacity = another.val_capacity;
  29. another.val = val, another.val_size = val_size, another.val_capacity = val_capacity;
  30. val = tempval, val_size = tempsize, val_capacity = tempcapacity;
  31. }

内存管理 

我们提供四个size相关的函数和两个capacity相关的函数维护内存。

获取有效数据长度size_t size(),返回val_size。

(函数()与{ }之间的const表示函数体内部不进行数据的更改操作)

判断有效空间是否为空bool empty(),返回val_size是否不等于0,是则false,否则true。

提供新长度的长度重置void resize(const int size),在size变小时截断后续数据。

提供新长度和填充值的长度重置void resize(const int size, T target_val),在size变小时截断后续数据,在size变大时在新位置填充target_val。

获取实际空间长度size_t capacity()const,返回val_capacity。

延长空间void reserve(const int num),如果申请的新空间小于val_capacity,无事发生,否则申请更大的空间。

  1. size_t size()const {
  2. return val_size;
  3. }
  4. bool empty()const {
  5. return val_size ? false : true;
  6. }
  7. void resize(const int size) {
  8. assert(size > 0);
  9. T* temp = new T[size * 2];
  10. if (size < val_size)memcpy(temp, val, sizeof(T) * size);
  11. else memcpy(temp, val, sizeof(T) * val_size);
  12. delete[]val;
  13. val = temp;
  14. val_size = size;
  15. val_capacity = size * 2;
  16. }
  17. void resize(const int size, T target_val) {
  18. assert(size > 0);
  19. T* temp = new T[size * 2];
  20. for (int i = 0; i < size; i++)temp[i] = target_val;
  21. if (size < val_size)memcpy(temp, val, sizeof(T) * size);
  22. else memcpy(temp, val, sizeof(T) * val_size);
  23. delete[]val;
  24. val = temp;
  25. val_size = size;
  26. val_capacity = size * 2;
  27. }
  28. size_t capacity()const {
  29. return val_capacity;
  30. }
  31. void reserve(const int num) {
  32. if (num > val_capacity) {
  33. T* temp = new T[num];
  34. memcpy(temp, val, sizeof(T) * val_size);
  35. delete[]val;
  36. val = temp;
  37. val_capacity = num;
  38. }
  39. }

数据控制

与固定长度数组不同,我们需要调用成员函数来实现数据增删。

压入新元素void push_back(T elem),在末尾追加新元素elem,如果有剩余空间大于1则直接写入,否则申请两倍的新空间。

插入新元素void insert(const int pos, T element),在给定位置插入元素elem。

另有函数重载实现区间插入,详见Code。

删除末元素void pop_back(),删除最后一个元素。

删除元素void erase(const int pos),删除指定位置的元素。

另有函数重载实现区间删除,详见Code。

清空元素void clear(),将val_size置为0。

(memet(指针p,char val,以字节为单位的内存长度len),以字节为单位将val赋给p指向的长度为len的空间)

  1. void push_back(T elem) {
  2. if (val_capacity - val_size <= 1)reserve(val_capacity * 2);
  3. val[val_size++] = elem;
  4. }
  5. void insert(const int pos, T elem) {
  6. assert(pos >= 0 && pos <= val_size);
  7. if (val_capacity - val_size <= 1)reserve(val_capacity * 2);
  8. val_size++;
  9. for (int i = val_size - 1; i > pos; i--) val[i] = val[i - 1];
  10. val[pos] = elem;
  11. }
  12. void insert(const int pos,const T* begin,const T* end) {
  13. assert(pos >= 0 && pos <= val_size);
  14. const int len = end - begin;
  15. if (val_capacity - val_size <= len)reserve(val_capacity * 2);
  16. val_size += len;
  17. for (int i = val_size - 1; i > pos+len-1; i--) val[i] = val[i - len];
  18. for (int j = 0; j < len; j++)val[pos+j] = *(begin + j);
  19. }
  20. void pop_back() {
  21. assert(val_size > 0);
  22. val_size--;
  23. }
  24. void erase(const int pos) {
  25. assert(pos >= 0 && pos < val_size);
  26. if(pos != val_size-1)
  27. for (int i = pos + 1; i < val_size; i++)
  28. val[i - 1] = val[i];
  29. val_size--;
  30. }
  31. void erase(const int begin,int end) {
  32. assert(begin >= 0 && begin < val_size);
  33. end = end >= val_size ? val_size : end;
  34. const int len = end - begin;
  35. if(end < val_size)
  36. for (int i = begin; i < end; i++)
  37. val[i] = val[i + len];
  38. val_size -= len;
  39. }
  40. void clear() {
  41. memset(val, 0, sizeof(T) * val_size);
  42. val_size = 0;
  43. }

数据访问

我们通过编写返回值为引用类型的函数来实现数据的读写功能。

引用返回这个数据本身而非他的拷贝,它是一种比指针更安全的数据传递手段。

以下的所有接口函数都要被声明为const函数,这意味这函数体本身不执行任何的数据变更操作,但这不表示不可以通过接口函数实现数据变更操作。

重载[]号T& operator[](int pos),为了使我们的数组类有定长数组的特征,重载[]号使arr[]具有作用。[]接收一个int pos,返回pos对应位置的引用。不允许访问负坐标。为了和下一个函数区分,我们允许访问溢出(pos>=size)

成员函数式访问下标T& at(int pos),上一个函数是模仿固定长度数组的行为,这个函数则提供类的函数式的下标访问,他不允许访问负坐标和访问溢出。

获取头元素T& front(),获取第一个元素的引用。

获取尾元素T& back(),获取最后一个元素的引用。

直接获取底部*T data(),我们运用array类来维护底部的T[]数组,那么用户就应该可以直接获取底部地址。不使用常量修饰而直接暴露接口的行为是极其不安全的,但考虑到这是一篇面向入门读者的文章,我们考虑通过暴露接口来对接一些外部功能(如排序、翻转),而将安全性暂放次要。

  1. T& operator[](const int pos)const {
  2. assert(pos >= 0);
  3. return val[pos];
  4. }
  5. T& at(const int pos)const {
  6. assert(pos >= 0 && pos < val_size);
  7. return val[pos];
  8. }
  9. T& front()const {
  10. assert(val_size > 0);
  11. return val[0];
  12. }
  13. T& back()const {
  14. assert(val_size > 0);
  15. return val[val_size - 1];
  16. }
  17. T* data()const {
  18. return val;
  19. }

Code

*注意*:代码中对于接收T类型参数的函数,我使用了template <typename V>并使得函数接受了万能引用V&&,如果你对此感兴趣,可以自行学习,否则你可以全部改回T。

  1. #include <initializer_list>
  2. #include <cassert>
  3. #ifndef CUSTOM_DAYNAMIN_ARRAY
  4. #define CUSTOM_DAYNAMIN_ARRAY
  5. namespace custom_dynamic_array {//封装一个命名空间
  6. template <typename T>
  7. class array {
  8. private:
  9. T* val;
  10. size_t val_size;
  11. size_t val_capacity;
  12. public:
  13. array() : val_size(0), val_capacity(1), val(nullptr) {};//无参构造
  14. array(const int num) : val_size(0), val_capacity(num) {//提供初始空间的构造
  15. assert(num > 0);
  16. val = new T[num];
  17. }
  18. template<typename V>
  19. array(const int size, V&& target_val) : val_size(size), val_capacity(size * 2) {//提供初始长度和初值的构造
  20. assert(size > 0);
  21. val = new T[size * 2];
  22. for (int i = 0; i < size; i++)val[i] = target_val;
  23. }
  24. array(const T* begin, const T* end) {//从固定长度数组中获取数据进行构造
  25. assert(begin != nullptr && end != nullptr);
  26. int size = end - begin;
  27. val = new T[size * 2];
  28. val_size = size;
  29. val_capacity = size * 2;
  30. memcpy(val, begin, sizeof(T) * size);
  31. }
  32. array(const array& another) : val_size(another.val_size), val_capacity(another.val_capacity) {//拷贝构造
  33. val = new T[another.val_capacity];
  34. memcpy(val, another.val, sizeof(T) * another.val_size);
  35. }
  36. array(array&& another) noexcept : val_size(another.val_size), val_capacity(another.val_capacity) {//移动构造
  37. val = another.val;
  38. another.val = nullptr;
  39. }
  40. array(const std::initializer_list<T> ini_list) {//初始化列表构造
  41. int size = ini_list.size();
  42. val = new T[size * 2];
  43. int i = 0;
  44. for (const T* it = ini_list.begin(); it != ini_list.end(); it++)val[i++] = *it;
  45. val_size = size;
  46. val_capacity = size * 2;
  47. }
  48. ~array() {
  49. delete[]val;
  50. }
  51. array& operator=(const array& another) {//重载等于号(它也可以作为构造函数在构造时使用)
  52. delete[]val;
  53. val = new T[another.val_capacity];
  54. memcpy(val, another.val, sizeof(T) * another.val_size);
  55. val_size = another.val_size;
  56. val_capacity = another.val_capacity;
  57. return *this;
  58. }
  59. array& operator=(array&& another)noexcept {//移动赋值运算符
  60. if (this == &another)return *this;
  61. delete[]val;
  62. val = another.val;
  63. val_size = another.val_size;
  64. val_capacity = another.val_capacity;
  65. another.val = nullptr;
  66. return *this;
  67. }
  68. template<typename V>
  69. void assign(const int size, V&& target_val) {
  70. assert(size > 0);
  71. delete[]val;
  72. val = new T[size * 2];
  73. for (int i = 0; i < size; i++)val[i] = target_val;
  74. val_size = size;
  75. val_capacity = size * 2;
  76. }
  77. void swap(array& another) {
  78. T* tempval = another.val;
  79. size_t tempsize = another.val_size, tempcapacity = another.val_capacity;
  80. another.val = val, another.val_size = val_size, another.val_capacity = val_capacity;
  81. val = tempval, val_size = tempsize, val_capacity = tempcapacity;
  82. }
  83. size_t size()const {
  84. return val_size;
  85. }
  86. bool empty()const {
  87. return val_size ? false : true;
  88. }
  89. void resize(const int size) {
  90. assert(size > 0);
  91. T* temp = new T[size * 2];
  92. if (size < val_size)memcpy(temp, val, sizeof(T) * size);
  93. else memcpy(temp, val, sizeof(T) * val_size);
  94. delete[]val;
  95. val = temp;
  96. val_size = size;
  97. val_capacity = size * 2;
  98. }
  99. template<typename V>
  100. void resize(const int size, V&& target_val) {
  101. assert(size > 0);
  102. T* temp = new T[size * 2];
  103. for (int i = 0; i < size; i++)temp[i] = target_val;
  104. if (size < val_size)memcpy(temp, val, sizeof(T) * size);
  105. else memcpy(temp, val, sizeof(T) * val_size);
  106. delete[]val;
  107. val = temp;
  108. val_size = size;
  109. val_capacity = size * 2;
  110. }
  111. size_t capacity()const {
  112. return val_capacity;
  113. }
  114. void reserve(const int num) {
  115. if (num > val_capacity) {
  116. T* temp = new T[num];
  117. memcpy(temp, val, sizeof(T) * val_size);
  118. delete[]val;
  119. val = temp;
  120. val_capacity = num;
  121. }
  122. }
  123. template<typename V>
  124. void push_back(V&& elem) {
  125. if (val_capacity - val_size <= 1)reserve(val_capacity * 2);
  126. val[val_size++] = elem;
  127. }
  128. template<typename V>
  129. void insert(const int pos, V&& elem) {
  130. assert(pos >= 0 && pos <= val_size);
  131. if (val_capacity - val_size <= 1)reserve(val_capacity * 2);
  132. val_size++;
  133. for (int i = val_size - 1; i > pos; i--) val[i] = val[i - 1];
  134. val[pos] = elem;
  135. }
  136. void insert(const int pos, const T* begin, const T* end) {
  137. assert(pos >= 0 && pos <= val_size);
  138. const int len = end - begin;
  139. if (val_capacity - val_size <= len)reserve(val_capacity * 2);
  140. val_size += len;
  141. for (int i = val_size - 1; i > pos + len - 1; i--) val[i] = val[i - len];
  142. for (int j = 0; j < len; j++)val[pos + j] = *(begin + j);
  143. }
  144. void pop_back() {
  145. assert(val_size > 0);
  146. val_size--;
  147. }
  148. void erase(const int pos) {
  149. assert(pos >= 0 && pos < val_size);
  150. if(pos != val_size-1)
  151. for (int i = pos + 1; i < val_size; i++)
  152. val[i - 1] = val[i];
  153. val_size--;
  154. }
  155. void erase(const int begin, int end) {
  156. assert(begin >= 0 && begin < val_size);
  157. end = end >= val_size ? val_size : end;
  158. const int len = end - begin;
  159. if (end < val_size)
  160. for (int i = begin; i < end; i++)
  161. val[i] = val[i + len];
  162. val_size -= len;
  163. }
  164. void clear() {
  165. memset(val, 0, sizeof(T) * val_size);
  166. val_size = 0;
  167. }
  168. T& operator[](const int pos)const {
  169. assert(pos >= 0);
  170. return val[pos];
  171. }
  172. T& at(const int pos)const {
  173. assert(pos >= 0 && pos < val_size);
  174. return val[pos];
  175. }
  176. T& front()const {
  177. assert(val_size > 0);
  178. return val[0];
  179. }
  180. T& back()const {
  181. assert(val_size > 0);
  182. return val[val_size - 1];
  183. }
  184. T* data()const {
  185. return val;
  186. }
  187. };
  188. }
  189. #endif

测试

  1. #include <iostream>
  2. #include "array.h"
  3. using namespace custom_dynamic_array;//全局使用自己的命名空间;
  4. template <typename T> //在不全局使用std命名空间时,使用custom_dynamic_array::array<T>....来实现同样效果
  5. void show(::array<T>& arr) {
  6. if (arr.empty())std::cout << "EMPTY";
  7. else for (int i = 0; i < arr.size(); i++)std::cout << arr[i] << ' ';
  8. std::cout << std::endl;
  9. }
  10. int main()
  11. {
  12. std::cout << "---------------test1---------------" << std::endl;
  13. array<int>a; show<int>(a);
  14. array<int>b(5); show<int>(b);
  15. array<int>c(3, 3); show<int>(c);
  16. double x[] = { 1.1,2.2,3.3 };
  17. array<double>d(x, x + 3); show<double>(d);
  18. array<double>e(d); show(e);
  19. array<int>f = { 5,6,7,8,9 }; show<int>(f);
  20. array<int>g = a; show<int>(g);
  21. std::cout <<"------------------------------------" << std::endl;
  22. std::cout << "---------------test2---------------" << std::endl;
  23. a = c; show<int>(a);
  24. b.assign(6, 2); std::cout << b.size() << std::endl;
  25. c.assign(8, 2); show<int>(c);
  26. b.swap(c); show<int>(b);
  27. std::cout << std::endl;
  28. std::cout << "------------------------------------" << std::endl;
  29. std::cout << "---------------test3---------------" << std::endl;
  30. std::cout << b.size() << std::endl;
  31. std::cout << (b.empty() ? "YES" : "NO") << std::endl;
  32. b.resize(10, 5); show<int>(b);
  33. std::cout << b.capacity() << std::endl;
  34. b.reserve(50);
  35. std::cout << b.capacity() << std::endl;
  36. std::cout << std::endl;
  37. std::cout << "------------------------------------" << std::endl;
  38. std::cout << "---------------test4---------------" << std::endl;
  39. a.push_back(10); a.push_back(10); a.push_back(10); a.push_back(10); show<int>(a);
  40. a.insert(1, 7); show<int>(a);
  41. int xx[3] = { 10,11,12 };
  42. a.insert(1, xx, xx + 2); show<int>(a);
  43. a.pop_back(); show<int>(a);
  44. a.erase(2); show<int>(a);
  45. a.erase(4, 6); show<int>(a);
  46. a.erase(2, 20); show<int>(a);
  47. a.clear(); std::cout << (a.empty() ? "YES" : "NO") << std::endl;
  48. std::cout << std::endl;
  49. std::cout << "------------------------------------" << std::endl;
  50. std::cout << "---------------test5---------------" << std::endl;
  51. show<double>(d);
  52. d[1] = 8.8; show<double>(d);
  53. std::cout << d.front() << ' ' << d.back() << std::endl;
  54. std::cout << "------------------------------------" << std::endl;
  55. return 0;
  56. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/994481
推荐阅读
相关标签
  

闽ICP备14008679号