<tfoot id='kOE9T'></tfoot>

    1. <i id='kOE9T'><tr id='kOE9T'><dt id='kOE9T'><q id='kOE9T'><span id='kOE9T'><b id='kOE9T'><form id='kOE9T'><ins id='kOE9T'></ins><ul id='kOE9T'></ul><sub id='kOE9T'></sub></form><legend id='kOE9T'></legend><bdo id='kOE9T'><pre id='kOE9T'><center id='kOE9T'></center></pre></bdo></b><th id='kOE9T'></th></span></q></dt></tr></i><div id='kOE9T'><tfoot id='kOE9T'></tfoot><dl id='kOE9T'><fieldset id='kOE9T'></fieldset></dl></div>
    2. <legend id='kOE9T'><style id='kOE9T'><dir id='kOE9T'><q id='kOE9T'></q></dir></style></legend>
      1. <small id='kOE9T'></small><noframes id='kOE9T'>

        • <bdo id='kOE9T'></bdo><ul id='kOE9T'></ul>

        我的 DFS 树 (C++) 的意外结果

        Unexpected result of my DFS tree (C++)(我的 DFS 树 (C++) 的意外结果)

        • <tfoot id='26hz9'></tfoot>
            <i id='26hz9'><tr id='26hz9'><dt id='26hz9'><q id='26hz9'><span id='26hz9'><b id='26hz9'><form id='26hz9'><ins id='26hz9'></ins><ul id='26hz9'></ul><sub id='26hz9'></sub></form><legend id='26hz9'></legend><bdo id='26hz9'><pre id='26hz9'><center id='26hz9'></center></pre></bdo></b><th id='26hz9'></th></span></q></dt></tr></i><div id='26hz9'><tfoot id='26hz9'></tfoot><dl id='26hz9'><fieldset id='26hz9'></fieldset></dl></div>

              <legend id='26hz9'><style id='26hz9'><dir id='26hz9'><q id='26hz9'></q></dir></style></legend>
                <bdo id='26hz9'></bdo><ul id='26hz9'></ul>
                  <tbody id='26hz9'></tbody>
              • <small id='26hz9'></small><noframes id='26hz9'>

                  本文介绍了我的 DFS 树 (C++) 的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经解决了这个问题!!!我发现如果我必须使用 vector儿童;.但我不是很确定原因,有人能告诉我为什么吗?谢谢:)

                  I have solved this problem!!! I found that if i have to use vector<Node*> children;. But I am not very sure the reason, can someone tell me why? Thanks:)

                  问题:

                  我使用 test.cpp 生成一个树结构,如:

                  I use test.cpp to generate a tree structure like:

                  (ROOT->children).size() 的结果是 2,因为 root 有两个孩子.

                  The result of (ROOT->children).size() is 2, since root has two children.

                  ((ROOT->children)[0].children).size() 的结果应该是 2,因为 的第一个孩子root 有两个孩子.但答案是0,为什么呢?这让我很困惑.

                  The result of ((ROOT->children)[0].children).size() should be 2, since the first child of root has two children. But the answer is 0, why? It really confuse for me.

                  test.cpp(此代码可在 Visual Studio 2010 中运行)

                  test.cpp (This code is runnable in visual studio 2010)

                  #include <iostream>
                  #include <vector>
                  using namespace std;
                  
                  struct Node {
                      int len;
                      vector<Node> children;
                      Node *prev;
                      Node(): len(0), children(0), prev(0) {};
                  };
                  
                  class gSpan {
                  public:
                      Node *ROOT;
                      Node *PREV;
                      void read();
                      void insert(int);
                  };
                  
                  int main() {
                      gSpan g;
                      g.read();
                      system("pause");
                  }
                  
                  void gSpan::read() {
                      int value[4] = {1, 2, 2, 1};
                      ROOT = new Node();
                      PREV = ROOT;
                      for(int i=0; i<4; i++) {
                          insert(value[i]);
                      }
                      cout << "size1: " << (ROOT->children).size() << endl; // it should output 2
                      cout << "size2: " << ((ROOT->children)[0].children).size() << endl; // it should output 2
                      system("pause");
                  }
                  
                  void gSpan::insert(int v) {
                  
                      while(v <= PREV->len)
                          PREV = PREV->prev;
                      Node *cur = new Node();
                      cur->len = v;
                      cur->prev = PREV;
                      PREV->children.push_back(*cur);
                      PREV = cur;
                  
                  }
                  

                  推荐答案

                  问题是你的 children 向量包含 Node 值而不是 Node*指针.虽然您的访问正确使用了根,但它只会找到您尝试维护的子项的副本.你的所有节点也都泄露了.

                  The problem is that you children vector contains Node values rather than Node* pointers. While your access uses the root correctly, it finds only copies of the children you try to maintain. All of your nodes are also leaked.

                  您可能希望为您的孩子使用 std::vector 并在某个时候删除他们.最简单的方法可能是使用智能指针向量,例如一个 tference 计数指针,并让智能指针负责释放.

                  You might want to use a std::vector<Node*> for your children and delete them at some point. The easiest way is probably to use a vector of smart pointers, e.g. a teference counted pointer, and have the smart pointer take care of the release.

                  这篇关于我的 DFS 树 (C++) 的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                      <tfoot id='WZ1FC'></tfoot>
                        <tbody id='WZ1FC'></tbody>
                      <i id='WZ1FC'><tr id='WZ1FC'><dt id='WZ1FC'><q id='WZ1FC'><span id='WZ1FC'><b id='WZ1FC'><form id='WZ1FC'><ins id='WZ1FC'></ins><ul id='WZ1FC'></ul><sub id='WZ1FC'></sub></form><legend id='WZ1FC'></legend><bdo id='WZ1FC'><pre id='WZ1FC'><center id='WZ1FC'></center></pre></bdo></b><th id='WZ1FC'></th></span></q></dt></tr></i><div id='WZ1FC'><tfoot id='WZ1FC'></tfoot><dl id='WZ1FC'><fieldset id='WZ1FC'></fieldset></dl></div>

                    • <small id='WZ1FC'></small><noframes id='WZ1FC'>

                            <bdo id='WZ1FC'></bdo><ul id='WZ1FC'></ul>
                            <legend id='WZ1FC'><style id='WZ1FC'><dir id='WZ1FC'><q id='WZ1FC'></q></dir></style></legend>