模板

  1. 函数模板

//#include "stdio.h"

#include "iostream"

#include "string"

using namespace std;

 

template <class T> void swap1(T &a,T &b)
{
     T c;
     c = a;
     a = b;
     b = c;
     cout<<a<<" "<<b<<endl;
}

int main()
{
int a = 2;
int b = 4;
string c = "a";
string d = "b";
double e = 1.1;
double f = 2.2;

swap1(a,b);
//cout<<a<<" "<<b<<endl;

swap1(c,d);
//cout<<c<<" "<<d<<endl;

swap1(e,f);
//cout<<e<<" "<<f<<endl;

//swap1(3,5);//c的类型没有初始化,'c' : const object must be initialized if not extern
//cout<<a<<" "<<d<<endl;

return 0;
}

posted @ 2016-10-31 09:25  朝_风  阅读(118)  评论(0编辑  收藏  举报