c++,模板

模板应用案例

template<typename T>

void swapl(T &a,T &b)

{

    T temp=a;

    a=b;

    b=temp;

}

template<typename T>

void mysort(T arr[],int len)

{

    for(int i=0;i<len;i++)

    {

        int max=i;

        for(int j=i+1;j<len;j++)

        {

            if(arr[max]<arr[j])

            {

                max=j;

            }

        }

        if(max!=i)

            {

                swapl(arr[i],arr[max]);

            }

    }

}

template<typename T>

void printf(T arr[],int len)

{

    for(int i=0;i<len;i++)

    {

        cout<<arr[i]<<" ";

    }

    cout<<endl;

}

void test()

{

    char chararr[]="abdfre";

    int num=sizeof(chararr)/sizeof(char);

    mysort(chararr,num);

    printf(chararr,num);

}

int main()

{

    test();

    system("pause");

    return 0;

}

函数模板

template<typename T>

T myadd(T a,T b)

{

    return a+b;

}

void test()

{

    int a=1;

    int b=2;

    char c='c';

    cout<<myadd<int>(a,c)<<endl;

}

int main()

{

    test();

    system("pause");

    return 0;

}

 并不是所有的都可以直接用函数

class Person

{

public:

    Person(string name,int age)

    {

        this->m_age=age;

        this->m_name=name;

    }

    string m_name;

    int m_age;

};

template<typename T>

bool mycompare(T &a,T &b)

{

    if(a==b)

    {

        return true;

    }

    else

    {

        return false;

    }

}

template<> bool mycompare(Person &p1,Person &p2)

{

    if(p1.m_name==p2.m_name&&p1.m_age==p2.m_age)

    {

        return true;

    }

    else

    {

        return false;

    }

}

void test01()

{

    int a=10;int b=20;

    bool ret=mycompare(a,b);

    if(ret)

    {

        cout<<"niniu";

    }

    else

    {

        cout<<"lala";

    }

}

void test02()

{

    Person p1("to",10);

    Person p2("to",10);

    bool ret=mycompare(p1,p2);

    if(ret)

    {

        cout<<"6";

    }

    else

    {

        cout<<"88";

    }

}

int main()

{

    test02();

    system("pause");

    return 0;

}

类模板

template<class NameType,class AgeType>

class Person

{

public:

    Person(NameType name,AgeType age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    void showPerson()

    {

        cout<<this->m_Name<<" "<<this->m_Age<<endl;

    }

    NameType m_Name;

    AgeType m_Age;

};

test01()

{

    Person <string,int>p("张子豪",999);

    p.showPerson();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板对象做函数参数

class Person1

{

public:

    void showPerson1()

    {

        cout<<"Person1 show"<<endl;

    }

};

class Person2

{

public:

    void showPerson2()

    {

        cout<<"Person2 show"<<endl;

    }

};

template<class T>

class MyClass

{

public:

    T obj;

    void func1()

    {

        obj.showPerson1();

    }

    void func2()

    {

        obj.showPerson2();

    }

};

void test01()

{

    MyClass<Person1>m;

    m.func1();

    //m.func2();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板对象做函数参数

template<class T1,class T2>

class Person

{

public:

    Person(T1 name,T2 age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    void showPerson()

    {

        cout<<"姓名"<<this->m_Name<<" "<<"年龄"<<this->m_Age<<endl;

    }

    T1 m_Name;

    T2 m_Age;

};

void printPerson1(Person<string,int>&p)

{

    p.showPerson();

}

void test01

类模板与继承

template<class T>

class Base

{

    T m;

};

class Son:public Base<int>

{

public:

};

void test01()

{

    Son s1;

}

template<class T1,class T2>

class Son2:public Base<T2>

{

public:

    Son2()

    {

        cout<<typeid(T1).name();

    }

    T1 obj;

};

void test02()

{

    Son2<int ,char>S2;

}

int main()

{

    test02();

    system("pause");

    return 0;

}

类模板成员函数类外实现

template<class T1,class T2>

class Person

{

public:

    Person(T1 name,T2 age);

    /*{

        this->m_Name=name;

        this->m_Age=age;

    }*/

    void showPerson();

    /*{

        cout<<this->m_Name<<" "<<this->m_Age<<endl;

    }*/

    T1 m_Name;

    T2 m_Age;

};

template<class T1,class T2>

Person<T1,T2>::Person(T1 name,T2 age)

{

    this->m_Name=name;

    this->m_Age=age;

}

template<class T1,class T2>

void Person<T1,T2>::showPerson()

{

    cout<<this->m_Name<<" "<<this->m_Age<<endl;

}

void test01()

{

    Person<string,int>p("tom",20);

    p.showPerson();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板用

Hpp

类模板友元

#include <iostream>

#include<malloc.h>

#include<stdio.h>

#include <fstream>//读写文件

#define MAX 1000

#include <string>

#include<typeinfo>

#define FILE "s111.txt"

using namespace std;

template<class T1,class T2>

class Person

{

    friend void printPerson(Person<T1,T2> p)

    {

        cout<<p.m_Name<<" "<<p.m_Age<<endl;

    }

public:

    Person(T1 name,T2 age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

private:

    T1 m_Name;

    T2 m_Age;

};

void test01()

{

    Person<string,int>p("tom",20);

    printPerson(p);

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板案例,数组类封装

#include <iostream>

#include<malloc.h>

#include<stdio.h>

#include <fstream>//读写文件

#include <string>

#include<typeinfo>

#define FILE "s111.txt"

using namespace std;

template<class T>

class MyArray

{

public:

    MyArray(int capacity)

    {

        cout<<"有参构造"<<endl;

        this->m_Capacity=capacity;

        this->m_Size=0;

        this->pAddress=new T[this->m_Capacity];

    }

    MyArray(const MyArray&arr)

    {

        cout<<"拷贝构造"<<endl;

        this->m_Capacity=arr.m_Capacity;

        this->m_Size=arr.m_Size;

        this->pAddress=new T[arr.m_Capacity];

        for(int i=0;i<this->m_Size;i++)

        {

            this->pAddress[i]=arr.pAddress[i];

        }

    }

    MyArray &operator=(const MyArray&arr)

    {

        cout<<"operator构造"<<endl;

        if(this->pAddress!=NULL)

        {

            delete[]this->pAddress;

            this->pAddress=NULL;

            this->m_Capacity=0;

            this->m_Size=0;

        }

        this->m_Capacity=arr.m_Capacity;

        this->m_Size=arr.m_Size;

        this->pAddress=new T[arr.m_Capacity];

        for(int i=0;i<this->m_Size;i++)

        {

            this->pAddress[i]=arr.pAddress[i];

        }

        return *this;

    }

    void Push_Back(const T&val)

    {

        if(this->m_Capacity==this->m_Size)

        {

            return;

        }

        this->pAddress[this->m_Size]=val;//在数组末尾插入数据

        this->m_Size++;

    }

    void pop_Back()

    {

        if(this->m_Size==0)

        {

            return;

        }

        this->m_Size--;

    }

    T &operator[](int index)

    {

        return this->pAddress[index];

    }

    int getCapacity()

    {

        return this->m_Capacity;

    }

    int getSize()

    {

        return this->m_Size;

    }

    ~MyArray()

    {

        if(this->pAddress!=NULL)

        {

            cout<<"析构"<<endl;

            delete[] this->pAddress;

            this->pAddress=NULL;

        }

    }

private:

    T*pAddress;

    int m_Capacity;

    int m_Size;

};

void printInArray(MyArray<int>&arr)

{

    for(int i=0;i<arr.getSize();i++)

    {

        cout<<arr[i]<<endl;

    }

}

void test01()

{

    MyArray <int>arr1(5);

    for(int i=0;i<5;i++)

    {

        arr1.Push_Back(i);

    }

    cout<<"输出"<<endl;

    printInArray(arr1);

    cout<<"容量"<<arr1.getCapacity()<<endl;

    cout<<"arr1的大小"<<arr1.getSize()<<endl;

    MyArray <int>arr2(arr1);

    cout<<"输出2"<<endl;

    printInArray(arr2);

    arr2.pop_Back();

    cout<<"arr尾删除后"<<endl;

    cout<<"容量"<<arr2.getCapacity()<<endl;

    cout<<"arr的大小"<<arr2.getSize()<<endl;

}

class Person

{

public:

    Person(){};

    Person(string name,int age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    string m_Name;

    int m_Age;

};

void printPersonArray(MyArray<Person>&arr)

{

    for(int i=0;i<arr.getSize();i++)

    {

        cout<<"姓名"<<arr[i].m_Name<<"年龄"<<arr[i].m_Age<<endl;

    }

}

void test02()

{

    MyArray<Person> arr(10);

    Person p1("孙悟空",999);

    Person p2("韩信",999);

    Person p3("啦啦啦信",999);

    Person p4("韩222信",999);

    Person p5("啦啦20132啦信",999);

    arr.Push_Back(p1);

    arr.Push_Back(p2);

    arr.Push_Back(p3);

    arr.Push_Back(p4);

    arr.Push_Back(p5);

    printPersonArray(arr);

    cout<<"array的容量"<<arr.getCapacity()<<endl;

    cout<<"arr的容量大小"<<arr.getSize()<<endl;

}

int main()

{

    test02();

    system("pause");

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木火火木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值