-
利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
-
排序规则从大到小
-
分别利用char数组和int数组进行测试
#include
#include
#include
using namespace std;
template
void printMsg(vector &nums,int len)
{
for(int i = 0;i
void mySwap(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
template
void mySort(vector &nums,int len)
{
for(int i = 0;inums[j+1]){
mySwap(nums[j+1],nums[j]);
}
}
}
}
void test01()
{
vector nums{1,4,2,1,5,3};
int len = nums.size();
mySort(nums,len);
printMsg(nums,len);
}
void test02()
{
vector nums{1.2,3.6,5,8.9,5.22};
int len = nums.size();
mySort(nums,len);
printMsg(nums,len);
}
void test03()
{
vector nums{"efgh","ijkl","abcd"};
int len = nums.size();
mySort(nums,len);
printMsg(nums,len);
}
void test04()
{
vector nums{'e','d','c','f','a'};
int len = nums.size();
mySort(nums,len);
printMsg(nums,len);
}
int main()
{
test01();
test02();
test03();
test04();
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)