实验一:函数内传入相同类型但大小不同的参数
项目练习:万能排序工具
#include <iostream>
void sort(int* ary, unsigned count, bool ispseq = true)
{
int ls{};
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if ((ispseq)^(ary[i] < ary[j]))
{
ls = ary[i];
ary[i] = ary[j];
ary[j] = ls;
}
}
}
for (int i = 0; i < count; i++)
{
std::cout << ary[i] << " ";
}
std::cout << std::endl;
}
template <typename T>
void sort_temp(T* ary, unsigned count, bool IsPSeque)
{
T ls{};
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if ((IsPSeque) ^ (ary[i] < ary[j]))
{
ls = ary[i];
ary[i] = ary[j];
ary[j] = ls;
}
}
}
}
int main()
{
int ary1[5]{ 45,34,65,67,232 };
short ary2[5]{ 65,69,67,98,70 };
std::string ary3[6]{ "abc","bcd","cde","fgh","ijk" };
sort_temp(ary2, sizeof(ary2) / sizeof(short), true);
for (auto x : ary2)std::cout << x<<" ";
std::cout << std::endl;
sort_temp(ary2, sizeof(ary2) / sizeof(short), false);
for (auto x : ary2)std::cout << x<<" ";
std::cout << std::endl;
sort_temp(ary1, sizeof(ary1) / sizeof(int), true);
for (auto x : ary1)std::cout << x << " ";
std::cout << std::endl;
sort_temp(ary1, sizeof(ary1) / sizeof(int), false);
for (auto x : ary1)std::cout << x << " ";
std::cout << std::endl;
sort_temp(ary3, sizeof(ary3) / sizeof(std::string), true);
for (auto x : ary3)std::cout << x << " ";
std::cout << std::endl;
}