如何从树中检索每个元素
How to retrieve every single element from a tree?
我被困在这里好几天了。有没有人可以看看toString方法在行类,我怎么能从点的树检索每一个点,并从点的字符值?我知道在vector或array中,我们可能会使用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中。非常感谢你给我的提示!
binNode
模板并不真正支持这种用法,因为它不公开它的子元素。它也不提供iterator
。这使得在类之外的代码中遍历树变得不可能。子类甚至对子类都不可见,所以你不能那样扩展它。
您可以为Point
专门化binNode
模板,并强制它向全局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
,即使在单独的bintree
上,这当然不会起作用。
要正确实现这些功能,您唯一的选择是修改bintree
和binNode
。
相关文章:
- 如何从C++程序中检索 DOM 元素?
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- 如何使用 STL 哈希容器中的哈希值检索元素?
- 在 QSet 中插入元素<QString>并检索 ,删除 QSet 的最后一项
- 如何检索向量中第一个找到的值最低的元素
- 如何从boost :: multi_index_container检索元素
- 如何使用libxml2从C 中的XML文件中检索节点和特定元素字符串而不使用XPATH
- 如何使用XPath使用libxml2从C 中的XML文件中检索节点和特定元素字符串
- 检索 c++ 向量中的第一个元素
- 如何从 O(1) C++哈希表中随机检索元素
- 如何按插入顺序从映射中检索元素
- 为什么只有非常量映射提供类似于关联数组的直接元素检索?
- MSVC 2013 Bug?正在从映射的容器中检索最后一个元素
- 检索CV_32FC3 CvMat的元素
- Qt c++ QDomDocument,递归迭代XML数据,检索没有子数据的文本元素
- c++ STL中是否有数据结构可以对log(n)中的第k个元素进行插入、搜索和检索?
- 为什么从CMap中检索元素是无序的
- 如何从树中检索每个元素
- 在c++中检索std::map的随机键元素
- 检索一个元素