[英]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:
由于发布的代码不完整,我不知道这些方法是否能真正解决您的问题:
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>*
构造。 operator++()
that the iterator can be incremented. operator++()
的前提。 If anything, the operator should report a misuse with debug settings enabled. last
: note that the end iterator is a position one past the last element. last
:注意,结束迭代器是一个位置的一个过去的最后一个元素。 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. 您没有发布显示
first
和last
的列表实现,但是我假设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.