Is it possible to make a template for a function that has a template class in its argument list?
I would like to make one template for statSelection() and statInsertion() that would allow me to test different sorting algorithms without having to create a separate stat function for each type of sorting algorithm I am testing. (My sorting algorithms are template classes)
#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();
}
I would rather use polymorphism. (The solution without polymorphism can be found after a horizontal rule)
I would inherit both
Insertion<_Tp>andSelection<_Tp>from an abstract class (interface) calledISortable<_Tp>, and name.InsertionSortand.SelectionSortmember functions simply as.Sort(which would be a virtual member function of Sortable<_Tp>).So your function can be written like this:
Solution without polymorphism:
It is possible to do, just name BOTH your sort and performance functions with the same name.
Then
The examples: (Im not sure if you actually need the
<Selection<int> >part after the function, but I’d call it with it.)