A lot of this question is probably impractical to use in the real world but I'm doing this as a learning process.
很多这个问题在现实世界中使用可能是不切实际的,但我这样做是为了学习过程。
I have started a project documenting each type of sorting algorithm and it's efficiency. I have been writing each sorting algorithm as a template function in c++ like as follows:
我已经开始了一个项目,记录每种类型的排序算法,并且它的效率。我一直在编写每个排序算法作为c ++中的模板函数,如下所示:
template <class T>
void bubble_sort(T arr[], int numItems) {
for (int i = 0; i < numItems; i++) {
for (int j = 0; j < numItems - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
I want my driver program to test each algorithms efficiency over a few sets of data and want to generalize my process a bit more by creating a template function that tests each sorting algorithm, but I have no idea how to do that. Here's what I'm thinking but it's not working:
我希望我的驱动程序能够在几组数据上测试每个算法的效率,并希望通过创建测试每个排序算法的模板函数来进一步推广我的过程,但我不知道如何做到这一点。这是我在想的但它不起作用:
template<typename F, typename T>
double test(F arr[], int numItems, T func) {
clock_t start, finish;
start = clock();
T(arr, numItems);
finish = clock();
return (double)(finish - start) / CLOCKS_PER_SEC;
}
The contents of the test don't matter, but I want to be able to pass in the sorting function like this:
测试的内容无关紧要,但我希望能够传递这样的排序函数:
double duration = test<int>(arr, numItems, bubble_sort<int>);
Any help in building this function would be better;
建立这个功能的任何帮助都会更好;
1 个解决方案
#1
0
I suppose you have to call func()
, inside test()
, as
我想你必须在test()里面调用func(),as
func(arr, numItems);
instead of
T(arr, numItems);
#1
0
I suppose you have to call func()
, inside test()
, as
我想你必须在test()里面调用func(),as
func(arr, numItems);
instead of
T(arr, numItems);