如何编写模板? [英] How to write a template?

查看:103
本文介绍了如何编写模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个包含节点的模板,该节点包含具有2个数据结构的数据:一个映射和一个最小堆,它们都具有相同的节点,并且每2个相同的节点都连接在一起. 问题是我需要堆知道例如heapify的节点字段,而我不知道这样做的正确方法是什么,朋友? Node中的公共字段?在堆里面写节点?使用吸气剂和二传手? 谢谢大家的帮助.

i need to write a template with Nodes containing data with 2 data structures : a map and a minimum heap, both got the same nodes in it and every 2 same nodes are connected. the problem is that i need the heap to know the node fields for the heapify for example, and i don't know what's the right way to do so, friends? public fields in Node? writing the Node inside the heap? using getters and setters? thanks all for your help.

推荐答案

好吧,链接列表的布局可能是这样的:

Well, a linked list might be laid out like this:

namespace my_namespace
{
namespace detail 
{
template <class T>
struct Node
{
    T value;
    Node* previous;
    Node* next;
    //constructors and other things that might help
};
}

template <class T>
class LinkedList
{
private:
    detail::Node<T>* head;
public:
    //all it does    
};
}

没有特别的原因要向用户或LinkedList类隐藏Node结构(将其放入详细的名称空间应该绰绰有余):LinkedList需要它,并且Node本身对用户几乎没有用.所有的封装都取决于LinkedList的实现:它只是不应该露头(或任何其他Node*).

There is no particular reason to hide the Node struct from the user or the LinkedList class (putting it into a detail namespace should be more than enough): LinkedList needs it and the Node itself is pretty much useless to the user. All encapsulation is up to LinkedList to achieve: it just shouldn't give out it's head (or any other Node*).

这篇关于如何编写模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆