c++上机考试题

来源:百度知道 编辑:UC知道 时间:2024/06/26 06:03:38
用模板函数实现三个数值中按最小值到最大值的排序程序

#include "stdafx.h"
#include <iostream>
using namespace std;

template<class T>
void Asort( T &a, T &b, T &c)
{
//
T temp;
if(a>b)temp=a,a=b,b=temp;
if(b>c)temp=b,b=c,c=temp;
if(a>b)temp=a,a=b,b=temp;
}

//测试程序 vs
int _tmain(int argc, _TCHAR* argv[])
{
int a,b,c;
cin >> a >> b >> c;
Asort(a,b,c); //排序
cout << a <<","<< b <<","<< c<<endl;

return 0;
}