【问题标题】:Are there any tree container that can store node which has multiple values in it?是否有任何树容器可以存储其中具有多个值的节点?
【发布时间】:2012-11-28 02:01:30
【问题描述】:

我正在编写一个 c++ 代码,它应该能够用我编写的节点构造一个多树。但是我尝试使用在以下地址下载的树容器来存储我的节点,但它们似乎都无法存储其中包含多个值的节点。

http://www.datasoftsolutions.net/tree_container_library/overview.php

http://archive.gamedev.net/archive/reference/programming/features/coretree2/default.html

struct node{    //construct the node
    char *dirname;
    char date[12];
    int loc;
    bool prot;
}

那么有没有树容器可以存储我写的节点? 我需要将节点存储为多树。

【问题讨论】:

  • std:map 的底层实现是一棵树;你只需要提供一个比较函数来比较你的两个“节点”结构。

标签: c++ tree containers nodes treenode


【解决方案1】:

我怀疑它与结构节点中的多个字段有什么关系。

存储在树中的任何数据类型都需要一些比较运算符。您需要弄清楚您尝试使用的库需要哪些库,然后为您的节点类型实现它们。我看了看文档。该包是用 C++ 编写的,使用模板,并且与 STL 兼容。所以它可能还需要某些类型定义,如 value_type 等......

向我们显示您收到的错误消息,我们可以解决。至少,您可能需要实现 operator

更新:为了搜索树(或将节点放入其中),您至少需要定义 operator

#include <cstring>

bool operator< (const node& left, const node&right) {
    return 0 > strcmp(left.dirname, right.dirname);
}

bool operator== (const node& left, const node&right) {
    return 0 == strcmp(left.dirname, right.dirname);
}

【讨论】:

  • 程序又跑错了。 “错误 C2676:二进制 '
  • 非常感谢您的帮助。我尝试在我的结构节点中添加运算符,它似乎工作。但是我只是没有弄清楚,运算符是如何工作的,实际上左右比较是什么?是否将现有节点与我插入的新节点进行比较?那我需要给左右吗?这让我很困惑,因为它看起来像二叉树。
  • 不要把那些放在节点结构定义中。把它们放在后面。树库代码使用这些函数将您指定的节点与树中已有的节点进行比较。
  • 这会在节点结构之外吗?那我该怎么用呢?它给出了这样的信息:“iterator find(const T &inT, bool (*obj)(const T&, const T&)) const”,那么第二个参数应该怎么写呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
相关资源
最近更新 更多