인수 목록에 템플릿 클래스가있는 함수의 템플릿을 만들 수 있습니까?템플릿 함수 : 템플릿 클래스를 인자로 템플릿 함수를 만드는 방법은 무엇입니까?
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
나는 정말로 모든 사람들의 반응에 감사드립니다.내가 묻는 바를 명확히하려고 노력하겠습니다.이 드라이버 파일은 스왑 및 비교의 각 정렬 알고리즘 수를 정확하게 출력하기 위해 올바르게 작동합니다. 정렬 클래스는 클래스 템플릿이며 모든 것이 올바르게 작동합니다. 내 질문은 템플릿 클래스를 인수로 사용할 때 내 통계 함수에 대한 템플릿을 어떻게 만듭니 까? Insert 클래스 나 Selection 클래스 (또는 다른 정렬 클래스)를 전달하는 stat을 호출 할 수 있기를 원하지만 이러한 클래스도 모두 템플릿입니다. 다형성없이 이것을 할 수 있습니까? – Zzz
함수의 이름을 동일하게 지정하면됩니다. –