1

是否可以为在其参数列表中具有模板类的函数制作模板?

我想为 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();
}
4

4 回答 4

2

我宁愿使用多态性。 (没有多态性的解可以在横尺后找到)

我将从一个名为 的抽象类(接口)继承Insertion<_Tp>和继承,并将名称和成员函数简单地称为(这将是 Sortable<_Tp> 的虚拟成员函数)。Selection<_Tp>ISortable<_Tp>.InsertionSort.SelectionSort.Sort

template<typename _Tp>
class ISortable<_Tp>{
public:
    virtual void Sort(_Tp *, int)=0; // btw functions are usually lowercase
    virtual void Performance()=0; 
};

template<typename _Tp>
class InsertionSort<_Tp> : public Sortable<_Tp>{
//...
    virtual void Sort(_Tp *, int); 
    virtual void Performance(); 
};
//...

所以你的函数可以这样写:

void statSelection(int[], int[], Sortable<int>&, Sortable<int>&);

void statSelection(int[], int[], Sortable<int>&sHundred, Sortable<int>&)
{
//...
  sHundred.Sort(vHundred, 100);
  sHundred.Performance();
//...
}

没有多态性的解决方案:

可以这样做,只需将您的排序和性能函数命名为相同的名称

然后

template<typename _Tp_sortable>
void statGeneral(int[], int[], _Tp_sortable sHundred, _Tp_sortable)
{
//...
  sHundred.Sort(vHundred, 100);
  sHundred.Performance();
//...
}

例子:(我不确定你是否真的需要<Selection<int> >函数后面的部分,但我会用它来调用它。)

statGeneral<Selection<int> >(valuesHundred, valuesThousand, selectHundred, selectThousand);
statGeneral<Insertion<int> >(valuesHundred, valuesThousand, insertionHundred, insertionThousand);
于 2012-12-03T19:58:05.490 回答
1

目前尚不清楚您在追求什么,但这是一个具有类模板参数的函数模板。

// class template
template <typename T> class Foo {};

// function template
template <typename T>
T doSomething(const Foo<T>& f) { .... }

如果您希望能够将类模板指定为模板参数,那么您需要一个“模板模板参数”:

// class templates
template <typename T> class Foo {};
template <typename T> class Bar {};

template <template<class> class T1, class T2>
T2 doSomething(const T1<T2>& f);

Foo<int> f;
Bar<double> b;
int n = doSomething(f);
double x = doSomething(b);
于 2012-12-03T19:56:49.763 回答
1

大概是这样的?

template <typename T>
void statSelection(T vHundred[], T vThousand[], Selection<T> sHundred, Selection<T> sThousand);

template <typename T>
void statInsertion(T vHundred[], T vThousand[], Insertion<T> iHundred, Insertion<T> iThousand);
于 2012-12-03T19:57:39.963 回答
0

你可以使用这样的类:

template <class T>
class Sortclass
{
public:
    virtual void sort(T array[] , int size) = 0;
    virtual void preformance() = 0;
};

template <class T>
class AsortClass : public Sortclass<T> 
{
public:
    virtual sort(T array[] , int size)
    {
        //do stuff
    }

    virtual void preformance()
    {
        //do stuff
    }
};

template <class T>
void stat(T vHundred[], T vThousand[], Sortclass<T>& iHundred, Sortclass<T>& iThousand)
{
    cout << "One Hundred Items" << endl;
    iHundred.sort(vHundred, 100);
    iHundred.preformance();
    cout << "One Thousand Items" << endl;
    iThousand.sort(vThousand, 1000);
    iThousand.preformance();
}    

然后你可以从这个类继承并实现排序功能。有了这个,你可以很容易地改变排序算法,而不用改变 stat 函数。

其称为策略模式请参阅: http ://en.wikipedia.org/wiki/Strategy_pattern

于 2012-12-03T20:05:57.930 回答