• C++程序设计教程笔记之模板入门


    C++程序设计教程笔记之模板入门

    live long and prosper

    C++为程序员提供模板,让程序员可以编写模板接受不同类型的数据。例如在下面的例子中,编写了一个在模板下交换值的函数,让函数可以同时接受不同类型的数据。

    1、程序通式

    template<typename T>//typename也可以替换为class
    void swap(T &a,T &b){
        T temp;
        temp=a;a=b;b=temp;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    上面的代码块第1行是编写模板的语句格式,尖括号中的typename指示数据类型,T是一个通用的数据类型,可以认为是整型,当然也可认为是浮点型。
    在swap函数中,为了接收不同类型的数据,都将形参指定为T类型,同时编写程序时,将模板语句与函数写在一起方便编译器识别。

    2、使用函数模板

    (1)、自动类型推导

    在调用模板函数时,直接调用,由编译器识别数据类型。

    #include
    using namespace std;
    
    template<class T>
    void Swap(T &a,T &b){
        T temp;
        temp=a;a=b;b=temp;
    }
    
    void test(){//测试函数
        int a=10,b=20;
        Swap(a,b);//自动类型推导
        cout<<"a: "<<a<<endl;
        cout<<"b: "<<b<<endl;
    }
    
    int main()
    {
        test();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    (2)、显式指定类型

    直接在调用函数时告诉编译器数据的类型。

    #include
    using namespace std;
    
    template<class T>
    void Swap(T &a,T &b){
        T temp;
        temp=a;a=b;b=temp;
    }
    
    void test(){//测试函数
        int a=10,b=20;
        Swap<int>(a,b);//显式指定类型
        cout<<"a: "<<a<<endl;
        cout<<"b: "<<b<<endl;
    }
    
    int main()
    {
        test();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3、使用模板式的注意事项

    (1)、模板的数据类型必须确定

    我什么说必须确定,是因为如果模板函数不含有数据类型的对象,假如只要输出一串字符,那么这时编译器报错

    #include
    using namespace std;
    
    template<class T>
    void func(){
        cout<<"func(function) is running."<<endl;
    }
    
    void test(){
        func();//couldn't deduce template parameter 'T'
    }
    
    int main()
    {
        test();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在第10行的地方编译器报错,编译器无法识别模板函数的数据类型。

    (2)、数据类型必须一致

    模板函数中使用到的T数据类型参数,它们的实参的数据类型必须一致,下面test函数中有两个整型的a、b,还有一个字符型的c。数据类型不一致。

    #include
    using namespace std;
    
    template<class T>
    void Swap(T &a,T &b){
        T temp;
        temp=a;a=b;b=temp;
    }
    
    void test(){
        int a=10,b=20;
        char c="c";
        Swap(a,c);//deduced conflicting types for parameter 'T' ('int' and 'char')
        cout<<"a: "<<a<<endl;
        cout<<"b: "<<b<<endl;
        cout<<"c: "<<c<<endl;
    }
    
    int main()
    {
        test();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    编译器会提示:

    没有与参数列表匹配的 函数模板 “Swap” 实例。

  • 相关阅读:
    STM32WB55开发(2)----修改蓝牙地址
    FreeRTOS入门教程(任务状态)
    计蒜客 T1895切蛋糕(单调队列)
    白平衡简介
    【路径插值与抽稀篇】(3)路径插值与抽稀篇
    基于AI算法的5G多接入协同方案及关键技术
    Open3D RANSAC拟合球
    Java8 list.stream()操作使用心得
    Azure微软云
    uboot启动学习笔记六-uboot启动参数传递及第二阶段函数board_init_f
  • 原文地址:https://blog.csdn.net/Qy_T543/article/details/127838292