使用模板能够极大到使得代码可重用。

记录一下,方便后续使用。

1. 函数模板,支持多种类型参数

 #include <stdio.h>
#include <math.h> //函数模板
template <class T>
T add(T a, T b){
return a + b;
} //函数模板特殊化
template <>
double add<double>(double a, double b){
return floor(a + b);
} class Vector{
public:
Vector(int a = , int b = ):_a(a), _b(b) { }
Vector operator +(Vector &v1){ //重载+
Vector res;
res._a = this->_a + v1._a;
res._b = this->_b + v1._b;
return res;
} int _a;
int _b;
}; int main (){
printf("3 + 4 = %d\n", add(, )); //支持int
printf("5.6 + 3.7 = %.0lf\n", add(5.6, 3.7)); //支持double Vector v1(,);
Vector v2(,);
Vector v3 = add(v1, v2); //支持类
printf("v3(%d, %d)\n", v3._a, v3._b);
return ;
}

2. 迭代器模板,支持多种容器

 #include <iostream>
#include <vector>
#include <list> //该模板函数, 支持各种容器到数据打印
template <class iterator>
void print(iterator begin, iterator end){
for(iterator it = begin; it != end; ++it){
std::cout << *it << std::endl;
}
} //使用迭代器需要添加关键字typename
//该模板函数, 支持各种容器到数据打印
template <class container>
void print(container con){
for(typename container::iterator it = con.begin(); it != con.end(); ++it){
std::cout << *it << std::endl;
}
} int main (){
std::vector<int> my_ver;
my_ver.push_back();
my_ver.push_back();
my_ver.push_back();
print(my_ver.begin(), my_ver.end()); std::list<std::string> my_list;
my_list.push_back("No.1");
my_list.push_back("No.2");
my_list.push_back("No.3");
print(my_list);
return ;
}

3.类模板

test_temple.h

 #ifndef MATH_CLASS
#define MATH_CLASS template <class T>
class Math{
public:
static T add(T v1, T v2){ //方法在类内声明+实现
return v1 + v2;
} static T sub(T v1, T v2); //方法在类内声明
}; #endif

test_temple.cpp

 #include "test_temple.h"

 #ifndef MATH_CPP
#define MATH_CPP template <class T>
T Math<T>::sub(T v1, T v2){
return v1 - v2;
} #endif

test.h

 //模板类到声明、实现都必须被编译时包含
//1.可以将实现都写到头文件
//2.或者同时包含.h , .cpp文件 //这里把他们了封装, 用户只需要包含test.h即可
#include "test_temple.h"
#include "test_temple.cpp"

main.cpp

 #include <stdio.h>
#include "test.h" int main(){
printf("3 + 4 = %d\n", Math<int>::add(, ));
printf("20.8 - 5.1 = %.2lf\n", Math<double>::sub(20.8, 5.1));
return ;
}

C++ 模板学习 函数模板、类模板、迭代器模板的更多相关文章

  1. C++模板类内友元(友元函数,友元类)声明的三种情况

    根据<C++ Primer>第三版16.4节的叙述,C++类模板友元分为以下几种情况 1.非模板友元类或友元函数. 书上给了一个例子: class Foo{     void bar(); ...

  2. 转:C++模板学习

    C++ 模板 转:http://www.runoob.com/cplusplus/cpp-templates.html 2018-01-05 模板是泛型编程的基础,泛型编程即以一种独立于任何特定类型的 ...

  3. tornado-模板继承extend,函数和类的导入

    大 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # ...

  4. C++模板学习:函数模板、结构体模板、类模板

    C++模板:函数.结构体.类 模板实现 1.前言:(知道有模板这回事的童鞋请忽视) 普通函数.函数重载.模板函数 认识. //学过c的童鞋们一定都写过函数sum吧,当时是这样写的: int sum(i ...

  5. 《C++ Primer Plus》第16章 string类和标准模板库 学习笔记

    C++提供了一组功能强大的库,这些库提供了很多常见编程问题的解决方案以及简化其他问题的工具string类为将字符串作为对象来处理提供了一种方便的方法.string类提供了自动内存管理动能以及众多处理字 ...

  6. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  7. C++学习笔记36:类模板

    类模板的目的 设计通用的类型式,以适应广泛的成员数据型式 类模板的定义格式 template<模板形式参数列表>class 类名称{...}; 原型:template<typenam ...

  8. C++中模板类使用友元模板函数

    在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...

  9. 初步C++类模板学习笔记

    类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编 ...

随机推荐

  1. 初识thinkphp(2)

    thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...

  2. linux Shell 脚本编写

    1. http://www.jb51.net/article/28514.htm 2. http://www.runoob.com/linux/linux-shell.html

  3. luoguP3768 简单的数学题

    题目链接 luoguP3768 简单的数学题 题解 上面那个式子的最后一步,需要定理 用数学归纳法证明 \(S1=1^3=1^2\) \(S2=1^3+2^3=9=3^2=(1+2)^2\) \(S3 ...

  4. 1722 最优乘车 1997年NOI全国竞赛

    题目描述 Description H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士 ...

  5. bsgs算法详解

    例题  poj 2417bsgs  http://poj.org/problem?id=2417 这是一道bsgs题目,用bsgs算法,又称大小步(baby step giant step)算法,或者 ...

  6. MAC下安装Homebrew 和 @权限的问题

    MAC下安装Homebrew和 @权限的问题 1.Homebrew简介: Homebrew是一个包管理器,用于安装Apple没有预装但你需要的UNIX工具.(比如著名的wget). Homebrew会 ...

  7. consul vs etcd3

    https://sysadmin.libhunt.com/project/etcd/vs/consul

  8. 使用CefSharp在.Net程序中嵌入Chrome浏览器(五)——Javascript交互

    要在CEF中和网页的JS进行交互,首先我们要通过设置启用Javascrit集成功能. CefSharpSettings.LegacyJavascriptBindingEnabled = true; 调 ...

  9. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. How to add Leading Zeroes to a Number (Delphi Format)

    How to add Leading Zeroes to a Number (Delphi Format) Here's how convert (an integer) number to a st ...