#include<iostream>
#include<string>
using namespace std;
//函数模板所生成的并不是重载函数,因为根据函数模板所生成的模板函数名并不相同
//如函数模板Swap生成的模板函数是Swap<int>,Swap<double>
template <class T>
void Swap(T&x, T&y)
{
T temp = x;
x = y;
y = temp;
}
int main()
{
int i1(1), i2(2);
Swap(i1, i2);
cout << i1 << ' ' << i2 << endl;
double d1(3.14), d2(2.56);
Swap(d1, d2);
cout << d1 << ' ' << d2 << endl;
string s1("dasd"), s2("qdqwd");
Swap(s1, s2);
cout << s1 << ' ' << s2 << endl;
char c1('a'), c2('b');
Swap(c1, c2);
cout << c1 << ' ' << c2 << endl;
system("pause");
}
C++函数模板
最新推荐文章于 2024-10-19 15:29:34 发布