模板函数sort

来源:互联网 发布:网络情歌对唱110首 编辑:程序博客网 时间:2024/06/11 04:40

 //第一周上机实验4.模板函数

#include <iostream>
#include <string>
using namespace std;

 
template<typename T>  //模版声明   
void sort( T &one,T &two, T &three)
{
  T temp;
  if(one<two)//使one 比two大
  {temp=two;
  two=one;
  one=temp;}
  if(one<three)//使one 比two大
  {temp=three;
  three=one;
  one=temp;}
     if(two<three)//使one 比two大
  {temp=three;
  three=two;
  two=temp;}
   //cout<<one<<two<<three<<endl; //证明参数没有传出去,改成引用

 
  
int main() { 
  int i1,i2,i3;
  cin>>i1>>i2>>i3; 
  sort(i1,i2,i3); 
  cout<<i1<<i2<<i3<<endl; 
  double d1,d2,d3; 
  cin>>d1>>d2>>d3; 
  sort(d1,d2,d3); 
  cout<<d1<<d2<<d3<<endl; 
  string s1,s2,s3; 
  cin>>s1>>s2>>s3; 
  sort(s1,s2,s3); 
  cout<<s1<<s2<<s3<<endl; 
  return 0;