Originally Posted by
mickey
I don't understand what want you say with your 2nd hint: beginning = top stack? If I understand fine, in this code, head points to the top element (the last inserted). Maybe Are you saying that must be the opposite?
In the first version you had:
iterator end() const {
cout << "end " << iterator(*this) << endl;
return iterator( *this);
}
iterator begin() const {
cout << "begin() " << iterator() << endl;
return iterator( );
}
iterator end() const {
cout << "end " << iterator(*this) << endl;
return iterator( *this);
}
iterator begin() const {
cout << "begin() " << iterator() << endl;
return iterator( );
}
To copy to clipboard, switch view to plain text mode
iterator::iterator() creates an iterator that points nowhere, because current == 0 and iterator::iterator( Stack<Type,size>& ) creates an iterator that points to the top of the stack.
Now, you would like to use those iterators like this:
for( Stack<...>::iterator it = stack.begin(); it != stack.end(); ++it ) {
// ...
}
for( Stack<...>::iterator it = stack.begin(); it != stack.end(); ++it ) {
// ...
}
To copy to clipboard, switch view to plain text mode
But the problem is that begin() returns an invalid iterator (because it uses iterator::iterator()), so ++it doesn't work.
Originally Posted by
mickey
why can I use :
cout << it.getValue() << endl;
and not
cout << (*it).getValue() << endl;
Because "it" is an object not a pointer.
Bookmarks