C++之模版

模版

c++面向对象编程思想:封装、继承、多态
c++泛型编程思想:模板

将功能相同,类型不同的函数(类)的类型抽象成虚拟的类型。当调用函数(类实例化对象)的时候,编译器自动将虚拟的类型具体化。这个就是函数模板(类模板)

1、函数模版

#include <iostream>

using namespace std;


template<typename T>
void swapAll(T &a,T &b){
    T temp=a;
    a=b;
    b=temp;
    return;
}

int main()
{
    int a=10;
    int b=20;
    swapAll(a,b);
    char aa='a';
    char bb='b';
    swapAll(aa,bb);
    return 0;
}

函数模板会编译两次:
第一次:是对函数模板 本身编译
第二次:函数调用处 将T的类型具体化
函数模板目标:模板是为了实现泛型,可以减轻编程的工作量,增强函数的重用性。

注意点:1、函数模板 和 普通函数 都识别。(优先选择 普通函数)

#include <iostream>

using namespace std;


void swapAll(int &a,int &b){
     cout<<"普通函数"<<endl;
    int temp=a;
    a=b;
    b=temp;
    return;
}

template<typename T>
void swapAll(T &a,T &b){
        cout<<"模版函数"<<endl;
    T temp=a;
    a=b;
    b=temp;
    return;
}

int main()
{
    int a=10;
    int b=20;
    swapAll(a,b);

    return 0;
}

普通函数

2、函数模板 和 普通函数 都识别。强制使用函数模板

    swapAll<>(a,b);//强制使用模版函数

3、函数模板自动类型推导时不能对函数的参数 进行自动类型转换。

#include <iostream>

using namespace std;

void printInt(int a,int b){
    cout<<"普通函数"<<endl;
    cout<<a<<' '<<b<<endl;
}



template<typename T>
void printInt(T a,T b){
        cout<<"模版函数"<<endl;
        cout<<a<<' '<<b<<endl;
}

int main()
{

    printInt(10,20);//普通函数
       printInt('a','b');//模版函数


//模版函数并不支持自动类型转换

    printInt(10,'a');//普通函数
    
//函数模板显示指明T的具体类型 这时函数模板的参数 可以自动类型转换。
    printInt<int>(10,'a');//模版函数


    return 0;
}

4、函数模板可以重载

template<typename T>
void printInt(T a){
    cout<<"模版函数"<<endl;
    cout<<a<<endl;
}

template<typename T>
void printInt(T a,T b){
        cout<<"模版函数"<<endl;
        cout<<a<<' '<<b<<endl;
}
int main()
{

    printInt(10,20);
    printInt(10);


    return 0;
}

5、模板的局限性

当函数模板推导出 T为数组或其他自定义类型数据 可能导致运算符不识别

解决办法一:运算符重载(推荐)

class Person{
    friend ostream &operator<<(ostream &out,Person p);
private:
    int age;
public:
    Person(int a){
        age=a;
    }

};
//重载输出运算符<<
ostream &operator<<(ostream &out,Person p){
    out<<p.age<<endl;
}

template<typename T>
void printInt(T a){
    cout<<"模版函数"<<endl;
    cout<<a<<endl;
}



int main()
{
    Person p(10);
    printInt(p);

    return 0;
}

解决办法二:具体化函数模板

template<typename T>
void printInt(T a){
    cout<<"模版函数"<<endl;
    cout<<a<<endl;
}


class Person{
//void printInt(T a)要在class Person前声明
  friend void printInt<Person>(Person a);//声明为友元函数不需要带上template<>
private:
    int age;
public:
    Person(int a){
        age=a;
    }

};


//函数模版具体化  --根据需要可以具体化
template <> void printInt<Person>(Person p){
    cout<<p.age<<endl;
}

int main()
{
    Person p(10);
    printInt(p);
    return 0;
}

2、类模版

类模版的定义

类模板 将类中类型 抽象成虚拟类型。

类模板 实例化对象 不能自动类型推导(重要)

template<class T1,class T2>
class Data{
private:
    T1 a;
    T2 b;
public:
    Data(){
        cout<<"无参构造"<<endl;
    }
    Data(T1 a,T2 b){
        this->a=a;
        this->b=b;

    }
    void showData(){
        cout<<a<<" "<<b<<endl;
    }
};

int main()
{
    //必须先指明类型,不能自动类型推导
    Data<int ,int> ob(10,20);
    ob.showData();

    return 0;
}

类模板的成员函数在类外实现

template<class T1,class T2>
class Data{
private:
    T1 a;
    T2 b;
public:
    Data(){
        cout<<"无参构造"<<endl;
    }
    Data(T1 a,T2 b){
        this->a=a;
        this->b=b;

    }
    void showData();
};
template<class T1, class T2>
void Data<T1, T2>::showData()
{
    cout<<a<<" "<<b<<endl;
}

函数模板作为类模板的友元

template<class T1,class T2>
class Data{
    template <class T3,class T4>//如果是T1和T2就与Data类的参数同名了,需要指明类模板参数
    friend void showData(Data<T3,T4> ob);//注意friend的位置
private:
    T1 a;
    T2 b;
public:
    Data(){
        cout<<"无参构造"<<endl;
    }
    Data(T1 a,T2 b){
        this->a=a;
        this->b=b;

    }

};
template <typename T3,typename T4>
void showData(Data<T3,T4> ob){
    cout<<ob.a<<" "<<ob.b<<endl;
}

int main()
{
    //必须先指明类型,不能自动类型推导
    Data<int,int> ob(10,20);
    showData(ob);

    return 0;
}

普通函数作为类模板的友元

template<class T1,class T2>
class Data{
    template <class T3,class T4>//如果是T1和T2就与Data类的参数同名了
    friend void showData(Data<T3,T4> ob);

    friend void myPrintData(Data<int,char> &ob);
private:
    T1 a;
    T2 b;
public:
    Data(){
        cout<<"无参构造"<<endl;
    }
    Data(T1 a,T2 b){
        this->a=a;
        this->b=b;

    }

};

//模版函数作为模版类的友元
template <typename T3,typename T4>
void showData(Data<T3,T4> ob){
    cout<<ob.a<<" "<<ob.b<<endl;
}

//普通函数作为模版类的友元
void myPrintData(Data<int,char> &ob){

    cout<<ob.a<<" "<<ob.b<<endl;

}
int main()
{
    //必须先指明类型,不能自动类型推导
    Data<int,int> ob(10,20);
    showData(ob);
    Data<int,char> ob2(10,'a');
    myPrintData(ob2);

    return 0;
}

类模板的继承:

1、类模板派生出普通类
template<class T1,class T2>
class Base{
private:
    T1 a;
    T2 b;
public:
    Base(){
        cout<<"无参构造"<<endl;
    }
    Base(T1 a,T2 b){
        this->a=a;
        this->b=b;
    }
    void showData(){
        cout<<a<<" "<<b<<endl;
    }

};
//类模板派生处普通类
class Son1:public Base<int,char>{
public:
    int  c;
public:
    Son1(int a ,char b,int c):Base<int,char>(a,b){//调用父类的构造方法
        this->c=c;

    }
};



int main()
{
    Son1 ob(10,'a',100);
    ob.showData();
    cout<<ob.c<<endl;
    return 0;
}

2、类模板派生出类模板
template<class T1,class T2>
class Base{
private:
    T1 a;
    T2 b;
public:
    Base(){
        cout<<"无参构造"<<endl;
    }
    Base(T1 a,T2 b){
        this->a=a;
        this->b=b;
    }
    void showData(){
        cout<<a<<" "<<b<<endl;
    }

};
//类模板派生处普通类
class Son1:public Base<int,char>{
public:
    int  c;
public:
    Son1(int a ,char b,int c):Base<int,char>(a,b){
        this->c=c;

    }
};


//类模版派生出类模版
template <class T1,class T2,class T3>
class Son2:public Base<T1,T2>{
public :
    T3 c;
public:
    Son2(T1 a,T2 b,T3 c):Base<T1,T2>(a,b){
        this->c=c;
    }
};


int main()
{
    Son2<int ,char ,int> ob(10,'a',100);
    ob.showData();
    cout<<ob.c<<endl;
    return 0;
}

类模板头文件和源文件分离问题

类模板最好存储在.hpp中(.h和.cpp的结合)

data.hpp

#ifndef DATA_H
 #define DATA_H
 #include<iostream>
 using namespace std;
 template<class T1, class T2>
 class Data
 {
 private:
 T1 a;
 T2 b;
 public:
 Data();
 Data(T1 a, T2 b);
 void showData(void);
 };
 
//类外实现
 template<class T1, class T2>
 Data<T1, T2>::Data()
 {
 cout<<"无参构造"<<endl;
 }
 template<class T1, class T2>
 Data<T1, T2>::Data(T1 a, T2 b)
 {
 this‐>a = a;
 this‐>b = b;
 }
 template<class T1, class T2>
 void Data<T1, T2>::showData(void)
 {
 cout<<a<<" "<<b<<endl;
 }
 #endif // DATA_H

main.cpp中

 #include <iostream>
 #include"data.hpp" #引入类模板.hpp
 using namespace std;

 int main(int argc, char *argv[])
 {
 Data<int,char> ob1(100,'A');
 ob1.showData();
 return 0;
 }

设计数组类模板

MyArray.hpp

#ifndef MYARRAY_H
#define MYARRAY_H
#include <iostream>
#include <string>
#include <string.h>
using  namespace std;


template<class T>
class MyArray{

    template<typename T1>
    friend ostream& operator<<(ostream &out,MyArray<T1> ob);

private:
    T *arr;
    int size;//大小
    int capcity;//容量
public:
    MyArray();
    MyArray(int  capacity);
    MyArray(const MyArray &ob);//拷贝函数
    ~MyArray();//析构函数

    MyArray &operator=(MyArray &ob);//重构=

    void pushBack(T elem);//从数组末尾插入元素
    void sortArray();//对数组进行排序

};
#endif // MYARRAY_H

//类外实现
template<class T>
MyArray<T>::MyArray(){
    capcity=0;
    size=0;
    arr=NULL;
}

template<class T>
MyArray<T>::MyArray(int capacity){
    this->capcity=capacity;
    size=0;
    arr=new T[this->capcity];
    memset(arr,0,sizeof(T)*capacity);
}

template<class T>
MyArray<T>::MyArray(const MyArray &ob){
    if(ob.arr==NULL){
        arr=NULL;
        size=0;
        capcity=0;
    }
    else{
        capcity=ob.capcity;
        size=ob.size;
        arr=new T[capcity];;
        memcpy(arr,ob.arr,sizeof(T)*capcity);
    }
}

template<class T>
MyArray<T>::~MyArray(){
    if(arr!=NULL) delete[]arr;
}




template<class T>
void MyArray<T>::pushBack(T elem){


    if(size==capcity){
        //满了
        capcity=(capcity==0?1:2*capcity);
        T *temp=new T[capcity];
        if(arr!=NULL){
            memcpy(temp,arr,sizeof(T)*capcity);
            delete[]arr;
        }
        arr=temp;
    }


    arr[size++]=elem;
    return;
}


template<class T>
MyArray<T> &MyArray<T>::operator=(MyArray<T> &ob){
    if(arr!=NULL){
        delete[] arr;
        arr=NULL;
    }
    size=ob.size;
    capcity=ob.capcity;
    arr=new T[capcity];

    memset(arr,0,sizeof(T)*capcity);
    memcpy(arr,ob.arr,sizeof(T)*capcity);
    return *this;
}


template<class T>
void MyArray<T>::sortArray(){

    if(arr==NULL){
        cout<<"容器为空"<<endl;
    }
    else{

        int i=0,j=0;
        for(i=0;i<size-1;i++){
            for(j=0;j<size-i-1;j++){
                if(arr[j]>arr[j+1]){
                    T temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
    }
    return;
}

template<typename T1>
ostream& operator<<(ostream &out,MyArray<T1> ob){
    int i=0;
    for(i=0;i<ob.size;i++){
        out<<ob.arr[i]<<" ";
    }
    return out;
}

main.cpp

#include"MyArray.hpp"

class Person{
    friend ostream &operator<<(ostream &out,Person ob);
private:
    int num;
    string name;
    float score;
public:
    Person(){}
    Person(int num,string name,float score){
        this->num=num;
        this->name=name;
        this->score=score;
    }
    bool operator>(const Person &ob){
        return num>ob.num;
    }

};

ostream& operator<<(ostream &out,Person ob){
    int  i=0;
    out<<ob.num<<" "<<ob.name<<" "<<ob.score<<endl;
    return out;
}

int main()
{
     MyArray<int> ob1(5);
     ob1.pushBack(10);
     ob1.pushBack(30);
     ob1.pushBack(20);
     ob1.pushBack(50);
     ob1.pushBack(40);

     cout<<ob1<<endl;
     ob1.sortArray();
     cout<<ob1<<endl;

    MyArray<char> ob2(5);
     ob2.pushBack('A');
     ob2.pushBack('C');
     ob2.pushBack('D');
     ob2.pushBack('B');
     ob2.pushBack('F');

     cout<<ob2<<endl;
     ob2.sortArray();
     cout<<ob2<<endl;
     MyArray<Person> ob3(5);
     ob3.pushBack(Person(100,"lucy", 88.8f));
     ob3.pushBack(Person(103,"bob", 89.9f));
     ob3.pushBack(Person(105,"tom", 98.8f));
     ob3.pushBack(Person(102,"德玛", 99.8f));
     cout<<ob3<<endl;
     ob3.sortArray();
     cout<<ob3<<endl;




    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值