是否有可能爲在其參數列表中具有模板類的函數創建模板?模板函數:如何使用模板類作爲參數來創建模板函數?
我想爲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
這是如果你的功能相同的名稱。 –