编写一个程序,用于在不使用std :: sort的情况下对任何数据类型的变量进行排序。
例子:
Input : 2000, 456, -10, 0
Output : -10 0 456 2000
Input : "We do nothing"
"Hi I have something"
"Hello Join something!"
"(Why to do work)"
Output :(Why to do work)
Hello Join something!
Hi I have something
We do nothing
上面的示例显示,我们可以将任何数据类型元素作为输入存在,并且输出将以输入数据的排序形式出现。
解决此问题的想法是制作一个模板。
方法1(编写我们自己的排序)在下面的代码中,我们实现了Bubble Sort对数组进行排序。
// CPP program to sort array of any data types.
#include
using namespace std;
// Template formed so that sorting of any
// type variable is possible
template
void sortArray(T a[], int n)
{
// boolean variable to check that
// whether it is sorted or not
bool b = true;
while (b) {
b = false;
for (size_t i=0; i a[i + 1]) {
T temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
b = true;
}
}
}
}
// Template formed so that sorting of any
// type variable is possible
template
void printArray(T a[], int n)
{
for (size_t i = 0; i < n; ++i)
cout << a[i] << " ";
cout << endl;
}
// Driver code
int main()
{
int n = 4;
int intArr[n] = { 2000, 456, -10, 0 };
sortArray(intArr, n);
printArray(intArr, n);
string strArr[n] = { "We do nothing",
"Hi I have something",
"Hello Join something!",
"(Why to do work)" };
sortArray(strArr, n);
printArray(strArr, n);
float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
sortArray(floatArr, n);
printArray(floatArr, n);
return 0;
}
输出:
-10 0 456 2000
(Why to do work) Hello Join something! Hi I have something We do nothing
-9.7 11.17 11.4 23.4
方法2(使用库函数)
我们可以在C++中使用std :: sort对任何数据类型的数组进行排序。
// CPP program to sort array of any data types.
#include
using namespace std;
// Template formed so that sorting of any
// type variable is possible
template
void printArray(T a[], int n)
{
for (size_t i = 0; i < n; ++i)
cout << a[i] << " ";
cout << endl;
}
// Driver code
int main()
{
int n = 4;
int intArr[n] = { 2000, 456, -10, 0 };
sort(intArr, intArr + n);
printArray(intArr, n);
string strArr[n] = { "We do nothing",
"Hi I have something",
"Hello Join something!",
"(Why to do work)" };
sort(strArr, strArr + n);
printArray(strArr, n);
float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
sort(floatArr, floatArr+n);
printArray(floatArr, n);
return 0;
}
输出:
-10 0 456 2000
(Why to do work) Hello Join something! Hi I have something We do nothing
-9.7 11.17 11.4 23.4