码迷,mamicode.com
首页 > 其他好文 > 详细

函数模板,函数模板重载,可变参数模板,函数模板覆盖,通过引用交换数据

时间:2014-08-17 01:07:21      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   数据   



1.函数模板初级,如果想使用模板,需要实例化,实例化的方式是加上<数据类型>

#include <iostream>

 

//函数模板可以对类型进行优化重载,根据类型会覆盖

//如果仍然要使用模板函数,需要实例化

 

template<class T>

T add(T a, T b)

{

    std::cout << "T add " << std::endl;

    return a + b;

}

 

int add(int a, int b)

{

    std::cout << "int add " << std::endl;

    return a + b;

}

 

void main()

{

    int a = 10, b = 20;

    double db1 = 10.9, db2 = 10.8;

    add(db1, db2);

    add(a, b);

    //这里加上了<int>相当于实例化,调用了模板

    add<int>(a, b);

 

    std::cin.get();

}

运行结果如下:

bubuko.com,布布扣

2.模板的重载,模板的重载会根据数据类型自动匹配

#include <iostream>

#include <array>

using std::array;

 

template<typename T>

void showarray(array<T, 10> myarray, int n)

{

    using namespace std;

    cout << "TTTTT" << endl;

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

    {

        cout << myarray[i] << " ";

    }

    cout << endl;

}

 

template<typename T>

void showarray(array<T*, 10> myarray, int n)

{

    using namespace std;

    cout << "T*T*T*T*T*" << endl;

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

    {

        cout << *myarray[i] << "  ";

    }

    cout << endl;

}

 

void main()

{

    array<int, 10> intarray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    array<int*, 10> pintarray;

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

    {

        pintarray[i] = &intarray[i];

    }

    array<int**, 10> ppintarray;

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

    {

        ppintarray[i] = &pintarray[i];

    }

    showarray(intarray, 10);

    showarray(pintarray, 10);

    showarray(ppintarray, 10);

 

    std::cin.get();

}

运行结果如下:

bubuko.com,布布扣

3.通用函数可变参数模板

#include <iostream>

 

//通用可变参数模板    处理不限定个数的参数,处理不同类型

 

//空函数,接口,用于最后结束递归 适用于新版本的编译器

void showall(){}

 

template<typename T, typename...Args>

void showall(const T &value, const Args &...args)

{

    std::cout << value << std::endl;

    showall(args...);   //继续传递

}

 

//设计可以修改原来的数据的T &value,  Args&...args

//设计可以修改副本   T value,  Args ...args

//设计不可以修改原来的数据,不可以修改副本const T value,  const Args...args

//设计引用原来的数据不可以修改 const  T &value,  const Args &...args

 

void main()

{

    int num1 = 10, num2 = 9, num3 = 11;

    double db1 = 10.8, db2 = 10.9;

    char str[40] = "yincheng";

    char ch = ‘A‘;

    showall(num1);

    std::cout << "\n";

    showall(num1, num2, num3);

    std::cout << "\n";

    showall(db1, db2, num1, ch);

    std::cout << "\n";

    showall(db1, db2, num1, ch, str);

 

    std::cin.get();

}

运行截图如下:

bubuko.com,布布扣

4.函数模板覆盖,并实现参数互换

#include <iostream>

 

//函数模板实现通用,可以根据自有数据类型,进行优化

 

//结构体和类没有私有变量时才可以直接赋值初始化

//所有成员都是公有的类型可以赋值初始化

 

struct info

{

    char name[40];

    double db;

    int data;

};

 

template<typename T>

void swap(T &a, T &b)

{

    std::cout << "通用函数模板" << std::endl;

    T temp = a;

    a = b;

    b = temp;//交换变量

}

 

//模板为空,明确参数类型,覆盖函数模板的类型

//template<>   //这里可有可无

void swap(info &info1, info &info2)

{

    std::cout << "特有函数模板" << std::endl;

    //通过模板可以实现通用,针对自己的数据类型做出优化

    info temp = info1;

    info1 = info2;

    info2 = temp;

}

 

void main()

{

    info info1 = {"tuzuoquan",20.9,10};

    info info2 = { "quanzuotu",9.2,1 };

    swap(info1, info2);

    std::cout << info1.name << info1.db << info2.data << std::endl;

    std::cout << info2.name << info2.db << info2.data << std::endl;

    std::cin.get();

}

运行结果:

bubuko.com,布布扣

5.通过引用的方式实现参数互换

#include <iostream>

 

//函数模板实现通用,可以根据自有数据类型,进行优化

 

//结构体和类没有私有变量时才可以直接赋值初始化

//所有成员都是公有的类型可以赋值初始化

template<typename T>

void swap(T &a, T &b)

{

    std::cout << "通用函数模板" << std::endl;

    T temp = a;

    a = b;

    b = temp;//交换变量

}

 

void main()

{

    int num1 = 100;

    int num2 = 10;

    swap(num1, num2);//实现交换

    std::cout << num1 << "  " << num2 << std::endl;

    char  ch1 = ‘Z‘;

    char  ch2 = ‘A‘;

    //注意,要指定使用模板,这里要使用<类型名称>

    swap<char>(ch1, ch2);

    std::cout << ch1 << "  " << ch2 << std::endl;

 

    std::cin.get();

}

bubuko.com,布布扣

6.可变参数模板

#include <iostream>

#include <cstdarg>

 

void showall(){}  //预留一个

 

template <typename T>

void show(T t, ...)

{

    std::cout << t << std::endl;

}

template <typename T, typename...Args>

void showall(T t, Args...args)

{

    std::cout << t << std::endl;

    showall(args...);

}

 

void main()

{

    int num1 = 10, num2 = 9, num3 = 11;

    double db1 = 10.8, db2 = 10.9;

    char str[40] = "yincheng";

    char ch = ‘A‘;

    show(num1);

    showall(num2, num3);

    showall(num2, num3, num1, str, ch);

 

    std::cin.get();

}

运行结果如下:

bubuko.com,布布扣

 

 

 

函数模板,函数模板重载,可变参数模板,函数模板覆盖,通过引用交换数据,布布扣,bubuko.com

函数模板,函数模板重载,可变参数模板,函数模板覆盖,通过引用交换数据

标签:style   blog   http   color   使用   os   io   数据   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/38626035

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!