C ++中的模板迭代器,解引用和typedef

时间:2016-04-15 08:05:28

标签: c++ recursion iterator dereference quadtree

我从我正在处理的程序中得到了奇怪的结果,无法弄清楚错误的位置或原因。我正在研究一个使用模板化迭代器的模板化四叉树类。即使我的函数似乎正常工作,它也不会返回它找到的值。

这是我的基本四叉树课程:

template <class number_type, class label_type>
class QuadTree {
public:
    // CONSTRUCTORS
    QuadTree() : size_(0), root_(NULL) {}

    // ACCESSORS
    unsigned int size() const { return size_; }

    typedef DepthIterator<number_type, label_type> iterator;

    iterator begin() const {
        return root_;
    }
    iterator end() const { return iterator(NULL); }

    // This is the function not returning the expected value 
    iterator find(number_type x, number_type y, Node<number_type, label_type>* parent = NULL) {
        if (parent == NULL) {
            // start at the root, if equal return, otherwise call recursively
            if (root_->pt.x == x && root_->pt.y == y) { return iterator(root_); }
            else {
                return find(x, y, root_);
            }
        }
        else {
            int quadrant;
            // find the quadrant of the point
            if (x < parent->pt.x && y < parent->pt.y) {
                quadrant = 0;
            }
            else if (x > parent->pt.x && y < parent->pt.y) {
                quadrant = 1;
            }
            else if (x < parent->pt.x && y > parent->pt.y) {
                quadrant = 2;
            }
            else if (x > parent->pt.x && y > parent->pt.y) {
                quadrant = 3;
            }
            // see if the quadrant is a branch or a leaf
            if (parent->children[quadrant] == NULL) {
                // child is a leaf and it matches the search
                if (x == parent->pt.x && y == parent->pt.y) { return iterator(parent); }
                // child is a leaf, but it does not match the search
                else { return iterator(NULL); }
            }
            else {
                // child is a branch, check if it's equal and return if it is, if not, continue on
                if (x == parent->pt.x && y == parent->pt.y) { return iterator(parent); }
                else { find(x, y, parent->children[quadrant]); }
            }
        }
    }
private:
    unsigned int size_;
    Node<number_type, label_type>* root_;
};

似乎我的find函数在我的四叉树上找到了正确的节点,因为我可以使用std :: cout打印出父节点,然后返回它以获得正确的值。然而

return iterator(parent);

给我一​​个NULL值或点(9,0),这不是我的四叉树。

这是我的DepthIterator类:

template <class number_type, class label_type>
class DepthIterator {
public:
    // CONSTRUCTOR / COPY / ASSIGN / DECONSTRUCTOR
    DepthIterator() : ptr_(NULL) {}
    DepthIterator(Node<number_type, label_type>* n) : ptr_(n) {}
    DepthIterator(const DepthIterator& old) : ptr_(old.ptr_) {}
    ~DepthIterator() {}

    // OPERATORS
    DepthIterator& operator=(const DepthIterator& old) { ptr_ = old.ptr_; return *this; }
    const Point<number_type>& operator*() const { return ptr_->pt; }
    bool operator== (const DepthIterator& rgt) { return ptr_ == rgt.ptr_; }
    bool operator!= (const DepthIterator& rgt) { return ptr_ != rgt.ptr_; }

    // ACCESSORS
    label_type getLabel() const { return ptr_->label; }


private:
    Node<number_type, label_type>* ptr_;
};

也许我的副本或解除引用位是问题?我倾向于取消引用,因为我的find函数在使用该操作之前确实有效,但之后却没有。

这是我的Node类:

template <class number_type, class label_type>
class Node {
public:
    Node() {}
    Node(Point<number_type> p, label_type l) : pt(p), label(l) { parent = NULL; }

    Node& operator=(const Node& old) { pt = old.pt; label = old.label; parent = *old; return *this; }

    Point<number_type> pt;
    label_type label;
    Node<number_type, label_type>* children[4];
    Node<number_type, label_type>* parent;
};

我的主要功能包含以下内容:

QuadTree<int, char>::iterator itr = test.find(20,10);
// the point (20,10) does exist in the quadtree
assert (itr != test.end());
assert (itr.getLabel() == 'A');
// both of these tests pass

const Point<int> &pt = *itr;
assert (pt.x == 20);
assert (pt.y == 10);
// both of these tests pass

QuadTree<int,char>::iterator itr = test.find(4,7);
// the point (4,7) does exist in the quadtree
assert (itr != test.end());
// this test fails however ^
assert (itr.getLabel() == 'B');

第一次测试有效,但第二次没有。我不确定这是因为第一个测试是根节点,第二个测试不是,或者是因为我在中间取消引用,或完全是其他原因。我对C ++有些新意,并且很难实现这一点。谁能解释为什么我的查找功能不起作用?

1 个答案:

答案 0 :(得分:0)

 iterator find(number_type x, number_type y, Node<number_type, label_type>* parent = NULL) {
    if (parent == NULL) {
        // start at the root, if equal return, otherwise call recursively
        if (root_->pt.x == x && root_->pt.y == y) { return iterator(root_); }
        else {
            return find(x, y, root_);
        }
    }
    else {
        int quadrant;
        // find the quadrant of the point
        if (x < parent->pt.x && y < parent->pt.y) {
            quadrant = 0;
        }
        else if (x > parent->pt.x && y < parent->pt.y) {
            quadrant = 1;
        }
        else if (x < parent->pt.x && y > parent->pt.y) {
            quadrant = 2;
        }
        else if (x > parent->pt.x && y > parent->pt.y) {
            quadrant = 3;
        }
        // see if the quadrant is a branch or a leaf
        if (parent->children[quadrant] == NULL) {
            // child is a leaf and it matches the search
            if (x == parent->pt.x && y == parent->pt.y) { return iterator(parent); }
            // child is a leaf, but it does not match the search
            else { return iterator(NULL); }
        }
        else {
            // child is a branch, check if it's equal and return if it is, if not, continue on
            if (x == parent->pt.x && y == parent->pt.y) { return iterator(parent); }
            else { find(x, y, parent->children[quadrant]); }
        }
    }
}

问题是未定义的行为,如果x == parent-&gt; pt.x或y == parent-&gt; pt.y因为象限将是未定义的,因为没有if条件的计算结果为true。在这种情况下parent->children[quadrant] == NULL表现得像薛定谔的猫,它可能是真的,它可能是假的。

可能的修复方法可能是:

if (x <= parent->pt.x && y <= parent->pt.y) {
        quadrant = 0;
    }
    else if (x > parent->pt.x && y <= parent->pt.y) {
        quadrant = 1;
    }
    else if (x <= parent->pt.x && y > parent->pt.y) {
        quadrant = 2;
    }
    else if (x > parent->pt.x && y > parent->pt.y) {
        quadrant = 3;
    }