我正在尝试掌握在此处实现的树容器库:http://tree.phi-sci.com/index.html。我一直在寻找一个树形容器,这里的建议似乎是这个容器或图形库。

在此特定示例中,我尝试制作一棵N元树,并向其中添加一些节点。问题是我不想重复这些项目。因此,在添加某些东西之前,我先检查它是否存在。

预期的树应该是这样的:

A
---- A.1
B
---- B.1
---- B.2
C
---- C.1
D
---- D.1
---- D.2
E
---- E.1
---- E.2

数据通过字符串对以任意顺序到达。例如,如果我得到“D”,“D.1”,则需要创建节点“D”(如果不存在),如果不存在则将节点“D.1”添加到“D”中,我不在乎A,B或C之前是否存在。

到目前为止,这是我的代码
#include "tree.hh"

#include <iostream>
#include <string>
#include <array>

int main(int argc, char *argv[])
{


    //Init the database
    std::string zones[10] = {"A",
                             "A",
                             "B",
                             "C",
                             "B",
                             "D",
                             "D",
                             "E",
                             "E",
                             "E"};

    std::string subZones[10] = {"A.1",
                                "A.1",
                                "B.1",
                                "C.1",
                                "B.2",
                                "D.1",
                                "D.2",
                                "E.1",
                                "E.1",
                                "E.2"};

    //Prepare the strings for the categories
    std::string tempZone = "";
    std::string tempSubZone = "";

    //Prepare the tree
    tree<std::string> bodyTree;
    tree<std::string>::iterator zoneIt, subZoneIt, topIt;
    topIt = bodyTree.begin();

    //Loop the entire database
    for(int i=0; i<10; i++){

        //Grab the data
        tempZone = zones[i];
        tempSubZone = subZones[i];

        //Check if we have that zone already
        zoneIt=find(bodyTree.begin(), bodyTree.end(), tempZone);

        //If we don't have the zone, add it to the tree
        if(zoneIt==bodyTree.end()){

            bodyTree.insert(topIt, tempZone);

            std::cout << "Added new Zone: "<< tempZone << "\n";

        }

        //Now we have the zone for sure, we do the same with the subZone

        //Check if we have that subzone already
        subZoneIt=find(bodyTree.begin(zoneIt), bodyTree.end(zoneIt), tempSubZone);

        //If the subZone doesn't exist, add it to the zone
        if(subZoneIt==bodyTree.end(zoneIt)){

            bodyTree.insert(zoneIt, tempSubZone);

            std::cout << "Added new subZone "<< tempSubZone << " --> to --> " << tempZone << "\n";

        }

    }

    return 0;
}

这是输出:
Added new Zone: A
Added new subZone A.1 --> to --> A
Added new subZone A.1 --> to --> A
Added new Zone: B
Added new subZone B.1 --> to --> B
Added new Zone: C
Added new subZone C.1 --> to --> C
Added new subZone B.2 --> to --> B
Added new Zone: D
Added new subZone D.1 --> to --> D
Added new subZone D.2 --> to --> D
Added new Zone: E
Added new subZone E.1 --> to --> E
Added new subZone E.1 --> to --> E
Added new subZone E.2 --> to --> E

如您所见,第一级节点很好,只添加了一次。多次添加第二级节点,以试图找出它们先前是否已存在于该特定节点中。

我的猜测是对兄弟使用迭代器的代码是错误的,因此它应该是以下两行之一:
//Check if we have that subzone already
subZoneIt=find(bodyTree.begin(zoneIt), bodyTree.end(zoneIt), tempSubZone);

//If the subZone doesn't exist, add it to the zone
if(subZoneIt==bodyTree.end(zoneIt)){

我想知道是否有人可以告诉我我做错了什么。

最佳答案

有两个问题:

  • 第一次插入区域zoneIt将等于bodyTree.end()。因此,您需要插入另一个对find(bodyTree.begin(), bodyTree.end(), tempZone);的调用,以使其指向新插入的元素。
  • insert将新元素添加为当前区域的同级。但是您想使用append_child代替将其添加为当前区域的子级。

  • these更改添加到您的代码后,我得到以下输出:
    Added new Zone: A
    Added new subZone A.1 --> to --> A
    Added new Zone: B
    Added new subZone B.1 --> to --> B
    Added new Zone: C
    Added new subZone C.1 --> to --> C
    Added new subZone B.2 --> to --> B
    Added new Zone: D
    Added new subZone D.1 --> to --> D
    Added new subZone D.2 --> to --> D
    Added new Zone: E
    Added new subZone E.1 --> to --> E
    Added new subZone E.2 --> to --> E
    

    关于c++ - 避免使用Kasper Peeters的Tree.hh库在节点中添加重复的子级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36870100/

    10-17 01:41