• C++之模板


    本阶段主要针对C++泛型编程ST技术做详细讲解,探讨C++更深层的使用

    1.1 模板的概念

    模板就是建立通用的模具,大大提高复用性

    模板的特点:

    模板不可以直接使用,它只是一个框架

    模板的通用并不是万能的 

    1.2 函数模板

    C++另一种编程思想称为 泛型编程,主要利用的技术就是模板

    C++提供两种模板机制:函数模板和类模板

    1.2.1 函数模板语法

    函数模板作用:

    建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表,

    语法:

    1. template<typename T>
    2. 函数声明或定义

    解释:

    template--- 声明创建模板

    typename---表面其后面的符号是一种数据类型,可以用class代替

    T ---通用的数据类型,名称可以替换,通常为大写字母

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. template<typename T>>//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用的数据类型
    4. void myswap(T &a,T &b)
    5. {
    6. T temp = a;
    7. a = b;
    8. b = temp;
    9. }
    10. void test01()
    11. {
    12. int a = 10;
    13. int b = 20;
    14. //1,自动类型推导
    15. cout << "第一次交换:自动类型推导" << endl;
    16. myswap(a, b);
    17. cout << "a=" << a << endl;
    18. cout << "b=" << b << endl;
    19. //2,显示指定类型
    20. cout << "第二次交换:显示指定类型" << endl;
    21. myswap<int>(a, b);
    22. cout << "a=" << a << endl;
    23. cout << "b=" << b << endl;
    24. }
    25. int main()
    26. {
    27. test01();
    28. system("pause");
    29. return 0;
    30. }

     

    总结:

    函数模板利用关键字 template

    使用函数模板有两种方式:自动类型推导、显示指定类型

    模板的目的是为了提高复用性,将类型参数化 

    1.2.2 函数模板注意事项

    注意事项:
    自动类型推导,必须推导出
    一致的数据类型T,才可以使用
    模板必须要确定出T的数据类型,才可以使用

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. template<typename T>//typename可以替换成class
    4. void myswap(T& a, T& b)
    5. {
    6. T temp = a;
    7. a = b;
    8. b = temp;
    9. }
    10. void test01()
    11. {
    12. int a = 10;
    13. int b = 20;
    14. //1,自动类型推导,必须推导出一致的数据类型T, 才可以使用
    15. cout << "第一次交换:自动类型推导" << endl;
    16. myswap(a, b);//正确
    17. cout << "a=" << a << endl;
    18. cout << "b=" << b << endl;
    19. cout << "第二次交换:显示指定类型" << endl;
    20. myswap<int>(a, b);
    21. cout << "a=" << a << endl;
    22. cout << "b=" << b << endl;
    23. }
    24. //2,模板必须要确定出T的数据类型,才可以使用
    25. template<class T>
    26. void func()
    27. {
    28. cout << "func调用" << endl;
    29. }
    30. void test02()
    31. {
    32. //func();错误,模板不能独立使用,必须确定出T的模型才可以
    33. func<int>();//利用显示指定类型的方式,给T一个类型,才可以使用该模板
    34. }
    35. int main()
    36. {
    37. test01();
    38. test02();
    39. system("pause");
    40. return 0;
    41. }

    1.2.3 函数模板案例

    案例描述:

    利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序

    排序规则从大到小,排序算法为选择排序

    分别利用char数组和int数组进行测试 

    1. #include<iostream>
    2. using namespace std;
    3. //利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
    4. //规则 从小到大
    5. //算法 选择
    6. //测试 char,数组,int 数组
    7. //打印函数模板
    8. template<typename T>
    9. void printArray(T arr[], int len)
    10. {
    11. for (int i = 0; i < len; i++)
    12. {
    13. cout << arr[i] << " ";
    14. }
    15. cout << endl;
    16. }
    17. //交换函数模板
    18. template<class T>
    19. void mySwap(T &a, T &b)
    20. {
    21. T temp = a;
    22. a = b;
    23. b = temp;
    24. }
    25. //排序算法
    26. template<class T>
    27. void mySort(T arr[], int len)
    28. {
    29. for (int i = 0; i < len; i++)
    30. {
    31. int max = i;//认定最大值的下标
    32. for (int j = i + 1; j < len; j++)
    33. {
    34. //认定的最大值 比 遍历的数值要小,说明j下边的元素是才是真正的最大值
    35. if (arr[max] < arr[j])
    36. {
    37. max = j;
    38. }
    39. }
    40. if (max != i)
    41. {
    42. //交换max和i元素的下标
    43. mySwap(arr[max], arr[i]);
    44. }
    45. }
    46. }
    47. void test02()
    48. {
    49. int intArr[] = { 7,5,1,3,9,2,4,6,8 };
    50. int num = sizeof(intArr) / sizeof(intArr[0]);
    51. mySort(intArr, num);
    52. printArray(intArr, num);
    53. }
    54. void test01()
    55. {
    56. //测试char数组
    57. char charArr[7] = "badcef";
    58. int num = sizeof(charArr) / sizeof(char);
    59. mySort(charArr, num);
    60. printArray(charArr, num);
    61. }
    62. int main()
    63. {
    64. test01();
    65. test02();
    66. system("pause");
    67. return 0;
    68. }

    1.2.4 普通函数与函数模板的区别

    普通函数与函数模板区别

    普通函数调用时可以发生自动类型转换(隐式类型转换)

    函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换

    如果利用显示指定类型的方式,可以发生隐式类型转换 

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. //普通函数与函数模板的区别
    4. //普通函数调用时可以发生自动类型转换(隐式类型转换)
    5. //函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
    6. //如果利用显示指定类型的方式,可以发生隐式类型转换 
    7. //普通数组
    8. int myAdd01(int a,int b)
    9. {
    10. return a + b;
    11. }
    12. //函数模板
    13. template<class T>
    14. T myAdd02(T a, T b)
    15. {
    16. return a + b;
    17. }
    18. void test01()
    19. {
    20. int a = 10;
    21. int b = 20;
    22. char c = 'c';
    23. cout << "a + b =" << myAdd01(a, b) << endl;
    24. cout <<"a + c =" << myAdd01(a, c) << endl;//a - 97 c - 99
    25. //自动类型推导
    26. cout << "自动类型推导" << endl;
    27. cout << myAdd02(a, b) << endl;
    28. //cout << myAdd02(a, c) << endl;//报错,不会发生隐式类型转换
    29. //显示指定类型
    30. cout << "显示指定类型" << endl;
    31. cout << myAdd02<int>(a, c) << endl;
    32. //会发生隐式类型转换
    33. }
    34. int main()
    35. {
    36. test01();
    37. system("pause");
    38. return 0;
    39. }

    总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型

     1.2.5 普通函数与函数模板的调用规则

    调用规则如下:

    1.如果函数模板和普通函数都可以实现,优先调用普通函数

    2.可以通过空模板参数列表来强制调用函数模板

    3.函数模板也可以发生重载

    4.如果函数模板可以产生更好的匹配,优先调用函数模板

    示例1:

    1. #include<iostream>
    2. using namespace std;
    3. //1.如果函数模板和普通函数都可以实现,优先调用普通函数
    4. void myprint(int a, int b)
    5. {
    6. cout << "调用的普通函数" << endl;
    7. }
    8. template<class T>
    9. void myprint(T a, T b)
    10. {
    11. cout << "调用的模板函数" << endl;
    12. }
    13. void test01()
    14. {
    15. int a = 10;
    16. int b = 10;
    17. myprint(a, b);
    18. }
    19. int main()
    20. {
    21. test01();
    22. system("pause");
    23. return 0;
    24. }

    示例2: 

    1. #include<iostream>
    2. using namespace std;
    3. //2.可以通过空模板参数列表来强制调用函数模板
    4. void myprint(int a, int b)
    5. {
    6. cout << "调用的普通函数" << endl;
    7. }
    8. template<class T>
    9. void myprint(T a, T b)
    10. {
    11. cout << "调用的模板函数" << endl;
    12. }
    13. void test01()
    14. {
    15. int a = 10;
    16. int b = 10;
    17. myprint<>(a, b);
    18. }
    19. int main()
    20. {
    21. test01();
    22. system("pause");
    23. return 0;
    24. }

    示例3:

    1. #include<iostream>
    2. using namespace std;
    3. void myprint(int a, int b)
    4. {
    5. cout << "调用的普通函数" << endl;
    6. }
    7. template<class T>
    8. void myprint(T a, T b)
    9. {
    10. cout << "调用的模板函数" << endl;
    11. }
    12. template<class T>
    13. void myprint(T a, T b,T c)
    14. {
    15. cout << "调用的模板重载函数" << endl;
    16. }
    17. void test01()
    18. {
    19. int a = 10;
    20. int b = 10;
    21. //3,函数也可以发生重载
    22. int c = 10;
    23. myprint(a, b, c);
    24. //4,如果函数模板可以产生更好的匹配,优先调用函数模板
    25. char c1 = 'a';
    26. char c2 = 'c';
    27. myprint(c1,c2);
    28. }
    29. int main()
    30. {
    31. test01();
    32. system("pause");
    33. return 0;
    34. }

     

    总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性 

    1.2.6 模板的局限性

    局限性:

    模板的通用性并不是万能的

    例如:

    1. template<class T>
    2. void f(T a, T b)
    3. {
    4. a = b;
    5. }

    在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

    再例如:

    1. template<class T>
    2. void f(T a, T b)
    3. {
    4. if(a>b)
    5. {
    6. ...
    7. }
    8. }

    在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行
    因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //模板的局限性
    5. //模板并不是万能的,有些特定数据类型,需要用具体化方式做特殊实现
    6. class Person
    7. {
    8. public:
    9. Person(string name, int age)
    10. {
    11. this->m_Name = name;
    12. this->m_age = age;
    13. }
    14. string m_Name;
    15. int m_age;
    16. };
    17. template<class T>
    18. bool myCompare(T &a,T &b)
    19. {
    20. if (a == b)
    21. {
    22. return true;
    23. }
    24. else
    25. {
    26. return false;
    27. }
    28. }
    29. //利用具体化Person版本实现代码,具体化优先调用
    30. template<>bool myCompare(Person& p1, Person& p2)
    31. {
    32. if (p1.m_Name == p2.m_Name && p1.m_age == p2.m_age)
    33. {
    34. return true;
    35. }
    36. else
    37. {
    38. return false;
    39. }
    40. }
    41. void test01()
    42. {
    43. int a = 10;
    44. int b = 20;
    45. bool ret = myCompare(a, b);
    46. if (ret)
    47. {
    48. cout << "a==b" << endl;
    49. }
    50. else
    51. {
    52. cout << "a!=b" << endl;
    53. }
    54. }
    55. void test02()
    56. {
    57. Person p1("Tom", 10);
    58. Person p2("Tom", 10);
    59. bool ret = myCompare(p1, p2);
    60. if (ret)
    61. {
    62. cout << "p1==p2" << endl;
    63. }
    64. else
    65. {
    66. cout << "p1!=p2" << endl;
    67. }
    68. }
    69. int main()
    70. {
    71. test01();
    72. test02();
    73. system("pause");
    74. return 0;
    75. }

    总结:

    利用具体化的模板,可以解决自定义类型的通用化

    学习模板不是为了写模板,而是在STL能够运用系统提供的模板 

    1.3 类模板

    1.3.1 类模板语法

    类模板作用:

    建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表。

    1. //语法:
    2. template<typename T>

    解释:

    template…声明创建模板

    typename -表面其后面的符号是一种数据类型,可以用class代替

    T… 通用的数据类型,名称可以替换,通常为大写字母

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //类模板
    5. template<class NameYtpe,class AgeType>
    6. class Person
    7. {
    8. public:
    9. Person(NameYtpe name, AgeType age)
    10. {
    11. this->m_Name = name;
    12. this->m_Age = age;
    13. }
    14. void ShowPerson()
    15. {
    16. cout << "name: " << this->m_Name <<"\t" << "age: " << this->m_Age << endl;
    17. }
    18. NameYtpe m_Name;
    19. AgeType m_Age;
    20. };
    21. void test01()
    22. {
    23. Person<string, int>p1("孙悟空", 99);
    24. p1.ShowPerson();
    25. }
    26. int main()
    27. {
    28. test01();
    29. system("pause");
    30. return 0;
    31. }

     总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

    1.3.2 类模板与函数模板区别

    类模板与函数模板区别主要有两点:

    1.类模板没有自动类型推导的使用方式

    2.类模板在模板参数列表中可以有默认参数

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //类模板与函数模板的区别
    5. template<class NameYtpe, class AgeType = int>
    6. class Person
    7. {
    8. public:
    9. Person(NameYtpe name, AgeType age )
    10. {
    11. this->m_Name = name;
    12. this->m_Age = age;
    13. }
    14. void ShowPerson()
    15. {
    16. cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
    17. }
    18. NameYtpe m_Name;
    19. AgeType m_Age;
    20. };
    21. //1.类模板没有自动类型推导的使用方式
    22. void test01()
    23. {
    24. //Person p("孙悟空",1000);
    25. Person<string, int>p1("孙悟空", 99);//只能用显示指定类型
    26. p1.ShowPerson();
    27. }
    28. //2.类模板在模板参数列表中可以有默认参数
    29. void test02()
    30. {
    31. Person<string>p2("猪八戒", 999);
    32. p2.ShowPerson();
    33. }
    34. int main()
    35. {
    36. test01();
    37. test02();
    38. system("pause");
    39. return 0;
    40. }

     

    1.3.3 类模板中成员函数创建时机

    类模板中成员函数和普通类中成员函数创建时机是有区别的:

    普通类中的成员函数一开始就可以创建

    类模板中的成员函数在调用时才创建

    1. // 类模板中成员函数创建时机
    2. //普通类中的成员函数一开始就可以创建
    3. //类模板中的成员函数在调用时才创建
    4. #include<iostream>
    5. using namespace std;
    6. class Person1
    7. {
    8. public:
    9. void showPerson1()
    10. {
    11. cout << "Person1 show" << endl;
    12. }
    13. };
    14. class Person2
    15. {
    16. public:
    17. void showPerson2()
    18. {
    19. cout << "Person2 show" << endl;
    20. }
    21. };
    22. template<class T>
    23. class Myclass
    24. {
    25. public:
    26. T obj;
    27. //类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成
    28. //类模板中的成员函数
    29. void func1()
    30. {
    31. obj.showPerson1();
    32. }
    33. void func2()
    34. {
    35. obj.showPerson2();
    36. }
    37. };
    38. void test01()
    39. {
    40. Myclass<Person1>m;
    41. m.func1();
    42. //m.func2();//编译会出错,说明函数调用时才会去创建成员函数
    43. }
    44. int main()
    45. {
    46. test01();
    47. system("pause");
    48. return 0;
    49. }

    1.3.4 类模板对象做函数参数

    学习目标:

    类模板实例化出的对象,向函数传参的方式

    共有三种传入方式:

    1.指定传入的类型   直接显示对象的数据类型

    2.参数模板化   将对象中的参数变为模板进行传递

    3. 将这个对象类型   模板化进行传递3.整个类模板化

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //类模板对象做函数参数
    5. template<class T1, class T2>
    6. class Person
    7. {
    8. public:
    9. Person(T1 name, T2 age)
    10. {
    11. this->m_Name = name;
    12. this->m_Age = age;
    13. }
    14. void showPerson()
    15. {
    16. cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
    17. }
    18. T1 m_Name;
    19. T2 m_Age;
    20. };
    21. void printPerson1(Person<string,int>&p)
    22. {
    23. p.showPerson();
    24. }
    25. //1,指定传入类型
    26. void test01()
    27. {
    28. Person<string ,int>p("孙悟空", 100);
    29. printPerson1(p);
    30. }
    31. //2,参数模板化
    32. template<class T1, class T2>
    33. void printPerson2(Person<T1, T2>& p)
    34. {
    35. p.showPerson();
    36. cout << "T1的类型为:" << typeid(T1).name() << endl;
    37. cout << "T1的类型为:" << typeid(T2).name() << endl;
    38. }
    39. void test02()
    40. {
    41. Person<string, int>p("猪八戒", 90);
    42. printPerson2(p);
    43. }
    44. //3,整个类模板化
    45. template<class T>
    46. void printPerson3(T &p)
    47. {
    48. p.showPerson();
    49. cout << "T1的类型为:" << typeid(T).name() << endl;
    50. }
    51. void test03()
    52. {
    53. Person<string, int>p("唐增", 90);
    54. printPerson3(p);
    55. }
    56. int main()
    57. {
    58. test01();
    59. test02();
    60. test03();
    61. system("pause");
    62. return 0;
    63. }

    总结:

    通过类模板创建的对象,可以有三种方式想函数中进行传参

    使用比较广泛的是第一种:指定传入的类型 

    1.3.5 类模板与继承

    当类模板碰到继承时,需要注意一下几点:

    当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T类型

    如果不指定,编译器无法给子类分配内存

    如果想灵活指定出父类中T的类型,子类也需变为类模板

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //类模板与继承
    5. template<class T>
    6. class Base
    7. {
    8. T m;
    9. };
    10. //class Son:public Base//错误,必须要指定父类中的T类型,才能继承给子类
    11. class Son1 :public Base<int>
    12. {
    13. };
    14. void test01()
    15. {
    16. Son1 s1;
    17. }
    18. //如果想灵活指定出父类中T的类型,子类也需变为类模板
    19. template<class T1,class T2>
    20. class Son2 :public Base<T2>
    21. {
    22. public:
    23. Son2()
    24. {
    25. cout << "T1的类型为:" << typeid(T1).name() << endl;
    26. cout << "T2的类型为:" << typeid(T2).name() << endl;
    27. }
    28. T1 obj;
    29. };
    30. void test02()
    31. {
    32. Son2<int, char>S2;
    33. }
    34. int main()
    35. {
    36. //test01();
    37. test02();
    38. //test03();
    39. system("pause");
    40. return 0;
    41. }

    总结:如果父类是类模板,子类需要指定出父类中T的数据类型 

    1.3.6 类模板成员函数类外实现

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. template<class T1, class T2>
    5. class Person
    6. {
    7. public:
    8. Person(T1 name, T2 age);
    9. void ShowPerson();
    10. T1 m_Name;
    11. T2 m_Age;
    12. };
    13. //构造函数的类外实现
    14. template<class T1, class T2>
    15. Person<T1,T2>::Person(T1 name, T2 age)
    16. {
    17. this->m_Name = name;
    18. this->m_Age = age;
    19. }
    20. //成员函数类外实现
    21. template<class T1, class T2>
    22. void Person<T1, T2>::ShowPerson()
    23. {
    24. cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
    25. }
    26. void test01()
    27. {
    28. Person<string, int>P("Tom", 20);
    29. P.ShowPerson();
    30. }
    31. int main()
    32. {
    33. test01();
    34. system("pause");
    35. return 0;
    36. }

    总结:类模板中成员函数类外实现时,需要加上模板参数列表 

    1.3.7 类模板分文件编写

    学习目标:

    掌握类模板成员函数分文件编写产生的问题以及解决方式

    问题:

    类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
    解决:

    解决方式1:直接包含.cpp源文件

    解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

    我们首先将类模板成员函数类外实现的文件分为以下三个文件:

    Person.h

    1. #include"Person.h"
    2. //构造函数的类外实现
    3. template<class T1, class T2>
    4. Person<T1, T2>::Person(T1 name, T2 age)
    5. {
    6. this->m_Name = name;
    7. this->m_Age = age;
    8. }
    9. //成员函数类外实现
    10. template<class T1, class T2>
    11. void Person<T1, T2>::ShowPerson()
    12. {
    13. cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
    14. }

    Person.cpp

    1. #pragma once
    2. #include<iostream>
    3. using namespace std;
    4. #include<string>
    5. //类模板分文件编写问题以及解决
    6. template<class T1, class T2>
    7. class Person
    8. {
    9. public:
    10. Person(T1 name, T2 age);
    11. void ShowPerson();
    12. T1 m_Name;
    13. T2 m_Age;
    14. };

     类模板分文件编写.cpp

    1. #include"person.h"
    2. void test01()
    3. {
    4. Person<string, int>P("Tom", 20);
    5. P.ShowPerson();
    6. }
    7. int main()
    8. {
    9. test01();
    10. system("pause");
    11. return 0;
    12. }

     解决方法1:将类模板的头文件改为:#include"person.cpp"即可运行

    解决方法2:将.h和.cpp中的内容写到一起,将后缀名改为.hpp文件

     

    总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp 

    1.3.8 类模板与友元

    学习目标:

    掌握类模板配合友元函数的类内和类外实现

    全局函数类内实现-直接在类内声明友元即可

    全局函数类外实现-需要提前让编译器知道全局函数的存在

    示例:

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. template<class T1, class T2>
    5. class Person;
    6. //类外实现
    7. template<class T1, class T2>
    8. void PrintPerson2(Person<T1, T2>p)
    9. {
    10. cout << "类外实现---姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
    11. }
    12. //通过全局函数,打印Perosn信息
    13. template<class T1,class T2>
    14. class Person
    15. {
    16. //全局函数类外实现
    17. //加空模板参数列表
    18. //如果全局函数 是类外实现,需要让编译器提前指定这个函数的存在
    19. friend void PrintPerson2<>(Person<T1, T2>p);
    20. public:
    21. //全局函数 类内实现
    22. friend void PrintPerson(Person<T1, T2>&p)
    23. {
    24. cout << "name: " << p.m_Name << "\t" << "age: " << p.m_Age << endl;
    25. }
    26. Person(T1 name, T2 age)
    27. {
    28. this->m_Name = name;
    29. this->m_Age = age;
    30. }
    31. private:
    32. T1 m_Name;
    33. T2 m_Age;
    34. };
    35. void test01()
    36. {
    37. Person<string, int>p("Tom", 100);
    38. PrintPerson(p);
    39. }
    40. //全局函数类外实现
    41. void test02()
    42. {
    43. Person<string, int>p("Jerry", 20);
    44. PrintPerson2(p);
    45. }
    46. int main()
    47. {
    48. test01();
    49. test02();
    50. system("pause");
    51. return 0;
    52. }

    总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别 

    1.3.9 类模板案例

     案例描述:实现一个通用的数组类,要求如下:

    可以对内置数据类型以及自定义数据类型的数据进行存储

    将数组中的数据存储到堆区

    构造函数中可以传入数组的容量

    提供对应的拷贝构造函数以及operator=防止浅拷贝问题

    提供尾插法和尾刚法对数组中的教据进行增加和刷除

    可以通过下标的方式访问数组中的元素

    可以获取数组中当前元素个数和数组的容量

    思路:

     

  • 相关阅读:
    YOLOV5改进:在C3模块不同位置添加D-LKA Attention(同时拥有SA注意力和大卷积核的能力)
    工具推荐 StartTool
    2022年痛风药行业定义及分类
    STM8应用笔记3.GPIO输出和输入
    BigDecimal不会丢失精度的浮点数
    SkyWalking快速上手(六)——告警
    AE& VAE 代码和结果记录
    行情分析——加密货币市场大盘走势(11.7)
    c++的作用域 (局部域,类域,名字命名空间,文件域)
    关于电话号码欺骗的一切
  • 原文地址:https://blog.csdn.net/bai_lan_ya/article/details/136749224