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



>我构建了一个迭代器类,旨在检查二叉树。此迭代器定义如下:

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;
}

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

迭代器不需要析构函数,也不需要任何其他特殊函数(如果需要)。

最新更新