zoukankan      html  css  js  c++  java
  • 用类模板实现容器存储普通数据类型(类似于STL里面的vector)

    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则需要考虑到深拷贝个浅拷贝的问题,需要重载拷贝构造函数。

  • 相关阅读:
    微信小程序日期插件默认获取延后时间示例
    【SpringBoot2 从0开始】底层注解
    【SpringBoot2 从0开始】实现自动配置的过程
    【SpringBoot2 从0开始】开发世界著名程序体验 springboot
    【SpringBoot2 从0开始】springboot 与 spring
    【SpringMVC 从 0 开始】使用注解方式配置 SpringMVC
    【SpringMVC 从 0 开始】异常处理器
    【SpringMVC 从 0 开始】拦截器介绍
    【SpringMVC 从 0 开始】文件上传和下载
    【SpringMVC 从 0 开始】HttpMessageConverter 报文信息转换器
  • 原文地址:https://www.cnblogs.com/jswu-ustc/p/8525280.html
Copyright © 2011-2022 走看看