Question on iterator. Why is a pointer?

H

Hongyu

Dear all:

I am reading "Thinking in C++" volume 2, p629, and have a question on
the below code:

.......
static void run( ) {

vector<Task *>::iterator it = tasks.begin( );
while (it != tasks.end())
(*it++)->operation( );
}

......

My question here is: why use (*it++)-> for the iterator it? which
indicate that it is a pointer. But from the defination, I only see
vector<Task *>::iterator it = tasks.begin( );, instead of vector<Task
*>::iterator *it; The vector <Task *> only indicate that the vector
contains <Task *> inside, doesn't mean it is a pointer, if from the
defination above, no pointer * is in front of it.

I believe the book is correct. But what's wrong with my understanding.

Any help for clarifying this will be appreciated.
 
I

Ian Collins

Hongyu said:
Dear all:

I am reading "Thinking in C++" volume 2, p629, and have a question on
the below code:

.......
static void run( ) {

vector<Task *>::iterator it = tasks.begin( );
while (it != tasks.end())
(*it++)->operation( );
}

......

My question here is: why use (*it++)-> for the iterator it? which
indicate that it is a pointer. But from the defination, I only see
vector<Task *>::iterator it = tasks.begin( );, instead of vector<Task
*>::iterator *it; The vector <Task *> only indicate that the vector
contains <Task *> inside, doesn't mean it is a pointer, if from the
defination above, no pointer * is in front of it.
Some implementation will use a T* as a std::vector<T> iterator and other
a class with the characteristics of a pointer.

So the iterator for a std::vector<Task*> may be a Task**. Either way,
(*it) will be a Task*.

(*it++) will yield a Task*
 
Z

Zeppe

Hongyu said:
My question here is: why use (*it++)-> for the iterator it? which
indicate that it is a pointer.

no, simply the operator*() for an iterator returns the element to which
the iterator is currently referring.
But from the defination, I only see
vector<Task *>::iterator it = tasks.begin( );, instead of vector<Task
*>::iterator *it;

this would be a pointer to an iterator, that is not what is meant by the
code.
The vector <Task *> only indicate that the vector
contains <Task *> inside, doesn't mean it is a pointer, if from the
defination above, no pointer * is in front of it.

in C++ the operator* has got much broader meaning than in C. The general
meaning is actually to return the object to which something is
referring. For an iterator, the currently visited element in a sequence.
Does it look like a pointer? That's not because the iterator is a
pointer, but because the pointer is (in some cases) an iterator :)

Best wishes,

Zeppe
 
H

Hongyu

Hongyu ha scritto:









Consider an iterator as an object which can traverse your container,
which in this case happens to be a vector of Task *.

Then:

it++ means, as usual, return the iterator then increment it to move on
the next item.

*it actually means dereferencing, that is: the current element of type
Task * where the iterator is actually on.

The -> operator is used in the usual way to access a class member of
function through a pointer.

If you prefer you can break the construct into something like:

// it is an iterator, *it is the element the iterator is on.
// do not confuse the two * which have different meanings,
// *it happens to be a pointer not because of the * prefix but
// because the vector is of Task *.
Task *item = *it;      

// call a member function of Task
item->operation( );

// move the iterator to the next element.
it++    

Actually how the iterator object is made inside is of no interest here.
Typically the iterator is defined in such a way that operators like ++
and -- are overloaded in a way to make it possible to traverse the
container even if the memory locations are not ajacent (like in a list).


That would indicate a pointer to an iterator which is not what you need.
An iterator is the entitiy that moves through a container.


You're confusing the element type of the vector, which is a pointer to a
Task class/structure and the iterator that moves along that vector.

The latter is an object of its own and can be used to access the items
it's currently on through the * operator.

Notice that I've always used the term "on" instead of "pointing to" when
  refering to an iterator to avoid confusion.

The fact that the iterator inside probably uses a pointer to keep track
of it's position is something you should ignore because the abstraction
is meant exactly for that purpose (hide the implementation).

Regards,
Giuliano Bertoletti.- Hide quoted text -

- Show quoted text -

Thanks all very much for the help, especially Giuliano and Zeppe for
the detailed explaination. I will study them in detail and understand
it better.

Thanks again and best wishes,

Hongyu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top