用类模板实现容器存储自定义数据类型(类似于STL里面的vector)
上一节里面已经提到了,用类模板存储自定义的数据类型,如Teacher类型时,需要重载Teacher类的拷贝构造函数,“=”操作符,"<<"操作符,特别要注意深拷贝和浅拷贝的问题。
例如:
1 //1.myvector.h文件 2 #ifndef MYVECTOR_H 3 #define MYVECTOR_H 4 5 #include <iostream> 6 using namespace std; 7 8 template<typename T> 9 class Myvector 10 { 11 friend ostream& operator<<<T>(ostream& out,Myvector<T>& obj); 12 public: 13 Myvector(int size);//构造函数 14 Myvector(Myvector<T>&);//拷贝构造函数 15 ~Myvector();//析构函数 16 17 public: 18 Myvector<T>& operator=(const Myvector<T>& obj); 19 T& operator[](int index); 20 int getlen(){return my_len;} 21 22 private: 23 T* my_space;//模板数组的首地址 24 int my_len;//模板数组的长度,等于最大索引数+1 25 }; 26 27 #endif 28 29 30 //2.myvector.cpp文件 31 #include <iostream> 32 #include "myvector.h" 33 34 35 using namespace std; 36 37 //构造函数 38 template<typename T> 39 Myvector<T>::Myvector(int size) 40 { 41 my_len=size; 42 my_space=new T[my_len]; 43 if(my_space==NULL) 44 { 45 cout<<"调用构造函数 分配内存失败!"<<endl; 46 return; 47 } 48 } 49 50 //拷贝构造函数,深拷贝。拷贝构造函数也是构造函数,一开始my_len,my_space的值都是没有的(随机数),不存在释放什么的,全是要靠自己复制才会有 51 template <typename T> 52 Myvector<T>::Myvector(Myvector<T>& obj) 53 { 54 my_len=obj.my_len; 55 my_space=new T[my_len]; 56 57 for(int i=0;i<my_len;i++) 58 { 59 my_space[i]=obj.my_space[i]; 60 } 61 } 62 63 //析构函数 64 template<typename T> 65 Myvector<T>::~Myvector() 66 { 67 cout<<"调用模板类的析构函数"<<endl; 68 if(my_space!=NULL) 69 { 70 delete [] my_space; 71 my_space=NULL; 72 my_len=0; 73 } 74 } 75 76 //重载"[]"操作符 77 template<typename T> 78 T& Myvector<T>::operator[](int index) 79 { 80 return this->my_space[index]; 81 } 82 83 84 //重载"="操作符 85 template<typename T> 86 Myvector<T>& Myvector<T>::operator=(const Myvector<T>& obj) 87 { 88 if(my_space!=NULL) 89 { 90 my_len=0; 91 delete [] my_space; 92 my_space = NULL; 93 } 94 95 my_len=obj.my_len; 96 for(int i=0;i<my_len;i++) 97 { 98 my_space[i]=obj.my_space[i]; 99 } 100 101 return *this; 102 } 103 104 105 //重载"<<"运算符 106 template<typename T> 107 ostream& operator<<(ostream& out,Myvector<T>& obj) 108 { 109 for(int i=0;i<obj.my_len;i++) 110 { 111 out<<obj.my_space[i]<<" "; 112 } 113 return out; 114 } 115 116 117 //3.main.cpp文件 118 #include <iostream> 119 #include"myvector.cpp" 120 #include"myteacher.h" 121 #include<string> 122 123 using namespace std; 124 125 class Teacher 126 { 127 friend ostream& operator<<(ostream& out,const Teacher& t); 128 public: 129 //构造函数,不带参 130 Teacher() 131 { 132 age=0; 133 name=NULL; 134 } 135 136 //构造函数,带参 137 Teacher(int a,char* n) 138 { 139 age=a; 140 int name_len=strlen(n); 141 name=new char[name_len+1]; 142 strcpy(name,n); 143 } 144 145 //拷贝构造函数 146 Teacher(const Teacher& t) 147 { 148 age=t.age; 149 int name_len=strlen(t.name); 150 name=new char[name_len+1]; 151 strcpy(name,t.name); 152 } 153 154 //析构函数 155 ~Teacher() 156 { 157 cout<<"调用Teacher类的析构函数"<<endl; 158 if(name!=NULL) 159 { 160 age=0; 161 delete [] name; 162 name=NULL; 163 } 164 } 165 166 //重载"="操作符 167 Teacher& operator=(Teacher& t) 168 { 169 if(name!=NULL) 170 { 171 delete [] name; 172 name=NULL; 173 age=0; 174 } 175 176 int name_len=strlen(t.name); 177 name=new char[name_len+1]; 178 strcpy(name,t.name); 179 age=t.age; 180 181 return *this; 182 } 183 184 void printT() 185 { 186 cout<<age<<" "<<name<<endl; 187 } 188 189 190 private: 191 int age; 192 char* name; 193 }; 194 195 ostream& operator<<(ostream& out,const Teacher& t) 196 { 197 out<<t.age<<" "<<t.name; 198 return out; 199 } 200 201 202 int main() 203 { 204 Teacher t1(26,"Wu"),t2(33,"Li"); 205 206 Myvector<Teacher> v(3); 207 208 v[0]=t1; 209 v[1]=t2; 210 211 cout<<v[0]<<endl; 212 cout<<v[1]<<endl; 213 214 215 216 return 0; 217 }
上述代码的执行结果是:
26 Wu
33 Li
调用模板类的析构函数
调用Teacher类的析构函数
调用Teacher类的析构函数
调用Teacher类的析构函数
调用Teacher类的析构函数
调用Teacher类的析构函数
之所以会5次调用Teacher类的析构函数,是因为定义了1个myvector v(3),即v是包含3个元素的数组,定义了2个Teacher类的对象t1和t2。
然后把v[0]=t1,v[1]=t2,则v[2]是空的。执行析构函数的时候,要析构v里面的元素3次,即3次调用Teacher类的析构函数;调用Teacher类对象的析构函数2次,即t1和t2各析构一次,总共析构了2+3=5次。
转载于:https://www.cnblogs.com/jswu-ustc/p/8527597.html
用类模板实现容器存储自定义数据类型(类似于STL里面的vector)相关推荐
- 用类模板实现容器存储普通数据类型(类似于STL里面的vector)
STL里面的很多容器,都是用类模板技术实现的.以vector为例,说明如何使用类模板技术来实现. 1 //1.myvector.h文件 2 #ifndef MYVECTOR_H 3 #define M ...
- java 中间容器 表格_【JAVA SE基础篇】45.迭代器、Collections工具类以及使用容器存储表格...
本文将要为您介绍的是[JAVA SE基础篇]45.迭代器.Collections工具类以及使用容器存储表格,具体完成步骤: 1.迭代器 迭代器为我们提供了统一遍历容器(List/Map/Set)的方式 ...
- 自定义Button,复写里面的onKeyDown,不起作用
李刚的Android疯狂讲义真是"疯狂",浪费了3天时间,到底是他的代码有问题,还是怎么的不得而知. 问题描述:他的书里面第3.3基于回调事件处理Propagation的例程.是为 ...
- C++ STL容器vector篇(一) vector容器存放内置和自定义数据类型并遍历
写在前面 存放内置数据类型 代码 存放自定义数据类型 代码 总结 写在前面 本文算是B站黑马C++课程STL-vector部分的一个总结, 包含自己的一些想法(都在代码里了). 存放内置数据类型 以i ...
- 笔记②:牛客校招冲刺集训营---C++工程师(面向对象(友元、运算符重载、继承、多态) -- 内存管理 -- 名称空间、模板(类模板/函数模板) -- STL)
0618 C++工程师 第5章 高频考点与真题精讲 5.1 指针 & 5.2 函数 5.3 面向对象(和5.4.5.5共三次直播课) 5.3.1 - 5.3.11 5.3.12-14 友元 友 ...
- 深圳大学计软《面向对象的程序设计》实验15 函数模板和类模板
A. 有界数组模板类(类模板) 题目描述 编写有界数组模板BoundArray(即检查对数组元素下标引用并在下标越界时终止程序的执行),能够存储各种类型的数据.要求实现对数组进行排序的方法sort,及 ...
- 【剑仙教程】易语言的结构体。自定义数据类型。
1楼. [剑仙教程]易语言的结构体.自定义数据类型. 在易语言中,有基本数据类型,自定义数据类型. 自定义数据类型,在C语言/C++中称为结构体.这是面向对象和类的编程. . . 先上代码.打开易语言 ...
- C++模板:类模板和类模板的友元【C++模板】(57)
类模板 Stack 类 类模板 格式 类模板的应用 类模板的多文件实现 类模板的友元 类模板中的友元在.h 类模板中的友元在.cpp hpp STL入门 类模板 Stack 类 我们先给出我们之前实现 ...
- C++类模板template <class T>简单使用方法
一个简单的例子 两个数比大小 如果两个数都是int类型 class Compare_int { public :Compare(int a,int b){x=a;y=b;}int max( ){ret ...
最新文章
- [译]Speeding up your PHP scripts
- autocomplete=off inpu属性
- vue按照url地址访问出错404
- 企业数字化转型以“活力”先行
- flex 学习笔记 tree的各个子级的颜色
- JAVA必备——Struts
- JAVA系列---项目打包部署
- comsol和matlab如何连接,comsol与matlab连接
- Solidworks直接打开SWB文件报错怎么办
- Git - 版本穿梭(时光穿梭机)
- Unity 最新UnityWebRequest下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享
- 【密码学】 一篇文章讲透数字证书
- ARM Compiler相关资料汇总
- JavaScript模板引擎Template.js
- 【javascript面向对象之路】让我们一起来坦克大战吧01
- 更改vscode主题里的注释斜体
- Jetty、Netty、Tomcat、Undertow
- SpringBoot应用监控Actuator使用的安全隐患
- jmeter可以做哪些测试?
- 正态分布是离散分布还是连续分布_高中就开始学的正态分布,原来如此重要
热门文章
- 批量修改图片名称(去掉原名字中的中文字符和空格)
- Verilog的6位,8位数码管译码
- 聊一聊基础的CPU寄存器~
- 人工智能科技成熟的11个Github上免费开源项目,很多电影中才有的场景应用到现实颠覆普通人的认知和想象
- 助力中小企业级连云端,促进工业互联网平台蓬勃发展,全套解决方案。附:技术产品...
- 小说作者推荐:渡山山合集
- SpringMVC:通配符的匹配很全面, 但无法找到元素 ‘context:component-scan‘ 的声明
- C++语法学习笔记二十七: 引用折叠,转发、完美转发,forward
- 离散数学 (II) 习题 5
- 校园IPTV数字电视教学直播系统在淮安生态文旅区实验小学的应用