#include <iostream>
using namespace std;
template <class T>
void my_swap (T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
template <class T>
void my_sort (T arr[], int len)
{
for (int i = 0; i < len; i ++)
{
int max = i;
for (int j = i + 1; j < len; j ++)
{
if (arr[max] < arr[j])
{
max = j;
}
}
if (max != i)
{
my_swap(arr[max], arr[i]);
}
}
}
template <class T>
void print_array (T arr[], int len)
{
cout << "heheda" << endl;
for (int i = 0; i < len; i ++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void test ()
{
char char_arr[] = "abcdefg";
int length = sizeof(char_arr);
my_sort(char_arr, length);
print_array(char_arr, length);
}
void test2 ()
{
int arr[] = {5, 3, 7, 8, 2, 1};
int len = sizeof(arr) / sizeof(arr[0]);
my_sort(arr, len);
print_array(arr, len);
}
int main ()
{
test();
test2();
return 0;
}
模板04、函数模板案例-数组排序
最新推荐文章于 2024-09-06 10:38:06 发布