函数模板
声明与定义函数模板
cpp
#include <iostream>
using namespace std;
template <class T> void swap_new(T& a, T& b);
int main() {
int a = 1, b = 2;
float c = 1.5, d = 3.6;
swap_new(a, b);
swap_new(c, d);
cout << a << " " << b << endl;
cout << c << " " << d << endl;
return 0;
}
template <class T> void swap_new(T& a, T& b) {
T c = a;
a = b;
b = c;
}
运行结果: