函数模板

来源:互联网 发布:mysql 数据库撞库工具 编辑:程序博客网 时间:2024/06/06 02:27

函数重载:具有不同的函数特性。

函数特性: 形参列表。
函数头部的返回类型,或void修饰符并不是这个函数的特性的一个部分。具有相同参数类型但不同返回类型的函数式不是函数重载。

使用函数模板替换函数重载,减少程序员工作量。

交换值函数模板:

template <typename  ElementType>void swap1(ElementType &a, ElementType &b){    ElementType temp = a;    a = b;    b = temp;}


显示数组函数模板:

template<typename ElementsType>void display(ElementsType array[], int numElements){    for(int i=0; i< numElements; ++i){        cout<< array[i]<<"\t";    }    cout<<endl;}


将模板的声明和实现放在同一个文件中。不同的函数模板需要使用不同的typename。 如交换值使用的是ElementType,显示数组使用的是ElementsType。

完整示意代码如下:

#include <iostream>using namespace std;template<typename ElementsType>void display(ElementsType array[], int numElements){    for(int i=0; i< numElements; ++i){        cout<< array[i]<<"\t";    }    cout<<endl;}template <typename  ElementType>void swap1(ElementType &a, ElementType &b){    ElementType temp = a;    a = b;    b = temp;}int main(){//    cout << "Hello world!" << endl;    double x[] = {1.1,2.2,3.3};    display(x,sizeof(x)/sizeof(double));    int y[] = {1,2,3,4,5};    display(y,sizeof(y)/sizeof(int));    int i_a = 1, i_b = 2;    swap1(i_a, i_b);    cout << i_a << "\t" << i_b<<endl;    double d_a = 1.2, d_b = 2.3;    swap1(d_a, d_b);    cout << d_a << "\t" << d_b<<endl;    return 0;}

程序输出:

1.1     2.2     3.31       2       3       4       52       12.3     1.2Process returned 0 (0x0)   execution time : 0.078 sPress any key to continue.

在交换值函数模板中,我们需要注意的是,他们只能用来交换两个相同类型的值,如同int,或者同double,但是如果传入的两个类型不相同,比如:一个是int类型另一个是double类型,则需要使用下面的方法:

#include <iostream>using namespace std;template <typename T, typename U>double GetMin(T value1 , U value2){        return (value1>value2? value1:value2);}int main(){    cout << GetMin(1,2)<<endl;    cout << GetMin(1,2.2)<<endl;    cout << GetMin(1.1,2)<<endl;    cout << GetMin(1.1,2.2)<<endl;    return 0;}

程序输出:

22.222.2

或者使用强制类型转换:

#include <iostream>using namespace std;template <typename T>T GetMin(T value1 , T value2){        return (value1>value2? value1:value2);}int main(){    cout << GetMin(1,2)<<endl;    // 使用强制类型转换,将int类型转换为double类型    cout << GetMin(static_cast<double>(1),2.2)<<endl;    // 显示指定(或限定)T的类型    cout << GetMin<double>(1,2.2)<<endl;    return 0;}

程序输出:

22.22.2


或者使用更加复杂的方法:auto。

在前面的double GetMin(T value1 , U value2)中不难发现,这里的返回值是用户自定义的double类型。那么如何根据不同的数值类型,来自动的设置为精度最高的那个T或者U呢?C++11中提供了这样的用法:

int x;decltype(x) y;
可以让y是x的类型。

decltype(x+y) x+y
让x+y的值是(x+y)的类型

double ft(T x, U y);auto ft(T x , U y) -> double
上面这两句是等价的,那么就有:

auto ft(T x, U y) -> decltype(x+y)
auto提供了一个占位符,其类型为(x+y)的类型

完整代码如下:

#include <iostream>using namespace std;template <class T, class U>auto sum(T x , U y) -> decltype(x+y)    // 根据(x+y)的类型来设置返回值类型{        return (x + y);}int main(){   int x = 5;   double y = 0.6;   cout << sum(x, y)<<endl;    return 0;}
如果您的编译器出现下面的错误:

||=== Build: Debug in test001 (compiler: GNU GCC Compiler) ===|D:\workspace\test\test001\main.cpp|5|warning: identifier 'decltype' is a keyword in C++11 [-Wc++0x-compat]|D:\workspace\test\test001\main.cpp|5|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|D:\workspace\test\test001\main.cpp|5|error: expected type-specifier before 'decltype'|D:\workspace\test\test001\main.cpp|5|error: expected initializer before 'decltype'|D:\workspace\test\test001\main.cpp||In function 'int main()':|D:\workspace\test\test001\main.cpp|15|error: 'sum' was not declared in this scope|||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
请尝试在编译器中勾选编译器对C++11的支持。比如:我的是codeblocks:

依次点击: settings ==> compiler ==> Global complier settings ==> 勾选 Have g++ follow the c++11 ISO C++ language standard [-std=c++11] ==> OK

程序输出:

5.6Process returned 0 (0x0)   execution time : 0.185 sPress any key to continue.

有关函数模板的字符串操作请参考 C++函数模板 http://www.cnblogs.com/archimedes/p/cpp-template.html

0 0
原创粉丝点击