如何从树中检索每个元素?

时间:2014-05-20 10:32:38

标签: c++

我被困在这里几天了。任何人都可以看看Row类中的toString方法,我如何从点树中检索每个点并从该点获取char值?我知道在向量或数组中我们可以使用for循环并执行类似find(points [i])之类的操作,但我真的不知道这里。

   **std::string toString() const
   {
      string s;
      for(int i = 0; i != points.size(); i++)
      {
         //Maybe use points.find(???) here to retrieve the point
         s.push_back(point.getType()); //Just for demonstration, this does not work
         return s;

      }
   }**
};

所以我无法修改binNode binTree类。我不能在Point类中使用print方法,因为print()方法会在新行中打印每个元素。但是我们必须从每个点检索char值,并将所有点的所有char值附加到String中。 非常感谢你给我一个提示!

1 个答案:

答案 0 :(得分:1)

binNode模板并不真正支持这种用法,因为它不会暴露它的子项。它也不提供iterator。这使得无法在类外部的代码中遍历树。子类甚至对子类都不可见,因此您无法以这种方式扩展它们。

你可能会对binNode的{​​{1}}模板感到愚蠢并专门化,并强制它将输出发送到全局Point

string

如果您有多个线程即使在单独的std::string output; // a global template <> class binNode<Point> { void print() const { if (left != NULL) left->print(); output += nodeData.getType(); if (right != NULL) right->print(); } }; // in some function output.clear(); // remember to clear previous output points.print(); // the string now has the output from `print` 上调用print,这当然也行不通。

要正确实施此类功能,您唯一的选择就是修改bintreebintree