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<<" ";

cout<<"j="<<j<<" ";

    double a=10.8;double b=2;

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

cout<<"a="<<a<<" ";

cout<<"b="<<b<<" ";

 return 0;

}

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

void swap1(T &a,T &b)

{

  T temp;

  temp=a;

  a=b;

  b=temp;

}

 
运行结果:
原文地址:https://www.cnblogs.com/whcsrj/p/12928170.html