简体   繁体   English

如何对多个函数或结构使用单个Template语句?

[英]How to use a single Template statement for more than one function or struct?

I've been trying to represent Stacks as a template, I used a struct and every thing is good, but every time I wanted to write a template function, I had to write the same template statement, which didn't seem correct -although working- 我一直试图将Stacks表示为模板,我使用了一个struct,并且一切都很好,但是每次我想编写一个模板函数时,我都不得不编写相同的template语句,这似乎并不正确-尽管工作-

So how can I write one template statement for all the functions?, here is my code : 那么如何为所有功能编写一个模板语句呢?这是我的代码:

template <typename T>
struct Stack
{
    T Value;
    Stack* next;
};
template <typename T>
void Push(T Value,Stack* &Top)
{
    Stack * Cell = new Stack();
    Cell->Value = Value;
    Cell->next = Top;
    Top = Cell;
};
template <typename T>
bool IsEmpty(Stack * Top)
{
    return (Top==0);
}
template <typename T>
void Pop(T &Value,Stack* &Top)
{
    if (IsEmpty(Top))
        cout  * Temp = Top;
        Value = Top->Value;
        Top = Top->next;
        delete Temp;
    }
}
template <typename T>
void GetTop(T &Value, Stack* &Top)
{
    if (IsEmpty(Top))
        cout Value;
}
template <typename T>
void EmptyStack(Stack * &Top)
{
    Stack * Temp;
    while (!(IsEmpty(Top)))
    {
        Temp = Top;
        Top = Top->next;
        delete Temp;
    }
}

Hope what I mean is clear now, sorry for the slight question :( 希望我的意思现在已经明白了,对这个小问题很抱歉:(

thanks in advance. 提前致谢。

If (as appears to be the case based on your comment) you want them as free functions, you can't. 如果您希望它们作为自由功能(根据您的评论似乎是这样),则不能。 You'll also have to change the Stack parameter, something like this: 您还必须更改Stack参数,如下所示:

template <typename T>
void Push(T Value, Stack<T>* &Top)
{
    Stack * Cell = new Stack();
    Cell->Value = Value;
    Cell->next = Top;
    Top = Cell;
};

As it stands, I'm not too excited about your design though. 就目前而言,我对您的设计并不感到兴奋。 You try to use the Stack type as both an actual stack, and as a single node (Cell) in the stack. 您尝试使用Stack式既是一个实际的堆栈, 堆的单个节点(小区)。 This is unnecessarily confusing at best. 这充其量是不必要的。

Edit: As far as stack vs. node goes, what I'm talking about is (as in the code immediately above): Stack *Cell = new Stack(); 编辑:就堆栈vs.节点而言,我正在谈论的是(如上面的代码中所示): Stack *Cell = new Stack(); -- you're allocating a single Cell that goes in the stack, but the type you're using for it is Stack . -你分配一个单细胞是云堆栈,但你使用它的类型 Stack

I'd do something like this instead: 我会做这样的事情:

template <class T>
struct Stack { 
    struct node { 
        T data;
        node *next;
    };

    node *head;
};

template <class T> 
void push(T item, Stack<T> *&s) { 
    Stack<T>::node *n = new Stack<T>:node();       
    n->data = item;
    n->next = s->head;
    s->head = n;
}

It doesn't make a lot of difference in what you're really doing, but when you're putting something onto a stack, allocating a Stack<T>::node seems (at least to me) to make a lot more sense than allocating a Stack<T> . 这与您实际执行的操作没有太大区别,但是当您将某些内容放入堆栈时,分配Stack<T>::node似乎(至少在我看来)更有意义而不是分配Stack<T> A stack containing multiple nodes makes sense -- a Stack containing multiple stacks really doesn't. 包含多个节点的堆栈有意义-包含多个堆栈的堆栈实际上没有意义。

You could simply write a template class instead, and write all those functions as methods of that class. 您可以直接编写一个模板类,然后将所有这些函数作为该类的方法来编写。 They will then share the same template parameters as the class. 然后,他们将共享与该类相同的模板参数。

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

相关问题 在单个auto语句中声明多个变量 - declaring more than one variable within a single auto statement 如何专门化具有多个参数的功能模板? - How to specialize a function template with more than 1 parameter? 多个函数模板实例与参数列表匹配 - more than one instance of function template matches the argument list 如何在模板 function 中使用不同的结构作为模板参数? - How can I use different struct as template argument in a template function? 具有超过1个typename的模板函数 - template function with more than 1 typename 如何在一个程序中运行多个 Windows 服务 - how to run more than one windows service in a single program 是否可以使用istringstream读入一个stream function的多个参数? - Is it possible to use istringstream to read in more than one parameter of a stream function? 模板:功能模板专业化:模板参数推导:-&gt;有人可以讲出更多有关此语句的示例吗? - Templates :Function template specializations:Template argument deduction: -->can any one tell some more examples for this statement? 如何为一个虚拟功能提供多个替代 - How to provide more than one overrides for one virtual function 如何在自己的结构中使用模板类的结构? - How to use struct of template class in own struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM