20200118-函数模板

#include "stdafx.h"

#include "iostream"

#include "cstring"

#include "string"

using namespace std;

template <class T> //或者template <typename T>注意没有分号

void swap1(T &a,T &b);//泛型引用变量,泛型就是泛指一切类型

int main()

{

int i=10;int j=20;

swap1(i,j);

cout<<"i="<<i<<"\n";

cout<<"j="<<j<<"\n";

    double a=10.8;double b=2;

swap1(a,b);//注意a,b的数据类型必须一致

cout<<"a="<<a<<"\n";

cout<<"b="<<b<<"\n";

 return 0;

}

template <class T> //或者template <typename T>注意没有分号

void swap1(T &a,T &b)

{

  T temp;

  temp=a;

  a=b;

  b=temp;

}

 
运行结果:
posted @ 2020-05-21 08:16  财盛  阅读(98)  评论(0编辑  收藏  举报