函数模板:

#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——模板的更多相关文章

  1. OpenCV 学习笔记(模板匹配)

    OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...

  2. Python Flask学习笔记之模板

    Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...

  3. Angular 5.x 学习笔记(1) - 模板语法

    Angular 5.x Template Syntax Learn Note Angular 5.x 模板语法学习笔记 标签(空格分隔): Angular Note on github.com 上手 ...

  4. tornado 学习笔记8 模板以及UI

          Tornado 包含一个简单.快速而且灵活的模板语言.       Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...

  5. C++学习笔记30:模板与型式参数化

    转型操作 接受目标型式作为模板参数 Programmer *p = dynamic_cast<Programmer*>(e) 模板工作原理 使用template<typename T ...

  6. play framework学习笔记之 模板引擎

    模板语法 ${client.name} ${client?.name} 不能确定client是否存在的时候? #{extends /} #{doLayout /}#{get} #{set} 比如 #{ ...

  7. C++学习笔记之模板(1)——从函数重载到函数模板

    一.函数重载 因为函数重载比较容易理解,并且非常有助于我们理解函数模板的意义,所以这里我们先来用一个经典的例子展示为什么要使用函数重载,这比读文字定义有效的多. 现在我们编写一个交换两个int变量值得 ...

  8. C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]

    模板与泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...

  9. 高放的c++学习笔记之模板与泛型编程

    函数模板 作用 有很多时候参数的类型以及返回值的类型是可变的,我们通过定义模板来让函数能更灵活的运用. 我们设计一个比较函数,如果能比较的两个参数是int型的,两个参数也可能都是string型的,单独 ...

随机推荐

  1. MVC4 中Remote的使用

    相信当你看到这篇文章的时候对remote是有一些了解了, 起码知道这个东西是来干嘛用的. 这里也就不废话了 直接上代码  看看Remote的一些使用方式. 1.判断表单上输入的数据是否存在 [Syst ...

  2. Android Proguard

    Android Proguard 14 May 2015 语法 -include {filename} 从给定的文件中读取配置参数 -basedirectory {directoryname} 指定基 ...

  3. [Locked] Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  4. Event 元标签中的代码提示问题

    自定的事件可以利用Event元标签在支持该事件的类里面做绑定,绑定后FlashBuilder会有代码提示,以提示该类支持的事件类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  5. Servlet中获取JSP内置对象

    方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...

  6. 漫步支持向量机(svm)之一

    设输入为$x$,表示训练集的特征向量,输出为$y=\{1,-1\}$,这些向量都属于两类中的其中一类,假设这些向量是线性可分的,现在要找一个最优的平面(在二维的时候为一条直线),将这些特征向量正确分类 ...

  7. springMVC3学习(二)--ModelAndView对象

    当控制器处理完请求时,一般会将包括视图名称或视图对象以及一些模型属性的ModelAndView对象返回到DispatcherServlet. 因此,常常须要在控制器中构造ModelAndView对象. ...

  8. 实现自己的cp命令

    1 综述 在Unix和Linux系统里,cp是经常使用的一个命令,用于复制文件,用法如下: $cp src_file dest_file 以下就使用若干系统调用来实现自己的cp. 2 原理 open: ...

  9. ADO.NET基础

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. 【翻译】使用nginx作为反向代理服务器,uWSGI作为应用服务器来部署flask应用

    最近在看关于Docker和Nginx方面的内容,先于在Docker上开发以及部署python应用自然要先能够在本机上部署,其中找到一篇文章写的最为详细并且实验成功,所以在此翻译转载过来以备后需.[原文 ...