C++ 模板学习 函数模板、类模板、迭代器模板
使用模板能够极大到使得代码可重用。
记录一下,方便后续使用。
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++ 模板学习 函数模板、类模板、迭代器模板的更多相关文章
- C++模板类内友元(友元函数,友元类)声明的三种情况
根据<C++ Primer>第三版16.4节的叙述,C++类模板友元分为以下几种情况 1.非模板友元类或友元函数. 书上给了一个例子: class Foo{ void bar(); ...
- 转:C++模板学习
C++ 模板 转:http://www.runoob.com/cplusplus/cpp-templates.html 2018-01-05 模板是泛型编程的基础,泛型编程即以一种独立于任何特定类型的 ...
- tornado-模板继承extend,函数和类的导入
大 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # ...
- C++模板学习:函数模板、结构体模板、类模板
C++模板:函数.结构体.类 模板实现 1.前言:(知道有模板这回事的童鞋请忽视) 普通函数.函数重载.模板函数 认识. //学过c的童鞋们一定都写过函数sum吧,当时是这样写的: int sum(i ...
- 《C++ Primer Plus》第16章 string类和标准模板库 学习笔记
C++提供了一组功能强大的库,这些库提供了很多常见编程问题的解决方案以及简化其他问题的工具string类为将字符串作为对象来处理提供了一种方便的方法.string类提供了自动内存管理动能以及众多处理字 ...
- 3.2 STL中的函数对象类模板
*: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...
- C++学习笔记36:类模板
类模板的目的 设计通用的类型式,以适应广泛的成员数据型式 类模板的定义格式 template<模板形式参数列表>class 类名称{...}; 原型:template<typenam ...
- C++中模板类使用友元模板函数
在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...
- 初步C++类模板学习笔记
类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型. -->抽象的类. 在调用类模板时, 指定參数, 由编 ...
随机推荐
- rabbitmq学习(六) —— 主题
主题交换(Topic exchange) 使用 topic 类型的交换器,不能有任意的绑定键,它必须是由点隔开的一系列的标识符组成.标识符可以是任何东西,但通常它们指定与消息相关联的一些功能.其中,有 ...
- spring 状态机
前言:“状态机”见名知意,用状态去管理业务操作,打个比方:0~1岁(出生状态),1~3岁(认知状态),3~6岁(启蒙状态),6~22岁(学习状态),22~60(工作状态),60以后(退休状态),那么人 ...
- POJ3070 Fibonacci[矩阵乘法]【学习笔记】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- Mysql update case
UPDATE table SET total = CASE WHEN total = '1' THEN total- 1 ELSE total = '2' END WHERE id = 17
- 使用 IntraWeb (14) - 基本控件之 TIWHRule、TIWRectangle
TIWHRule //一条横线, 对应 Html 中的 <hr/> TIWRectangle //矩形; 中间可以有行文本, 文本可任意对齐 TIWHRule 所在单元及继承链: IWHT ...
- Linux下的sqlserver简单试用
微软自2017年就推出了可以在linux上使用的sql-server,最近接触到了一个用sqlserver的项目,便尝试使用了一下. 下载 为了简化安装,我还是使用的docker的方式,镜像可以直接从 ...
- Maven具体解释之------maven版本号管理
本文同意转载,但请标明出处:http://blog.csdn.net/wanghantong/article/38424065, 版权全部 如今所说的maven版本号不同于SVN的版本号控制哦!!! ...
- lua中的pairs和ipairs差别
pairs Returns three values: the next function, the table t, and nil, so that the construction for k, ...
- MFC之菜单
1菜单与菜单项的操作 //获取菜单指针----CWnd::GetMenu() //GetSubMenu()获取子菜单 /CheckMenuItem()加入/取消标记 GetMenu()->Get ...
- Delphi 类的类 class of 用法
http://blog.csdn.net/blue_morning/article/details/8815609 Delphi 类的类 class of 用法 这个概念本来在一个关于Delphi ...