树不维护递归迭代器成员
Tree not maintaining iterator members on recursion
我正在构建一个简单的边界体积层次结构,在二叉搜索树结构中实现。 有时,树节点最终会附加多个对象(例如,如果它们的质心位于同一位置(。 因此,我必须让每个节点维护一个对象集合,而不是单个指针。
我试图通过迭代器让每个树节点包含对象集合的特定切片来做到这一点。 在生成树时分发它们似乎可以正常工作,但是迭代器在检索叶子时会以某种方式损坏。
代码的简化版本:
#include <iostream>
#include <cstdlib>
#include "util.h"
#include <vector>
#include <algorithm>
using namespace std;
class Point {
public:
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
double x;
double y;
};
Point elementWiseMin(Point a, Point b) {
return Point(min(a.x, b.x), min(a.y, b.y));
}
Point elementWiseMax(Point a, Point b) {
return Point(max(a.x, b.x), max(a.y, b.y));
}
class BoundingBox {
public:
BoundingBox(Point minCorner, Point maxCorner) : minCorner(minCorner), maxCorner(maxCorner) {}
Point minCorner;
Point maxCorner;
};
class Shape {
public:
Shape(BoundingBox* obj, Point x) : boundingBox(obj), centroid(x) {}
BoundingBox* boundingBox;
Point centroid;
};
typedef vector<Shape*> ShapeContainer;
class Tree {
public:
Tree(ShapeContainer::iterator begin, ShapeContainer::iterator end) :
begin(begin), end(end), hasNodes(true) {
cout << end - begin << "n";
if (end - begin < 1) hasNodes = false;
else {
minCorner = (*begin)->boundingBox->minCorner;
maxCorner = (*begin)->boundingBox->maxCorner;
for (ShapeContainer::iterator i = begin + 1; i < end; i++) {
minCorner = elementWiseMin(minCorner, (*i)->boundingBox->minCorner);
maxCorner = elementWiseMax(maxCorner, (*i)->boundingBox->maxCorner);
}
double split = minCorner.x + ((maxCorner.x - minCorner.x) / 2);
splitAt = split;
ShapeContainer::iterator middle = partition(begin, end,
[split](Shape* n) {
return n->centroid.x < split;
});
if (middle - begin > 0 && end - middle > 0) {
Tree c1 = Tree(begin, middle);
child1 = &c1;
Tree c2 = Tree(middle, end);
child2 = &c2;
}
}
}
ShapeContainer::iterator begin;
ShapeContainer::iterator end;
Point minCorner;
Point maxCorner;
Tree* child1 = nullptr;
Tree* child2 = nullptr;
bool hasNodes;
double splitAt;
//get the node at x
Tree* getNode(int x) {
cout << end - begin << "n";
if (x < splitAt) {
if (child1 == nullptr) return this;
else return child1->getNode(x);
}
else {
if (child2 == nullptr) return this;
else return child2->getNode(x);
}
}
};
int main() {
ShapeContainer container;
Shape shape1 = Shape(new BoundingBox(Point(0, 0), Point(1, 1)), Point(0.5, 0.5));
container.push_back(&shape1);
Shape shape2 = Shape(new BoundingBox(Point(-2, 0), Point(-1, 1)), Point(-1.5, 0.5));
container.push_back(&shape2);
Shape shape3 = Shape(new BoundingBox(Point(2, 0), Point(3, 1)), Point(2.5, 0.5));
container.push_back(&shape3);
Shape shape4 = Shape(new BoundingBox(Point(4, 0), Point(5, 1)), Point(4.5, 0.5));
container.push_back(&shape4);
cout << "Generate treen";
Tree t = Tree(container.begin(), container.end());
cout << "Traverse treen";
Tree* node = t.getNode(-1);
return 0;
}
输出:
Generate tree
4
2
1
1
2
1
1
Traverse tree
4
-47673
生成时的输出正是我所期望的,但看起来迭代器没有正确保存到子级。
在Tree
构造函数的末尾,将局部变量的地址分配给Tree
类的成员 ( child1 = &c1
(。 这很糟糕,因为几行后c1
对象超出范围并将被销毁。 这使得child1
(和child2
(成为悬空指针,因为它们指向的对象不再有效。
您可能希望使用动态内存分配(使用 unique_ptr
或 shared_ptr
(来正确构造树。
相关文章:
- "迭代器"和"const_iterator"不是 STL 容器的必需成员?
- 如何将迭代器调用转发给类的私有成员?
- 成员函数中的迭代器出现问题
- 如何在C++中仅获取容器内类成员的迭代器
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 为什么向量的.at()成员函数返回引用而不是迭代器
- 将列表迭代器存储为成员
- 使用迭代器成员函数是否仅适用于某些向量类型"empty()"?
- C++ 向量::使用类对象迭代器擦除不擦除向量成员
- 树不维护递归迭代器成员
- 如何将迭代器变量声明为私有成员变量
- 我如何从循环内部使用迭代器的函数内部的getter中检索特定的成员
- C :我可以将成员变量的迭代器带入班级之一
- 如何使用C STD :: SET中的迭代器访问成员功能
- C++:将成员容器的迭代器接口转发到类接口
- 如何从 std 容器的迭代器为成员元素创建迭代器
- 通过迭代器为映射的成员赋值
- 如果我创建一个修改值的迭代器,静态成员"reference"应该是什么类型?
- C++迭代器作为类方法中使用的类成员
- 类成员向量上的迭代器未正确取消引用