• 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” 实例。

  • 相关阅读:
    【lesson13】进程地址空间收尾
    【web-攻击会话管理】(4.2.2)会话令牌生成过程中的薄弱:令牌可预测
    ASP.NET Core如何知道一个请求执行了哪些中间件?
    JetBrains Annotations:将NPE杀死在编译期
    Springboot毕设项目车辆管理系统1200l(java+VUE+Mybatis+Maven+Mysql)
    多图详解Windows恶意软件删除工具的常用操作
    适配与视口、分辨率、媒体查询、缩放的学习、消化
    50道基础数据结构面试题
    基础 | NIO - [Files & Path & Charset]
    【图像检测】基于FT算法实现图像显著性检测附matlab代码
  • 原文地址:https://blog.csdn.net/Qy_T543/article/details/127838292