简体   繁体   English

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

[英]Template Function: How to make a template function with a template class as an argument?

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. 我想为statSelection()和statInsertion()创建一个模板,它允许我测试不同的排序算法,而不必为我正在测试的每种类型的排序算法创建单独的stat函数。 (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> and Selection<_Tp> from an abstract class (interface) called ISortable<_Tp> , and name .InsertionSort and .SelectionSort member functions simply as .Sort (which would be a virtual member function of Sortable<_Tp>). 我将继承两个Insertion<_Tp>Selection<_Tp>从抽象类(接口)称为ISortable<_Tp>和名称.InsertionSort.SelectionSort成员函数简单地.Sort (这将是可排序的虚拟成员函数< _TP>)。

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(); 
};
//...

So your function can be written like this: 所以你的函数可以像这样写:

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

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

Solution without polymorphism: 没有多态性的解决方案:

It is possible to do, just name BOTH your sort and performance functions with the same name . 可以这样做,只需使用相同的名称命名您的排序和性能函数。

Then 然后

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

The examples: (Im not sure if you actually need the <Selection<int> > part after the function, but I'd call it with it.) 示例:(我不确定您是否确实需要在函数之后使用<Selection<int> >部分,但我会用它来调用它。)

statGeneral<Selection<int> >(valuesHundred, valuesThousand, selectHundred, selectThousand);
statGeneral<Insertion<int> >(valuesHundred, valuesThousand, insertionHundred, insertionThousand);

It isn't clear what you are after, but this is a function template that has a class template parameter. 目前尚不清楚您的目标是什么,但这是一个具有类模板参数的函数模板。

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

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

If you want to be able to specify the class template as a template parameter, then you need a "template template parameter": 如果您希望能够将类模板指定为模板参数,则需要“模板模板参数”:

// 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);

Something like this perhaps? 也许这样的事情?

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);

you can use a class like this: 你可以使用这样的类:

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();
}    

then you can inherit from this class and implement the sort funktion. 然后你可以从这个类继承并实现排序funktion。 with this you can change the sort algorythm realy easy without changing the stat function. 有了这个你可以很容易地改变排序algorythm而不改变stat函数。

its called strategy pattern see: http://en.wikipedia.org/wiki/Strategy_pattern 它的战略模式见: http//en.wikipedia.org/wiki/Strategy_pattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM