现在的c++编译器实现了c++新增的特性之一函数模板。
函数模板允许任意类型的方式来定义函数。
template <typename AnyType>//指出要建立一个模板,并将类型名命名为AnyType。
void Swap(AnyType& a, AnyType& b)//关键字template和typename必不可少,除非class代替typename。
{
AnyType temp;
temp = a;
a = b;
b = temp;
}
定义一个特定形式的交换函数
#include<iostream>
using namespace std;
template<typename T>//模板声明typename可以被class替换。
void Swap(T& a, T& b);
int main()
{
int i = 10;
int j = 20;
cout << "i=" << i << "j=" << j << endl;
Swap(i, j);//接受int参数将所有的T换成int.
cout << "i=" << i << "j=" << j << endl;
double x = 10.3;
double y = 12.5;
cout << "x=" << x << "y=" << y << endl;
Swap(x, y);//接受double参数将所有的T换成double.
cout << "x=" << x << "y=" << y << endl;
return 0;
}
template<typename T>
void Swap(T& a, T& b)//函数定义。
{
T temp;
temp = a;
a = b;
b = temp;
}
/*
i=10j=20
i=20j=10
x=10.3y=12.5
x=12.5y=10.3
F:\新建文件夹\object\Debug\object.exe (进程 21952)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
重载的模板
重载的函数模板的特征标必须不同。
#include<iostream>
using namespace std;
template<typename T>//模板一
void Swap(T& a, T& b);
template<typename T>//模板二
void Swap(T* a, T* b, int n);
void show(int a[]);
const int t = 3;
int main()
{
int a = 10;
int b = 20;
cout << "a=" << a << "b=" << b << endl;
Swap(a, b);
cout << "a=" << a << "b=" << b << endl;//交换数字
int d1[t] = { 1,2,3 };
int d2[t] = { 4,5,6 };
show(d1);
show(d2);
Swap(d1, d2,t);
show(d1);
show(d2);//交换数组元素
return 0;
}
template<typename T>//模板一的定义
void Swap(T& a, T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
template<typename T>//模板二的定义
void Swap(T* a, T* b, int n)
{
T temp;
for (int i = 0; i < n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void show(int a[])
{
for (int i = 0; i < t; i++)
{
cout << a[i];
}
cout << endl;
}
/*
a=10b=20
a=20b=10
123
456
456
123
F:\新建文件夹\object\Debug\object.exe (进程 19844)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
模板的显示具体化
#include<iostream>
using namespace std;
#include<string>
template<typename T>
void Swap(T& a, T& b);
struct job//创建一个job结构体
{
string name;
double salary;
int floor;
};
template <> void Swap<job>(job& j1, job& j2);//只交换job里的salary和floor。
void show(job& j);
int main()
{
int a = 111;
int b = 999;
cout << "a=" << a << "b=" << b << endl;
Swap(a, b);
cout << "a=" << a << "b=" << b << endl;
job one = { "小明",23000,3 };
job two = { "小红",349900,5 };
show(one);
show(two);
Swap(one, two);
show(one);
show(two);
system("pause");
return 0;
}
template<typename T>
void Swap(T& a, T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
template <> void Swap<job>(job& j1, job& j2)
{
double t1;
int t2;
t1 = j1.salary;
j1.salary = j2.salary;
j2.salary = t1;
t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}
void show(job& j)
{
cout << j.name << " " << j.floor << " " << j.salary << endl;
}
/*
a=111b=999
a=999b=111
小明 3 23000
小红 5 349900
小明 5 349900
小红 3 23000
请按任意键继续. . .
F:\新建文件夹\object\Debug\object.exe (进程 6260)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
decltype C++11新增的关键字用于难以明确的模板。类似于auto关键字
int x;
decltype(x)y;//让y和x具有相同的类型。
double x = 12.3;
double y = 7.3;
double& z = x;
const double* p;
decltype (x)w;//w和于x是同类型double.
decltype(z)u = y;//u的类型是double&。
decltype(p)v;//v的类型是double*。