📄 405a.cpp
字号:
/*
405a.cpp
DEMO implement Swap() using function template
*/
#include <iostream.h>
#include <string.h>
template <class T> void Swap(T& a,T& b);
void Swap(char * a, char * b);
void main(void)
{
long n1 = 1, n2 = 2;
Swap(n1,n2);
cout << n1 << " " << n2 << endl;
float f1 = 1.5, f2 = 2.5;
Swap(f1,f2);
cout << f1 << " " << f2 << endl;
char c1 = 'A', c2 = 'b';
Swap(c1,c2);
cout << c1 << " " << c2 << endl;
char name1[20] = "Wang ", name2[20] = "Lee";
Swap(name1,name2);
cout << name1 << " " << name2 << endl;
}
void Swap(char * a, char * b)
{
char temp[20] ;
strcpy (temp,a);
strcpy (a,b);
strcpy (b,temp);
}
template <class T> void Swap(T& a,T& b)
{ T temp=a; a = b; b = temp;}
/*
2 1
2.5 1.5
b A
Lee Wang
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -