• C++ 12:函数模板,模板函数,类模板



    1. 函数模板

    1.1 函数模板特点

    (1)函数模板可以用来创建一个通用功能的函数,以支持多种不同形参,简化重载函数的设计。函数模板定义如下:template<模板参数表>返回类型函数名(形式参数表) {.....; }//函数体

    (2)<模板参数表>(template parameter list)尖括号中不能为空,参数可以有多个,用逗号分开。
    模板参数主要是模板类型参数。

    (3)模板类型参数(template type parameter)代表一种类型,由关键字class或typename(建议用typename )后加一个标识符构成,在这里两个关键字的意义相同,它们表示后面的参数名代表一个潜在的内置或用户定义的类型。

    (4)函数模板根据一组实际类型或(和)值构造出独立的函数的过程通常是隐式发生的,称为模板实参推演(templateargument deduction)。

    (5)为了判断用作模板实参的实际类型,编译器需检查函数调用中提供的函数实参的类型。ia 的类型为int数组,dx的类型为double 数组。都被用来决定每个实例的模板参数。该过程称为模板实参推演。

    1.2 代码示例

    1.2.1 例1:类型推演

    代码示例

    #include<iostream
    #include
    using namespace std;
    template<class T>
    void fun(T a)
    {
        T x, y;
        cout << "T type:" << typeid(T).name() << endl;
        cout << "a type:" << typeid(a).name() << endl;
    }
    int main()
    {
        //int x = 10;//int     int*
        //const int x=10;//int     const int*
        /*fun(x);
        fun(&x);*/
        /*
        int x=10;
        const int y=10;
        int*xp=&x;
        const int*yp=&y;
        fun(xp);
        fun(yp);
         //int*   const int* */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行结果
    在这里插入图片描述

    1.2.2 例2:类型推演失败

    因为明确给出需要指针类型,给出的是整型,类型不匹配,推演失败,函数都可以重载

    using namespace std;
    template<class T>
    void fun(T* a)//必须是指针所有推演失败 部分泛化
    {
        T x, y;
        cout << "T type: " << typeid(T).name() << endl;
        cout << "a type: " << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 10;
        fun(x);
        fun(y);
        //推演失败解决办法:加引用
        fun(&x);//int   int*
        fun(&y);//int   const int*
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    1.2.3 例3:类型推演

    template<class T>
    void fun(T& a)
    {
        T x, y;
        cout << "T type: " << typeid(T).name() << endl;
        cout << "a type: " << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 10;
        int* xp = &x;
        const int* yp = &y;
        fun(x);
        fun(y);
        fun(&x);
        fun(&y);
        fun(xp);
        fun(yp);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2. 模板函数

    2.1 定义

    在编译main()函数中,由编译函数模板(functron template)而生成的函数,称为模板函数(template function).这两个概念须分清楚。

    2.2 泛化

    void fun(T a)//完全泛型
    void fun(T *a);//部分泛型
    void fun<char>(const char* a)//完全特化
    
    • 1
    • 2
    • 3

    2.3 模板类型推演示例

    2.3.1 类型推演示例

    template<class T>
    void fun(T a)
    {
        T x, y;
        cout << "T type: " << typeid(T).name() << endl;
        cout << "a type: " << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        int* xp = &x;
        const int* yp = &y;
        fun(x);//int           int
        fun(y);//int           int
        fun(&x);//int*         int*
        fun(&y);//const int*   const int*
        fun(xp);//int*         int*
        fun(yp);//const int*   const int*
        fun(&xp);//int**       int**
        fun(&yp);//const int** const int**
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2.3.2 推演失败案例,类型不匹配

    template<typename T>
    void fun(T* a)
    {
        T x, y;
        cout << typeid(T).name() << endl;
        cout << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        fun(x);//推演失败(类型不匹配)
        fun(y);//推演失败
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.3.3 无类型指针示例

    代码示例

    template<typename T>
    void fun(T* a)
    {
        T x, y;
        cout << typeid(T).name() << endl;
        cout << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        int* xp = &x;
        const int* yp = &y;
        void* p = &x;//无类型指针
        //带有类型萃取能力
        fun(&x);//int          int*
        fun(&y);//const int    const int*
        fun(p);//void          void*    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    无类型无法定义变量

    2.4.模板函数的引用

    代码示例

    template<typename T>
    void fun(T& a)//引用是和变量名结合,不是和类型名结合
    {
        T x = 0, y = 0;//进行初始化才能编译通过
        cout << typeid(T).name() << endl;
        cout << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        fun(x);//int         int&
        fun(y);//const int   const int&
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果
    在这里插入图片描述

    2.5 引用与指针结合

    代码示例

    template<typename T>
    void fun(T& a)//引用是和变量名结合,不是和类型名结合
    {
        T x = 0, y = 0;//进行初始化才能编译通过
        cout << typeid(T).name() << endl;
        cout << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        int* xp = &x;
        const int* yp = &y;
        fun(xp);//int*          int*&
        fun(yp);//const int*    const int*&
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行结果
    在这里插入图片描述

    2.6 引用与二级指针结合

    template<typename T>
    void fun(T const& a)//引用是和变量名结合,不是和类型名结合
    {
        T x = 0, y = 0;//进行初始化才能编译通过
        cout << typeid(T).name() << endl;
        cout << typeid(a).name() << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        fun(&x);//int* int const*
        fun(&y);//int* int const*
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    2.7 模板函数类型推演示例

    代码示例

    template<class T>
    void fun(T a)
    {
        cout << "T a" << endl;
    }
    
    template<class T>
    void fun(T* a)
    {
        cout << "T *a" << endl;
    }
    
    template<class T>
    void fun(const T* a)
    {
        cout << "const T*" << endl;
    }
    int main()
    {
        int x = 10;
        const int y = 20;
        fun(x);//T
        fun(&x);//T*
        fun(&y);//const T*
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行结果

    在这里插入图片描述

    2.8 模板函数的重载

    template<class T>
    void fun(T a, int)
    {
    
    }
    template<class T>
    void fun(T a, char)
    {
    
    }
    int main()
    {
        fun(12, 23);
        fun(12, 'a');
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3. 类模板

    3.1 概念

    (1)类模板不存在推演过程
    (2)类模板中的函数都是模板函数

    代码示例

    void push(const T& x)
    {
    	cout<<N<<endl;
    	N=100;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2 代码示例

    //类模板
    template<class T>
    class stack
    {
        T* data;
        size_t count;
    public:
        void Push(const T& x);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.3 模板函数类型

    template<class T,int N>
    class Stack
    {
        T data[N];
    public:
        void push(const T& x);
    };
    int main()
    {
        //is和ist类型不相同
        Stack<int, 100> is;
        Stack<int, 10> ist;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.4 模板函数中的new

    template<class T>
    class Array
    {
        enum { INIT = 10 };
        T* data;//.heap 10
        size_t capacity;//10
        size_t count;//5
    public:
        Array()
        {
            count = 0;
            data = new T[capacity = INIT];//连续开辟数组,调动构造函数,对每个空间构造函数
        }
        ~Array() { delete[]data; }
    };
    int main()
    {
        Array<int> ar;
        Array<double> dr;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3.5 模板类型识别

    int main()
    {
        Array<int> a;//不是模板类型,已经被实例化
        Array b;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.6 综合代码示例

    #define _CRT_SECURE_NO_WARNINGS
    #include"stdio.h"
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    class String
    {
    protected:
        struct StrNode
        {
            int ref;  // 引用计数
            int len;  // 字符串的长度
            int size; // 柔性数组的容量
            char data[];
        };
    private:
        StrNode* pstr;
    public:
        String(const char* p = NULL) :pstr(NULL)
        {
            if (p != NULL)
            {
                int sz = strlen(p);
                pstr = (StrNode*)malloc(sizeof(StrNode) + sz * 2 + 1);
                pstr->ref = 1;
                pstr->len = sz;
                pstr->size = sz * 2;
                strcpy(pstr->data, p);
            }
        }
        ~String()
        {
            if (pstr != NULL && --pstr->ref == 0)
            {
                free(pstr);
            }
            pstr = NULL;
        }
        String(const String& str) :pstr(NULL)
        {
            if (str.pstr != NULL)
            {
                pstr = str.pstr;
                pstr->ref += 1;
            }
        }
        String& operator=(const String& s)
        {
            if (this == &s) return *this;
            if (this->pstr != NULL && --this->pstr->ref == 0)
            {
                free(this->pstr);
            }
            this->pstr = s.pstr;
            if (this->pstr != NULL)
            {
                this->pstr->ref += 1;
            }
            return *this;
        }
    
        char& operator[](const int index)
        {
            //写时拷贝
            if (pstr == NULL) exit(1);
            assert(index >= 0 && index <= pstr->len - 1);
            //越界问题
            if (pstr->ref > 1)
            {
                int total = sizeof(StrNode) + pstr->size + 1;
                StrNode* newnode = (StrNode*)malloc(total);
                memmove(newnode, pstr, total);
                newnode->ref = 1;
                pstr->ref -= 1;
                pstr = newnode;
            }
            return pstr->data[index];
        }
        const char& operator[](const int index) const
        {
            return (*this)[index];
        }
        ostream& operator<<(ostream& out) const
        {
            if (pstr != NULL)
            {
                out << pstr->data;
            }
            return out;
        }
        String(String&& s);//移动构造
        String& operator=(String&& s);//移动赋值
    };
    ostream& operator<<(ostream& out, const String& s)
    {
        s << out;
        return out;
    }
    int main()
    {
        String s1("yhping");
        String s2(s1);
    
        String s3("hello");
        String s4("new");
    
        //s1 = s1 + s4;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

  • 相关阅读:
    微服务 第二章 CountDownLatch和Semaphone的应用
    实例方法和静态方法有区别吗?
    鹅厂3.5亿打水漂,又一款产品黄了
    存储数据恢复- raid5多块硬盘出现坏道的数据恢复案例
    如何制作gif动图gif (多图合成gif、GIF录制软件、视频制作成GIF动图)
    现代企业架构框架 — 业务架构
    365天挑战LeetCode1000题——Day 089 删除某些元素后的数组均值 设计位集 最大加号标志
    前端学习笔记 第1集:WebStorm 安装、vue3.0 安装、npm安装
    【LeetCode:637. 二叉树的层平均值 | bfs】
    1、Flutter移动端App实战教程【环境配置、模拟器配置】
  • 原文地址:https://blog.csdn.net/qq_48580892/article/details/125992947