码迷,mamicode.com
首页 > 其他好文 > 详细

用类模板实现容器(STL里面的vector)

时间:2018-03-08 00:00:10      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:using   ==   print   etl   const   strcpy   name   for   names   

STL里面的很多容器,都是用类模板技术实现的。以vector为例,说明如何使用类模板技术来实现。

  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;
 25 };
 26 
 27 #endif
 28 
 29 
 30 
 31 //2.myvector.cpp文件
 32 #include <iostream>
 33 #include "myvector.h"
 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 //析构函数
 65 template<typename T>
 66 Myvector<T>::~Myvector()
 67 {
 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         delete [] my_space;
 91         my_space = NULL;
 92     }
 93 
 94     my_len=obj.my_len;
 95     for(int i=0;i<my_len;i++)
 96     {
 97         my_space[i]=obj.my_space[i];
 98     }
 99 
100     return *this;
101 }
102 
103 
104 //重载"<<"运算符
105 template<typename T>
106 ostream& operator<<(ostream& out,Myvector<T>& obj)
107 {
108     for(int i=0;i<obj.my_len;i++)
109     {
110         out<<obj.my_space[i]<<" ";
111     }
112     return out;
113 }
114 
115 
116 
117 //main.cpp文件
118 #include <iostream>
119 #include"myvector.cpp"
120 
121 using namespace std;
122 
123 int main()
124 {
125     Myvector<int> v1(10);
126     for(int i=0;i<v1.getlen();i++)
127     {
128         v1[i]=i;
129     }
130     cout<<v1<<endl;
131 
132     
133     Myvector<int> v2=v1;
134     cout<<v2<<endl;
135     
136     
137     Myvector<int> v3(v1);
138     cout<<v3<<endl;
139     
140 
141     return 0;
142 }

 以上是操作只能在myvector里面存储一般的数据类型,如int,double,char等,存储自定义的Teacher类型时,则要注意:

假如Teacher类型定义如下:

class Teacher
{
public:
Teacher(int a=30,char* n="Zhang")
{
age=a;
strcpy(name,n);
}
void printT()
{
cout<<age<<" "<<name<<endl;
}

private:
int age;
char name[20];
};

1.myvector存储这种自定义的类型时,需要重载和"="和"<<"操作符。

2.上面的Teacher类型中的name占20个字节,如多定义成char *name则需要考虑到深拷贝个浅拷贝的问题,需要重载拷贝构造函数。

 

用类模板实现容器(STL里面的vector)

标签:using   ==   print   etl   const   strcpy   name   for   names   

原文地址:https://www.cnblogs.com/jswu-ustc/p/8525280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!