函数模板:
#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>(1, 2) << endl;
cout << max<double>(3.1, 4.2) << endl;
cout << min<int, char>(100, 'a') << endl;
cout << compare("ab", "a");
print("a", 12, 1.23);
system("pause");
return 0;
}
类模板:
#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 = 0, int image = 0);
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 >= 0)
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;
}
}
View Code
#include <iostream>
#include "complexNumber.hpp" //需包含.hpp文件而不是.h文件
int main()
{
complexNum<int> c1(1, 2);
cout << c1;
printCom(c1);
system("pause");
return 0;
}