是否有可能为在其参数列表中具有模板类的函数创建模板?模板函数:如何使用模板类作为参数来创建模板函数?
我想为statSelection()和statInsertion()创建一个模板,它允许我测试不同的排序算法,而不必为我测试的每种排序算法类型创建单独的stat函数。 (我的排序算法是模板类)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "FileGen.h"
#include "FileRead.h"
#include "SelectionSort.h"
#include "SelectionSort.cpp"
#include "InsertionSort.h"
#include "InsertionSort.cpp"
using namespace std;
void statSelection(int[], int[], Selection<int>, Selection<int>);
void statInsertion(int[], int[], Insertion<int>, Insertion<int>);
int main()
{
FileGen fileGen;
FileRead fileRead;
Selection<int> selectHundred;
Selection<int> selectThousand;
Insertion<int> insertionHundred;
Insertion<int> insertionThousand;
int valuesHundred[100];
int valuesThousand[1000];
fileGen.generateFiles();
fileRead.readFiles(valuesHundred, valuesThousand);
statSelection(valuesHundred, valuesThousand, selectHundred, selectThousand);
fileGen.generateFiles();
fileRead.readFiles(valuesHundred, valuesThousand);
statInsertion(valuesHundred, valuesThousand, insertionHundred, insertionThousand);
system("pause");
return 0;
}
void statSelection(int vHundred[], int vThousand[], Selection<int> sHundred, Selection<int> sThousand)
{
cout << "One Hundred Items" << endl;
sHundred.SelectionSort(vHundred, 100);
sHundred.selectionSortPreformance();
cout << "One Thousand Items" << endl;
sThousand.SelectionSort(vThousand, 1000);
sThousand.selectionSortPreformance();
}
void statInsertion(int vHundred[], int vThousand[], Insertion<int> iHundred, Insertion<int> iThousand)
{
cout << "One Hundred Items" << endl;
iHundred.InsertionSort(vHundred, 100);
iHundred.insertionSortPreformance();
cout << "One Thousand Items" << endl;
iThousand.InsertionSort(vThousand, 1000);
iThousand.insertionSortPreformance();
}
你能提供一些简短的代码示例,说明你想达到什么样的? – juanchopanza
我真的很感谢大家的回应。让我试着澄清我所问的:这个驱动程序文件能够正确地打印出每个排序算法的交换次数和比较次数。排序类是类模板,一切工作正常。我的问题是如何为模板类创建一个模板作为参数?我希望能够调用传递插入类或选择类(或其他排序类)的统计信息,但这两个类都是模板。没有多态性可以做到这一点吗? – Zzz
这是如果你的功能相同的名称。 –