Question on iterator. Why is a pointer?

Discussion in 'C++' started by Hongyu, Jan 22, 2009.

  1. Hongyu

    Hongyu Guest

    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.
    Hongyu, Jan 22, 2009
    #1
    1. Advertising

  2. Hongyu

    Ian Collins Guest

    Hongyu wrote:
    > 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*

    --
    Ian Collins
    Ian Collins, Jan 22, 2009
    #2
    1. Advertising

  3. Hongyu

    Zeppe Guest

    Hongyu wrote:

    > 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
    Zeppe, Jan 23, 2009
    #3
  4. Hongyu

    Hongyu Guest

    On Jan 22, 6:40 pm, Giuliano Bertoletti <> wrote:
    > Hongyu ha scritto:
    >
    >
    >
    >
    >
    > > 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.

    >
    > 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).
    >
    > > But from the defination, I only see
    > > vector<Task *>::iterator it = tasks.begin( );, instead of vector<Task
    > > *>::iterator *it;

    >
    > That would indicate a pointer to an iterator which is not what you need.
    > An iterator is the entitiy that moves through a container.
    >
    > > 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.

    >
    > 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
    Hongyu, Jan 23, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,419
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    10
    Views:
    697
    Chris Torek
    Feb 4, 2005
  5. forester
    Replies:
    16
    Views:
    716
    Howard Hinnant
    Aug 25, 2005
Loading...

Share This Page