list Iterator Dereferencing Problem

Discussion in 'C++' started by Mike Copeland, Sep 10, 2010.

  1. I have the following structure defined and declared, and I have
    populated the list with 11 records (I watched them go in during debug.)
    After building the list, I'm trying to write it out to a file...but I
    only write 11 copies if the _first_object_instance_ - not the 11
    different objects.
    I know I'm doing something wrong here, but I'm unable to see what it
    is. Please advise. TIA

    struct Tandems
    {
    long tandemLink;
    string teamMate;

    } tandemWork;
    typedef list<Tandems> TandemList;
    TandemList tandemData;
    list<TandemList>::iterator tandemIter;
    ....
    Tandems tanWork; // local work variable
    list<Tandems>::iterator xIter; // local iterator
    for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)
    {
    tanWork = (*xIter); // move list object to local variable
    printf("%12d|%s", tanWork.tandemLink,
    tanWork.teamMate.c_str());
    // *** Always seeing the first stored object here! 8<{{
    } // for
     
    Mike Copeland, Sep 10, 2010
    #1
    1. Advertising

  2. Mike Copeland

    Ian Collins Guest

    On 09/11/10 10:19 AM, Mike Copeland wrote:
    > I have the following structure defined and declared, and I have
    > populated the list with 11 records (I watched them go in during debug.)
    > After building the list, I'm trying to write it out to a file...but I
    > only write 11 copies if the _first_object_instance_ - not the 11
    > different objects.
    > I know I'm doing something wrong here, but I'm unable to see what it
    > is. Please advise. TIA
    >
    > struct Tandems
    > {
    > long tandemLink;
    > string teamMate;
    >
    > } tandemWork;
    > typedef list<Tandems> TandemList;
    > TandemList tandemData;
    > list<TandemList>::iterator tandemIter;
    > ....
    > Tandems tanWork; // local work variable


    Declare this when you initialise it.

    > list<Tandems>::iterator xIter; // local iterator
    > for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)


    Pre-increment is preferred for iterators.

    > {
    > tanWork = (*xIter); // move list object to local variable


    You aren't changing this, so it's better to write:

    const Tandems& tanWork = *xIter;

    > printf("%12d|%s", tanWork.tandemLink,
    > tanWork.teamMate.c_str());
    > // *** Always seeing the first stored object here! 8<{{
    > } // for


    Are you sure? It would have helped if you'd posted a buildable example.
    I put your snippet into a test case ant it works fine.

    #include <list>
    #include <string>
    #include <stdio.h>

    struct Tandems
    {
    long tandemLink;
    std::string teamMate;

    } tandemWork;

    typedef std::list<Tandems> TandemList;

    int main()
    {
    TandemList tandemData;

    for( int n = 0; n < 11; ++n )
    {
    Tandems t = {n, "Hello"};

    tandemData.push_back( t );
    }

    TandemList::iterator xIter;

    for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)
    {
    const Tandems& tanWork = *xIter;

    printf("%12ld|%s\n", tanWork.tandemLink,
    tanWork.teamMate.c_str());
    }
    }

    --
    Ian Collins
     
    Ian Collins, Sep 10, 2010
    #2
    1. Advertising

  3. > > I know I'm doing something wrong here, but I'm unable to see what it
    > > is. Please advise. TIA
    > >
    > > struct Tandems
    > > {
    > > long tandemLink;
    > > string teamMate;
    > >
    > > } tandemWork;
    > > typedef list<Tandems> TandemList;
    > > TandemList tandemData;
    > > list<TandemList>::iterator tandemIter;
    > > ....
    > > Tandems tanWork; // local work variable

    >
    > Declare this when you initialise it.
    >
    > > list<Tandems>::iterator xIter; // local iterator
    > > for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)

    >
    > Pre-increment is preferred for iterators.


    Why is that? Please explain.
    BTW, your example code and other hints were enough for me to get my
    code right. Although I _thought_ I saw the list being populated
    correctly during debug, it wasn't happening. It started working when I
    changed to a local iterator, and everything is okay (I believe).
    Thanks.
     
    Mike Copeland, Sep 12, 2010
    #3
  4. Mike Copeland

    Ian Collins Guest

    On 09/12/10 04:35 PM, Mike Copeland wrote:
    >>> I know I'm doing something wrong here, but I'm unable to see what it
    >>> is. Please advise. TIA
    >>>
    >>> struct Tandems
    >>> {
    >>> long tandemLink;
    >>> string teamMate;
    >>>
    >>> } tandemWork;
    >>> typedef list<Tandems> TandemList;
    >>> TandemList tandemData;
    >>> list<TandemList>::iterator tandemIter;
    >>> ....
    >>> Tandems tanWork; // local work variable

    >>
    >> Declare this when you initialise it.
    >>
    >>> list<Tandems>::iterator xIter; // local iterator
    >>> for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)

    >>
    >> Pre-increment is preferred for iterators.

    >
    > Why is that? Please explain.


    Post-increment is often implemented on to using pre-increment.

    Thing operator++(int) {
    Thing temp = *this;
    operator++();
    return temp;
    }

    --
    Ian Collins
     
    Ian Collins, Sep 12, 2010
    #4
  5. On Sep 11, 10:14 pm, Ian Collins <> wrote:
    > On 09/12/10 04:35 PM, Mike Copeland wrote:
    >
    >
    >
    > >>>      I know I'm doing something wrong here, but I'm unable to see what it
    > >>> is.  Please advise.  TIA

    >
    > >>> struct Tandems
    > >>> {
    > >>>       long   tandemLink;
    > >>>       string teamMate;

    >
    > >>> } tandemWork;
    > >>> typedef list<Tandems>   TandemList;
    > >>>       TandemList    tandemData;
    > >>>       list<TandemList>::iterator tandemIter;
    > >>> ....
    > >>>       Tandems tanWork;                // local work variable

    >
    > >> Declare this when you initialise it.

    >
    > >>>       list<Tandems>::iterator xIter;  // local iterator
    > >>>       for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)

    >
    > >> Pre-increment is preferred for iterators.

    >
    > >     Why is that?  Please explain.

    >
    > Post-increment is often implemented on to using pre-increment.
    >
    > Thing operator++(int) {
    >    Thing temp = *this;
    >    operator++();
    >    return temp;
    >
    > }


    Which in turn may be slower and less efficient than the caller using
    the prefix version where applicable. (And it's a general assumption
    that faster code, all other things being equal, is better and
    preferred.)
     
    Joshua Maurice, Sep 12, 2010
    #5
    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. Replies:
    6
    Views:
    651
    Jim Langston
    Oct 30, 2005
  2. Replies:
    12
    Views:
    5,955
    James Kanze
    Jun 24, 2007
  3. David Bilsby
    Replies:
    5
    Views:
    2,047
    David Bilsby
    Oct 9, 2007
  4. Mike Copeland

    list Iterator Dereferencing Problem

    Mike Copeland, Sep 10, 2010, in forum: C++
    Replies:
    2
    Views:
    315
    Mike Copeland
    Sep 12, 2010
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    130
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page