模板函数:如何使用模板类作为参数创建模板函数?

时间:2012-12-03 19:48:36

标签: c++ templates

是否可以为其参数列表中具有模板类的函数创建模板?

我想为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 个答案:

答案 0 :(得分:2)

我宁愿使用多态。(在水平规则之后可以找到没有多态的解决方案)

我将从名为Insertion<_Tp>的抽象类(接口)继承Selection<_Tp>ISortable<_Tp>,并将.InsertionSort.SelectionSort成员函数简称为{ {1}}(这将是Sortable&lt; _Tp&gt;的虚拟成员函数)。

.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> >

答案 1 :(得分: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);

答案 2 :(得分: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);

答案 3 :(得分: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();
}    

然后你可以从这个类继承并实现sort funktion。 有了这个你可以很容易地改变排序algorythm而不改变stat函数。

其所谓的战略模式见: http://en.wikipedia.org/wiki/Strategy_pattern

相关问题