模板函数的基础应用

来源:互联网 发布:程序员年薪百万 编辑:程序博客网 时间:2024/06/10 17:53

#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");}


0 0