c ++在迭代器类中定义析构函数?

时间:2013-12-19 08:52:24

标签: c++ data-structures iterator binary-tree destructor

我已经构建了一个旨在检查二叉树的迭代器类。这个迭代器定义如下:

template <class Key> class intervalST_const_iterator
{
//friend class IntervalST<Key>;

private:
    const Interval<Key> *interval;// equivalent to Interval<Key> const *interval; meaning: interval is apointer to the const object: Interval<Key>
                                  //meaning that the object cannot be changed via interval
                                  //interval can change here

public:
    intervalST_const_iterator(const Interval<Key> *p): interval(p){}
    //~intervalST_const_iterator() {delete interval;}
    //the const at the end of this operator means that the operator will not change *this
    bool operator != (const intervalST_const_iterator & other) const
    {
        return this->interval != other.interval;
    }
    bool operator == (const intervalST_const_iterator & other) const
    {
        return this->interval == other.interval;
    }
    //the const at the beginning means that the it's not allowed to change the object
    const Interval<Key> operator *() const
    {
        return *(this->interval);
    }
    intervalST_const_iterator<Key> & left()
    {
        interval = interval->left;
        return *this;

    }
    intervalST_const_iterator<Key> & right()
    {
        interval = interval->right;
        return *this;
    }
};

在本课程中,我还定义了一个析构函数 ~intervalST_const_iterator() {delete interval;}我已将其置于评论中。

我正在使用这个迭代器来迭代看起来像这样的树类:

template <class Key> class intervalST_const_iterator;

template <class Key> class IntervalST
{
private:
    Interval<Key> *root;

    //friend class intervalST_const_iterator<Key>;//allow the iterator class to access the private section of intervalST

    bool isRed(Interval<Key> *interval);
    Interval<Key> *rotateLeft(Interval<Key> *h);
    Interval<Key> *rotateRight(Interval<Key> *h);
    Interval<Key> *put(Interval<Key> *h,Key lo, Key hi, Key val);
    Interval<Key> *moveRedLeft(Interval<Key> *h);
    Interval<Key> *moveRedRight(Interval<Key> *h);
    Interval<Key> *deleteMin(Interval<Key> *h, Key hi);
    Interval<Key> *balance(Interval<Key> *h);
    Interval<Key> *remove(Interval<Key> *h, Key lo, Key hi);
    Interval<Key> *min(Interval<Key> *h);
    Interval<Key> *addDuplicate(Interval<Key> *h, Key hi);
    Interval<Key> *removeDuplicate(Interval<Key> *h, Key low, Key hi);
    Interval<Key> *getPointerToKey(Key low);

    void flipColors(Interval<Key> *h);
    void destroy(Interval<Key> *h);
    void printTree(Interval<Key> *h, int indent);
    Key maxVal(Interval<Key> *h);
    int size(Interval<Key> *h);
    bool isBST(Interval<Key> *x, Key min, Key max);
    inline bool isBST(){return isBST(root,0,0);}
    bool isSizeConsistent(Interval<Key> *x);
    inline bool isSizeConsistent(){return isSizeConsistent(root);}
    bool is23(Interval<Key> *x);
    inline bool is23(){return is23(root);}
    bool isBalanced();
    bool isBalanced(Interval<Key> *x,int black);
    int getKeySize(Key low);
    int compare(Key a, Key b);

public:

    //don't forget to build the constructor
    //and overload the =equal operator
    typedef intervalST_const_iterator<Key> const_iterator;//const iterator on a tree


    const_iterator begin() const;
    const_iterator end() const;

    IntervalST():root(NULL){};
    ~IntervalST();
    void remove(Key lo, Key hi);
    void put(Key lo, Key hi);
    inline int size(){return size(root);}
    inline bool isEmpty(){return root == NULL;}
    void print(int indent = 0);
    void check();


};

使用迭代器

的相应begin()end()方法
template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::begin() const
{
return const_iterator(root);
}

template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::end() const
{
//return const_iterator(NULL,this);
return const_iterator(NULL);
}

然后我正在使用一个函数 - 参见下面的文件 - 当没有定义迭代器类的析构函数时它很有效,而在定义析构函数时给出奇怪的结果

为什么会发生这种情况? 我是否需要为迭代器类定义析构函数以防止泄漏? 如何来处理这个问题? 谢谢你的帮助。

template <typename Key> std::vector<Segment<Key> > findIntersections(const IntervalST<Key> &tree ,Segment<Key> segment)
{
//initialize a const iterator on the tree
intervalST_const_iterator<Key> iterator = tree.begin();
vector<Segment<Key> > intersections;

Key k = (*iterator).max;
//walk the tree
while(iterator != tree.end())
{
    //if(*(iterator).intersects(segment.gety1(),segment.gety2())) intersections.push_back(segment);
    if(intersects((*iterator).low,(*iterator).back,segment.gety1(),segment.gety2()))
    {
        intersections.push_back(segment);
        return intersections;
    }
    else if (iterator.left() == tree.end())                                          iterator = iterator.right();
    else if (segment.gety1() > (*iterator.left()).max)                               iterator = iterator.right();
    else                                                                             iterator = iterator.left();
}
return intersections;
}

1 个答案:

答案 0 :(得分:3)

迭代器不拥有Interval,它只是指向一个迭代的IntervalST所拥有的指针。因此,它不应该删除它。

迭代器不需要析构函数,也不需要它所需的任何other special functions