#include<iostream>
using namespace std;
//函数模板基本语法
//声明一个模板 T是一个通用数据类型
template<typename T>
void myswap(T& a, T& b) {
T temp;
temp = a;
a = b;
b = temp;
}
void test() {
int c = 10;
int d = 20;
//两种方式使用函数模板
//1自动类型推导
myswap(c, d);
cout << "c=" << c << endl;
cout << "d=" << d << endl;
//2显示指定类型
myswap<int>(c, d);
cout << "c=" << c << endl;
cout << "d=" << d << endl;
}
int main() {
test();
system("pause");
return 0;
}
//模板的目的是提高复用性 将类型参数化
05-22
3891
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)