简体   繁体   English

自己的迭代器不起作用

[英]Own Iterator doesn't work

I work on own iterator , that should iterate double linked list. 我在自己的iterator上工作,应该迭代双链表。 If I'm iterating, for cycle is immediately skipped. 如果要迭代,则立即跳过for循环。 I haven't errors, but from debugging I don't read much. 我没有错误,但是从调试中我读得很少。

for cycle: for周期:

for(SemestralWork::DoubleList<Student>::iterator it = link->begin(); it != link->end(); ++it){
         //something...
    }

iterator + begin() + end() : iterator + begin() + end()

class iterator {
private:
    Node<T> * node;
public:
    iterator(){}
    iterator(const iterator& cit){}
    iterator(T t) {

    }
    ~iterator(){}

    iterator& operator=(const iterator& second){
        node = second.node;
        return(*this);
    }
    iterator& operator++(){     
        if (node != NULL){
            node = node->GetNext();
        }
        return(*this);
    }
    iterator operator++(int){
        iterator tmp = *this;   //iterator tmp(*this)
        operator++();
        return tmp;
    }
    bool operator==(const iterator& second) {
        return node == second.node;
    }
    bool operator!=(const iterator& second) {
        return node != second.node;
    }
    T& operator*() {return node->GetData();}
    T* operator->(){return((DoubleList<T>::iterator)*this);} 
};

iterator begin(){
    return iterator(first->GetData());
}

iterator end(){
    return iterator(last->GetData());
}

Node : Node

template <class U>
    class Node{
        Node<U> * next;
        Node<U> * previous;
        U data;
    public:
        Node(const U &data){
            next = NULL;
            previous = NULL;
            this->data = data;
        }
        void SetNext(Node<U> *next) { this->next = next; }
        Node<U> *GetNext(){ return next; }
        void SetPrevious(Node<U> *previous) { this->previous = previous; }
        Node<U> *GetPrevious(){ return previous; }
        U &GetData() { return data; }
    };

Here are a few things I noticed. 这是我注意到的几件事。 Whether any of these will actually resolve your problem I don't know because the posted code is incomplete: 由于发布的代码不完整,我不知道这些方法是否能真正解决您的问题:

  1. Your iterator should not be constructed from a T object and the implementation of the constructor should actually do something (I would guesss the fact that iterator::iterator(T) doesn't do anything at all is your actual problem). 您的iterator 应该由T对象构造,并且构造函数的实现实际上应该做一些事情(我猜想iterator::iterator(T)根本不做任何事情是您的实际问题)。 Instead, the iterator should be constructed from an Node<T>* . 相反, iterator应从Node<T>*构造。
  2. The preincrement operator should not check if there is actually a next element! 该预增量运营商应该检查是否确实存在下一个元素! It is a precondition for the operator++() that the iterator can be incremented. 可以增加迭代器是operator++()的前提。 If anything, the operator should report a misuse with debug settings enabled. 如果有的话,操作员应在启用调试设置的情况下报告滥用情况。
  3. I'm suspicious of your use of last : note that the end iterator is a position one past the last element. 我怀疑你使用的last :注意,结束迭代器是一个位置的一个过去的最后一个元素。
  4. Your comparision oerators should be const members and typically operator!=() just delegates to operator==() , ie: 您的比较运算符应该是const成员,通常是operator!=()仅代表operator==() ,即:

     bool iterator::operator!=(iterator const& other) const { return !(*this == other); } 

    The advantage of this implementation is that the operator!=() is consistent with the operator==() even if the implementation of operator==() is changed. 此实现的优点是即使更改了operator==()的实现, operator==() operator!=()也与operator==()一致。

You didn't post the list implementation showing first and last , but I'm assuming last points at the last element. 您没有发布显示firstlast的列表实现,但是我假设last一点在last元素上。 With iterators, end() should point beyond the last element, not to the last element. 随着迭代器, end()应指向超出了最后一个元素而不是最后一个元素。 For example, if the list contains exactly 1 element, your for-loop won't run at all since first == last and therefore begin() == end() . 例如,如果列表中仅包含1个元素,则因为first == last ,因此begin() == end() ,for循环将根本不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM