C++学习笔记7——模板
函数模板:
#include <iostream>
using namespace std; template <typename T>
T max(const T &lhs, const T &rhs)
{
return lhs > rhs ? lhs : rhs;
} template <typename T,class U>//在模板参数列表中,typename和class没有区别
T min(const T &lhs, const U &rhs)
{
return lhs > rhs ? rhs : lhs;
} //非类型函数模板
template<unsigned N, unsigned M>
int compare(const char(&p1)[N], const char(&p2)[M])
{
return strcmp(p1, p2);
} //可变参函数模板 函数模板重载
template <typename T>
void print(const T &t)
{
cout << t;
} template <typename T, typename... Args>
void print(const T &t, const Args&... rest)
{
cout << t << ",";
print(rest...);
} int main()
{
cout << max<int>(, ) << endl;
cout << max<double>(3.1, 4.2) << endl; cout << min<int, char>(, 'a') << endl; cout << compare("ab", "a"); print("a", , 1.23); system("pause");
return ;
}
类模板:
#pragma once
#ifndef _COMPLEXNUMBER_
#define _COMPLEXNUMBER_ #include <iostream>
using namespace std; template <typename T> class complexNum; //前置声明
template <typename T> void printCom(complexNum<T> &obj); template <typename T>
class complexNum
{
friend ostream& operator<< <T>(ostream &out, complexNum<T> &rhs);
friend istream& operator>><T>(istream &in, complexNum<T> &rhs);
friend void printCom<T>(complexNum<T> &obj); public:
complexNum(int real = , int image = );
complexNum(const complexNum<T> &obj); public:
complexNum<T>& operator=(const complexNum<T> &rhs); complexNum<T> operator+(const complexNum<T> &rhs); complexNum<T>& operator++(void); //前置++
complexNum<T> operator++(int); //后置++
complexNum<T>& operator+=(const complexNum &rhs);
bool operator>(const complexNum<T> &rhs); private:
T real;
T image;
}; #endif
#include "complexNumber.h" template <typename T>
complexNum<T>::complexNum(int real, int image) :real(real), image(image){} template <typename T>
complexNum<T>::complexNum(const complexNum &obj) : real(obj.real), image(obj.image){} template <typename T>
std::ostream& operator<<(std::ostream &out, complexNum<T> &rhs)
{
out << rhs.real; if (rhs.image >= )
out << "+"; out << rhs.image << "i" << std::endl; return out;
} template <typename T>
std::istream& operator>>(std::istream &in, complexNum<T> &rhs)
{
return in >> rhs.real >> rhs.image;
} template <typename T>
void printCom(complexNum<T> &obj)
{
operator<<(cout, obj);
} template <typename T>
complexNum<T>& complexNum<T>::operator=(const complexNum<T> &rhs)
{
this->real = rhs.real;
this->image = rhs.image; return *this;
} template <typename T>
complexNum<T> complexNum<T>::operator+(const complexNum<T> &rhs)
{
complexNum tmp; tmp.real = this->real + rhs.real;
tmp.image = this->image + rhs.image; return tmp;
} template <typename T>
complexNum<T>& complexNum<T>::operator++(void)
{
this->real++;
this->image++; return *this;
} template <typename T>
complexNum<T> complexNum<T>::operator++(int)
{
complexNum tmp = *this; this->real++;
this->image++; return tmp;
} template <typename T>
complexNum<T>& complexNum<T>::operator+=(const complexNum &rhs)
{
this->operator+(rhs); return *this;
} template <typename T>
bool complexNum<T>::operator>(const complexNum<T> &rhs)
{
if (this->real > rhs.real)
return true;
else if (this->real < rhs.real)
return false;
else
{
if (this->image > rhs.image)
return true;
else
return false;
}
}
#include <iostream>
#include "complexNumber.hpp" //需包含.hpp文件而不是.h文件 int main()
{
complexNum<int> c1(, );
cout << c1;
printCom(c1); system("pause");
return ;
}
C++学习笔记7——模板的更多相关文章
- OpenCV 学习笔记(模板匹配)
OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...
- Python Flask学习笔记之模板
Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...
- Angular 5.x 学习笔记(1) - 模板语法
Angular 5.x Template Syntax Learn Note Angular 5.x 模板语法学习笔记 标签(空格分隔): Angular Note on github.com 上手 ...
- tornado 学习笔记8 模板以及UI
Tornado 包含一个简单.快速而且灵活的模板语言. Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...
- C++学习笔记30:模板与型式参数化
转型操作 接受目标型式作为模板参数 Programmer *p = dynamic_cast<Programmer*>(e) 模板工作原理 使用template<typename T ...
- play framework学习笔记之 模板引擎
模板语法 ${client.name} ${client?.name} 不能确定client是否存在的时候? #{extends /} #{doLayout /}#{get} #{set} 比如 #{ ...
- C++学习笔记之模板(1)——从函数重载到函数模板
一.函数重载 因为函数重载比较容易理解,并且非常有助于我们理解函数模板的意义,所以这里我们先来用一个经典的例子展示为什么要使用函数重载,这比读文字定义有效的多. 现在我们编写一个交换两个int变量值得 ...
- C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]
模板与泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- 高放的c++学习笔记之模板与泛型编程
函数模板 作用 有很多时候参数的类型以及返回值的类型是可变的,我们通过定义模板来让函数能更灵活的运用. 我们设计一个比较函数,如果能比较的两个参数是int型的,两个参数也可能都是string型的,单独 ...
随机推荐
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- poj 1149 最大流
题目链接:http://poj.org/problem?id=1149 #include <cstdio> #include <cmath> #include <algo ...
- poj 4618 暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 #include <cstdio> #include <cmath> # ...
- 用Myeclipse 编写struts.xml时,自动提示
之所以不自动提示,是因为这个xml它不知道自己的xml格式是什么有哪些标签,所以不知道该怎么提示 所以就要给它引入格式,所以要引入XSD或者DTD文件 1.首先打开MyEclipse的窗口,选择“Wi ...
- python环境配置selenium与IE、Chrome、Firefox、PhantomJS
安装.升级selenium pip install -U selenium 下载对应平台最新版的browser driver chrome: http://chromedriver.storage.g ...
- [置顶] cocos2d-x 植物大战僵尸(4) 帽子僵尸的产生
大家早上好,趁着阳光美好的时候,我打算写下博客:今天要说的是僵尸的产生了,这块和太阳因子的产生比较相似,大体上的区别在于僵尸的基类这块:我在考虑是详细的写还是大体的写,本着对自己作业的态度和 ...
- 使用Listener准备application作用域数据
在程序中.有些数据我们希望在程序启动的时候就准备好,而且仅仅准备一次,放在application作用域中,这时候.我们一般会用Listener来准备这些数据. 可是,用Listener准备applic ...
- Qt 学习之路:视图选择 (QItemSelectionModel)
选择是视图中常用的一个操作.在列表.树或者表格中,通过鼠标点击可以选中某一项,被选中项会变成高亮或者反色.在 Qt 中,选择也是使用了一种模型.在 model/view 架构中,这种选择模型提供了一种 ...
- <<java 并发编程>>第七章:取消和关闭
Java没有提供任何机制来安全地终止线程,虽然Thread.stop和suspend等方法提供了这样的机制,但是存在严重的缺陷,应该避免使用这些方法.但是Java提供了中断Interruption机制 ...
- SDK目录结构和adb工具及命令介绍
1.SDK目录介绍: ******************************** add-ons:Android开发需要的第三方文件,附加的库,如Google APIs.GoogleMaps. ...