C++之函数模板与模版函数

  直接上代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 
/*
    名称:C++函数模板
    作者:Michael Joessy
    日期:2017-06-07   高考第一天 莘莘学子加油!
    知识:类型作为参数
    关键字:template typename (class)
*/

#include <iostream>
#include <string>
using namespace std;

// 运算逻辑完全相同
int max(int a, int b)
{
    return (a > b) ? a : b;
}
float max(float a, float b)
{
    return (a > b) ? a : b;
}
double max(double a, double b)
{
    return (a > b) ? a : b;
}
char max(char a, char b)
{
    return (a > b) ? a : b;
}

// 将类型作为参数
template <class T>
T Max(T a, T b)                         // 函数模板
{
    return (a > b) ? a : b;
}

template<typename T>
void Swap(T &a, T &b)
{
    T tmp = a;
    a = b;
    b = tmp;
}

// 将变量作为参数
template<int size>
void Display()
{
    cout << size << endl;
}

// 多参数
template<typename T, typename C>
void Show(T a, C b)
{
    cout << a << "," << b << endl;
}

// typename和class可以混用
template<typename T, class U>
T Minus(T *a, U b);

template<typename T, int size>
void GoWarrior(T a)
{
    ; i < size; i++)
    {
        cout << a << endl;
    }
}

// 函数模板与重载,丰富了重载
template<typename T>
void Bloom(T a);

template<typename T>
void Bloom(T a, T b);

template<typename T, int size>
void Bloom(T a);

int main(void)
{
    );         // 模板函数,下同
    cout << nMax << endl;

;
    ;
    Swap<int>(a, b);
    cout << a << "," << b << endl;

Display<>();

Show<);

string str("Hey! Warrior~");
    GoWarrior<string, >(str);

cin.get();
    ;
}

C++之函数模板的更多相关文章

  1. c++函数模板作为类的成员函数,编译报错LNK2019的解决方法

    为了使某个类的成员函数能对不同的参数进行相同的处理,需要用到函数模板,即template<typename T> void Function(). 编译时报错LNK2019 解决方法: 1 ...

  2. C++STL - 函数模板

    模板主要是为了泛型编程,做到与类型无关 模板有函数模板和类模板,本文主要整理的是函数模板 1.函数模板定义 template<typename 类型形参1,typename 类型形参2,...& ...

  3. 使用getopt_long来解析参数的小函数模板

    getopt_long原型 #define no_argument 0 #define required_argument 1 #define optional_argument 2 struct o ...

  4. C++函数重载和函数模板

    1.函数重载 这是小菜鸟写的一个例子. 函数重载应该注意以下几点: 1.1重载函数有类似的功能: 1.2只能以参数的类型(形参个数和类型)来重载函数, int max(int a,int b);flo ...

  5. 零值初始化&字符串常数作为函数模板参数

    1.在定义一个局部变量时,并希望该局部变量的初始化一个值,可以显示调用其默认构造函数,使其值为0(bool类型默认值为false). template <typename T> void ...

  6. 让gcc支持成员函数模板的trick

    让gcc支持成员函数模板的trick 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循“署名-非商业用途-保持一致”创作公用协议   gcc 4.7.3 不支持成员 ...

  7. 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板

    [源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...

  8. Effective C++ -----条款45:运用成员函数模板接受所有兼容类型

    请使用member function templates(成员函数模板)生成”可接受所有兼容类型“的函数. 如果你声明member templates 用于“泛化copy构造”或“泛化assignme ...

  9. c++ 左值右值 函数模板

    1.先看一段代码,这就是一种函数模板的用法,但是红色的部分如果把a写成a++或者写成一个常量比如1,都是编译不过的,因为如果是a++的话,实际上首先是取得a的 值0,而0作为一个常量没有地址.写成1也 ...

  10. 读书笔记_Effective_C++_条款四十五:运用成员函数模板接受所有兼容类型

    比如有一个Base类和一个Derived类,像下面这样: class BaseClass {…}; class DerivedClass : public BaseClass {…}; 因为是父类与子 ...

随机推荐

  1. HTML 事件属性(如:onsubmit)

    学习啥都需要一个基础 基础很重要: HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 在现代浏览器中都内置有 ...

  2. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  3. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  4. opensips编译安装时可能遇到的问题

    错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...

  5. SQL Server2005 两台服务器上的数据库同步(转载)

    1.1测试环境 Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server SQL SQL Server 2005 企业版 SQL Serv ...

  6. spring中ApplicationContext

    1 spring 容器应用上下文:ApplicationContext 主要的实现类是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicatio ...

  7. C# 获得文件名

    string strFilePaht="文件路径"; Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的 还有的就是用 ...

  8. UIView总结

    转自:http://langhua9527.iteye.com/blog/1377741 如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类 performSelector: pe ...

  9. samba 文件和目录权限控制

    [laps_test]         comment = laps_test         path = /home/laps         browseable = yes         w ...

  10. Spring学习12-Spring利用mock进行单元测试

    一.概述    对于Java组件开发者来说,他们都盼望拥有一组能够对组件开发提供全面测试功能的好用的单元测试.一直以来,与测试独立的Java对象相比,测试传统型J2EE Web组件是一项更为困难的任务 ...