list Iterator Dereferencing Problem

M

Mike Copeland

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
 
I

Ian Collins

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());
}
}
 
M

Mike Copeland

I know I'm doing something wrong here, but I'm unable to see what it
Declare this when you initialise it.


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.
 
I

Ian Collins

Why is that? Please explain.

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

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

Joshua Maurice

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.)
 

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,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top