• C++(day7)


    思维导图

     Vector

    1. #include
    2. using namespace std;
    3. template <typename V>
    4. class Myvector{
    5. private:
    6. V *data;
    7. int Capacity;
    8. int Size;
    9. public:
    10. //无参构造
    11. Myvector():data(new V[Capacity]),Capacity(0),Size(0){
    12. cout<<"无参构造函数"<
    13. }
    14. //有参构造
    15. Myvector(int c,int s):Capacity(c),Size(s){
    16. data=new V[Capacity];
    17. cout<<"有参构造函数"<
    18. }
    19. //析构函数
    20. ~Myvector(){
    21. delete []data;
    22. cout<<"析构函数"<
    23. }
    24. //拷贝构造函数
    25. Myvector(const Myvector &other):data(new V[other.Capacity]),Capacity(other.Capacity),Size(other.Size){
    26. for (int i = 0; i < Size; i++) {
    27. data[i] = other.data[i];
    28. }
    29. cout<<"拷贝构造函数"<
    30. }
    31. //拷贝赋值函数
    32. Myvector &operator= (const Myvector &other){
    33. if(this!=&other){
    34. this->Capacity=other.Capacity;
    35. this->Size=other.Size;
    36. }
    37. if(this->data!=NULL){
    38. delete this->data;
    39. }
    40. this->data=new V(*other.data);
    41. for (int i = 0; i < Size; i++) {
    42. data[i] = other.data[i];
    43. }
    44. cout<<"拷贝赋值函数"<
    45. return *this;
    46. }
    47. //判空函数
    48. bool empty(){
    49. return 0==Size;
    50. }
    51. //capacity函数
    52. int capacity(){
    53. return Capacity;
    54. }
    55. //size函数
    56. int size(){
    57. return Size;
    58. }
    59. //插入函数
    60. void insert(int pos,V e){
    61. if(pos<0||pos>Size){
    62. throw string("下标不能小于0,不能超过Size");
    63. }
    64. else{
    65. if(Size==Capacity){
    66. V* temp=new V[Capacity*2];
    67. for (int i = 0; i < Size; i++) {
    68. temp[i] = data[i];
    69. }
    70. delete this->data;
    71. this->data=temp;
    72. this->Capacity*=2;
    73. }
    74. for(int i=Size;i>pos;i--){
    75. data[i]=data[i-1];
    76. }
    77. data[pos]=e;
    78. Size++;
    79. }
    80. cout<<"插入成功"<
    81. }
    82. //show函数
    83. void show(){
    84. for(int i=0;i
    85. cout<'\t';
    86. }
    87. cout<
    88. }
    89. };
    90. int main()
    91. {
    92. Myvector<int> v(5,0);
    93. cout<<"容量:"<capacity()<
    94. cout<<"元素数量:"<size()<
    95. v.insert(0,1);
    96. v.insert(0,2);
    97. v.insert(0,3);
    98. v.insert(0,4);
    99. v.insert(0,5);
    100. v.show();
    101. v.insert(0,6);
    102. cout<<"容量:"<capacity()<
    103. cout<<"元素数量:"<size()<
    104. v.insert(0,7);
    105. v.insert(0,8);
    106. v.insert(0,9);
    107. v.insert(0,10);
    108. v.insert(0,11);
    109. v.insert(0,12);
    110. cout<<"容量:"<capacity()<
    111. cout<<"元素数量:"<size()<
    112. v.show();
    113. return 0;
    114. }

  • 相关阅读:
    SpringMVC概述及入门
    【分享】“飞书第三方“在集简云平台集成应用的常见问题与解决方案
    HTTPS 的加密流程
    Spring MVC 工作流程源码分析
    nginx--技术文档--架构体系--底层核心-原理
    Kafka消费者
    Spring的创建和使用
    Android 老开发被薪资竟然被应届生倒挂了……
    Postgresql顺滑升级步骤(11升级到14)
    云服务器上部署仿牛客网项目
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/132890984