Vector Iterator

Discussion in 'C++' started by JackC, Apr 22, 2008.

  1. JackC

    JackC Guest

    Hi,

    If i have a std::vector containing pointers, then iterate through the
    vector and im slightly confused as to how i use the item from the
    iterator?

    for example,

    vector<MyClass*> myvector;
    ....
    vector<MyClass*>::iterator it;

    for(it = myvector.begin(); it < myvector.end(); it++)
    {
    // This doesn't work,
    **it->myclassmember();
    }

    What am i doing wrong? I thought the iterator is a pointer to a
    pointer in this case?

    Thanks alot for any help,
    JackC, Apr 22, 2008
    #1
    1. Advertising

  2. On 2008-04-22 19:25, JackC wrote:
    > Hi,
    >
    > If i have a std::vector containing pointers, then iterate through the
    > vector and im slightly confused as to how i use the item from the
    > iterator?
    >
    > for example,
    >
    > vector<MyClass*> myvector;
    > ...
    > vector<MyClass*>::iterator it;
    >
    > for(it = myvector.begin(); it < myvector.end(); it++)
    > {
    > // This doesn't work,
    > **it->myclassmember();
    > }
    >
    > What am i doing wrong? I thought the iterator is a pointer to a
    > pointer in this case?


    Use either

    (*it)->myclassmember();

    or

    (**it).myclassmember();

    --
    Erik Wikström
    Erik Wikström, Apr 22, 2008
    #2
    1. Advertising

  3. JackC

    JackC Guest

    On 22 Apr, 18:50, Erik Wikström <> wrote:
    > On 2008-04-22 19:25, JackC wrote:
    >
    >
    >
    > > Hi,

    >
    > > If i have a std::vector containing pointers, then iterate through the
    > > vector and im slightly confused as to how i use the item from the
    > > iterator?

    >
    > > for example,

    >
    > > vector<MyClass*> myvector;
    > > ...
    > > vector<MyClass*>::iterator it;

    >
    > > for(it = myvector.begin(); it < myvector.end(); it++)
    > > {
    > > // This doesn't work,
    > > **it->myclassmember();
    > > }

    >
    > > What am i doing wrong? I thought the iterator is a pointer to a
    > > pointer in this case?

    >
    > Use either
    >
    > (*it)->myclassmember();
    >
    > or
    >
    > (**it).myclassmember();
    >
    > --
    > Erik Wikström


    Thanks very much, works fine.
    JackC, Apr 22, 2008
    #3
  4. JackC

    Guest

    On Apr 22, 8:36 pm, Pete Becker <> wrote:
    > On 2008-04-22 13:50:53 -0400, Erik Wikström <> said:

    ...
    > Or
    >
    > it->myclassmember();
    >

    ...

    Is that possible? myvector is a vector of pointers.

    Regards.
    , Apr 22, 2008
    #4
  5. JackC

    sk_usenet Guest

    <> wrote in message
    On Apr 22, 8:36 pm, Pete Becker <> wrote:
    >> On 2008-04-22 13:50:53 -0400, Erik Wikström <>
    >> said:
    >> it->myclassmember();


    > Is that possible? myvector is a vector of pointers.


    No. Probably Pete was in a hurry.

    --
    http://techytalk.googlepages.com
    sk_usenet, Apr 22, 2008
    #5
  6. JackC

    Ian Collins Guest

    Pete Becker wrote:
    > On 2008-04-22 16:38:22 -0400, said:
    >
    >> On Apr 22, 8:36 pm, Pete Becker <> wrote:
    >>> On 2008-04-22 13:50:53 -0400, Erik Wikström <> sa

    >> id:
    >> ...
    >>> Or
    >>>
    >>> it->myclassmember();
    >>>

    >> ...
    >>
    >> Is that possible? myvector is a vector of pointers.
    >>

    >
    > Try it.
    >

    Ant you'll get a compile error!

    --
    Ian Collins.
    Ian Collins, Apr 22, 2008
    #6
    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. pmatos
    Replies:
    6
    Views:
    23,782
  2. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,576
    Francesco S. Carta
    Sep 7, 2010
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    357
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page