【原创】模板函数的应用
//模板函数的应用//阿杰编写于:2009年9月6日13:41:10
#include <stdio.h>
template <class t>//这里的t可以是任意名子,如abc,aa等。
//通用模板函数
swap(t &a,t &b)
{
t c;
c=a;
a=b;
b=c;
}
void main()
{
int a=3,b=4;
char *s1="阿杰",*s2="马大哈";
//交换整数变量
printf("%d==%d\n",a,b);
swap(a,b);
printf("%d==%d\n",a,b);
//交换字符串变量
printf("%s==%s\n",s1,s2);
swap(s1,s2);
printf("%s==%s\n",s1,s2);
} 运行结果:
3==4
4==3
阿杰==马大哈
马大哈==阿杰 :funk: LZ
=啊杰?! /*
说明:交换两个数的值
作者:阿杰
时间:2019年10月15日21:11:55
*/
#include <stdio.h>
#include<stdlib.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
//printf("swap:%d==%d\n",*a,*b);
}
void main()
{
int a =55,b=66;
printf("交换前: a=%d,b=%d\n",a,b);
swap(&a,&b);
printf("交换后: a=%d,b=%d\n",a,b);
system("pause");
}
页:
[1]