簡體   English   中英

C ++在迭代器類中定義析構函數?

[英]c++ define a destructor in an iterator class?

我建立了一個旨在檢查二叉樹的迭代器類。 該迭代器定義如下:

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它的迭代。 因此,它不應刪除它。

迭代器不需要析構函數,也不需要它需要的任何其他特殊功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM