美文网首页
c++函数模板

c++函数模板

作者: 三角绿毛怪 | 来源:发表于2019-07-16 08:13 被阅读0次

    函数模板,通过几个例子对照更好的理解什么是模板

    #include <iostream>
    using namespace std;
    
    void swapint(int& a,int& b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    
    void swapdouble(double& a, double& b)
    {
        double temp = a;
        a = b;
        b = temp;
    }
    
    template<typename T>
    void my_swap(T& a, T& b)
    {
        T temp = a;
        a = b;
        b = temp;
    }
    
    
    void test01()
    {
        int a = 10, b = 20;
        swapint(a, b);
        cout << "a = " << a << " " << "b = " << b << endl;
    }
    void test02()
    {
        double a = 1.1, b = 2.2;
        swapdouble(a, b);
        cout << "a = " << a << " " << "b = " << b << endl;
    }
    void test03()
    {
        int a = 10, b = 20;
        //1.自动推导类型来实现
        //my_swap(a, b);
        //2.显示指定类型
        my_swap<int>(a, b);
        cout << "a = " << a << " " << "b = " << b << endl;
    }
    int main()
    {
        //test01();
        //test02();
        test03();
    
        return 0;
    }
    

    输出结果么不用想了啊

    函数模板的注意事项(会出错的地方)

    #include<iostream>
    using namespace std;
    
    
    //-自动类型推导,必须推导出一致的数据类型T, 才可以使用
    //-模板必须要确定出T的数据类型,才可以使用
    template<class T>
    void my_swap(T& a, T& b)
    {
        T temp = a;
        a = b;
        b = temp;
    }
    
    void test01()
    {
        int a = 10,b = 20;
        char c = 'c';
        //my_swap(a, b);//正确
        //my_swap(a, c);//必须要能推出一致的数据类型才能用自动类型推导
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
    }
    template<class T>
    void func()
    {
        cout << "调用func函数" << endl;
    }
    void test02()
    {
        //func();//不知道T是什么数据类型,因此不能调用
        func<int>();//随便给他一个类型->确定了T的类型可以使用
    }
    int main()
    {
        //test01();
        test02();
        return 0;
    }
    

    函数模板的案例-同一个模板下不同数据类型的选择排序

    #include<iostream>
    using namespace std;
    
    //目标-完成两个不同类型数组在同一个模板下的选择排序
    
    //定义一个交换的函数名模板类
    template<class T>
    void my_swap(T& a, T& b)
    {
        T temp = a;
        a = b;
        b = temp;
    }
    
    //选择排序算法的模板
    template<class T>
    void my_sort(T arr, int n)
    {
        for (int i = 0; i < n; i++)
        {
            int max = i;
            for (int j = i + 1; j < n; j++)
            {
                if (arr[max] < arr[j]) max = j;
            }
            if (max != i)
            {
                my_swap(arr[max], arr[i]);
            }
        }
    }
    
    //索性再来一个输出的模板吧
    template<class T>
    void print(T arr,int n)
    {
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
    
    void test01()//测试字符型
    {
        char char_arr[] = "cbadef";
        int n = sizeof(char_arr) / sizeof(char);
        my_sort(char_arr,n);
        print(char_arr, n);
    }
    
    void test02()//测试整型
    {
        int int_arr[] = { 2,4,5,3,1,6 };
        int n = sizeof(int_arr) / sizeof(int);
        my_sort(int_arr, n);
        print(int_arr, n);
    }
    int main()
    {
        //test01();
        test02();
        return 0;
    }
    

    普通函数与模板函数的区别

    #include <iostream>
    using namespace std;
    
    //-普通函数调用时可以发生自动类型转换(隐式类型转换)
    //-函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
    //-如果利用显示指定类型的方式,可以发生隐式类型转换
    
    //普通函数
    int my_add01(int a,int b)
    {
        return a + b;
    }
    template<class T>
    T my_add02(T a, T b)
    {
        return a + b;
    }
    
    void test01()
    {
        int a = 10, b = 20;
        char c = 'c';
        //正确,将char类型转化为整形
        cout << my_add01(a, c) << endl;
        //报错,使用自动推导,不会发生隐式转换
        //cout << my_add02(a, c) << endl;
        cout<< my_add02<int>(a, c) << endl;
    }
    int main()
    {
        test01();
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:c++函数模板

          本文链接:https://www.haomeiwen.com/subject/fwgakctx.html