P170 模板-函数模板案例-数组排序
#include <iostream>
using namespace std;
#include <string>//(1)只要涉及string打印输出要加此文件!!!
//定义函数模板
template<class T>//(2)注意这后面不加分号,它和下边函数实一个整体!!!
void mysort(T arr[], int len) //(5)数组做参数且值引用传递形式
{
//(3)选择排序法
for (int i = 0; i < len; i++)
{
int max = i;
for (int j = i + 1; j < len; j++) //选择法排序:每一次内循环都找到前j个数最大值下标
{
if (arr[max] < arr[j])
{
max = j;}
}
//交换最大值使其往前排
if(max!=i)
{
T temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
}
}
}
template<typename T>
void myprint(T arr[],int len)//(5)数组做参数且值引用传递形式
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl;
}
}
int main()
{
int arr[3] = {
1,9,6 };
char arr1[] =