首页 > 学院 > 开发设计 > 正文

模板函数的基础应用

2019-11-14 10:52:56
字体:
来源:转载
供稿:网友

#include <iostream>

using namespace std;//template告诉C++编辑器要进行泛型编程 T在这里是一种类型template<typename T>//数据类型参数化void myswap(T &a,T &b){	T t = a;	a = b;	b = t;}void main(){	int a1 = 10;	int b1 = 20;	myswap(a1,b1);//这是最容易董的函数调用方式	cout<<"a1="<<a1<<endl;	cout<<"b1="<<b1<<endl;		myswap<int>(a1,b1);	cout<<"a1="<<a1<<endl;	cout<<"b1="<<b1<<endl;	system("pause");}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表