Erratic Container Behavior (list & vector)

M

Mike Copeland

I'm experiencing strange behavior from both a std::list and
std::vector - it's consistently returning data out of the order I placed
the data. I have the following declaration:

vector<string> l2Vect;
vector<string>::const_iterator l2Iter;

I'm adding the following data to it:

004 1 M 1-19 Males Age Group 1-19
004 2 M20-24 Males Age Group 20-24
004 3 M25-29 Males Age Group 25-29
004 4 M30-34 Males Age Group 30-34
004 5 M35-39 Males Age Group 35-39
004 6 M40-44 Males Age Group 40-44
004 7 M45-49 Males Age Group 45-49
004 8 M50-54 Males Age Group 50-54
004 9 M55-59 Males Age Group 55-59
004 10 M60-64 Males Age Group 60-64
004 11 M65-69 Males Age Group 65-69
004 12 M70-88 Males Age Group 70-88
004 13 M89-89 Males Unknown Age
004 30 MWHEEL Males WheelChairs
004 31 F 1-19Females Age Group 1-19
004 32 F20-24Females Age Group 20-24
004 33 F25-29Females Age Group 25-29
004 34 F30-34Females Age Group 30-34
004 35 F35-39Females Age Group 35-39
004 36 F40-44Females Age Group 40-44
004 37 F45-49Females Age Group 45-49
004 38 F50-54Females Age Group 50-54
004 39 F55-59Females Age Group 55-59
004 40 F60-64Females Age Group 60-64

via push_back() - as well as other data before and after this data,
including the following:

005 E 0 0 ;Initial EntId values
009 18.205
014 20
010 True
011 3
032 0 0.0
005 Xa5K Run
039 0 0
005 Q 0 0 ;Skipped Finishers

I have watched the data come in and be stored in the above sequence -
it's as I expected.
HOWEVER, when I retrieve the data it comes out like this(!):

004 1 M 1-19 Males Age Group 1-19
004 3 M25-29 Males Age Group 25-29
004 5 M35-39 Males Age Group 35-39
004 7 M45-49 Males Age Group 45-49
004 9 M55-59 Males Age Group 55-59
004 11 M65-69 Males Age Group 65-69
004 13 M89-89 Males Unknown Age
004 31 F 1-19Females Age Group 1-19
004 33 F25-29Females Age Group 25-29
004 35 F35-39Females Age Group 35-39
004 37 F45-49Females Age Group 45-49
004 39 F55-59Females Age Group 55-59
004 41 F65-69Females Age Group 65-69
004 43 F89-89Females Unknown Age
005 E 0 0 ;Initial EntId values
009 18.205
014 20
010 True
032 0 0.0
039 0 0
004 1 M 1-19 Males Age Group 1-19
004 3 M25-29 Males Age Group 25-29
004 5 M35-39 Males Age Group 35-39
004 7 M45-49 Males Age Group 45-49
004 9 M55-59 Males Age Group 55-59
004 11 M65-69 Males Age Group 65-69
004 13 M89-89 Males Unknown Age
004 31 F 1-19Females Age Group 1-19
004 33 F25-29Females Age Group 25-29
004 35 F35-39Females Age Group 35-39
004 37 F45-49Females Age Group 45-49
004 39 F55-59Females Age Group 55-59
004 41 F65-69Females Age Group 65-69
004 43 F89-89Females Unknown Age

In other words, some of the records I stored are returned, but others
are not! Also, the records I got back are returned again!
I'm doing no sort of this vector, and the only thing I'm doing is to
pass through the vector data to retrieve a few records I added (8 total
that are scattered throughout the data) and erase them from the set
before I retrieve the rest. The same problem occurs with a std::list...
The purpose of the application is to add new data to a file, but I
want certain record types to appear at the "top" of the file. The data
I'm adding has different record types (such as 108, 101, 102, 103), but
I want these 4 records to appear immediately after the 008, 001, 002,
003 record that were in the original file. The remainder of the
original file and the other records I'm adding should be wherever they
were originally set or added: the 4 record types must be at the
beginning of the output file.
It seemed that a std::list or sd::vector would support this
application, and for the most part it does. The problem is that the
retrieval from whichever container I use gives the data back
inconsistently. (Note that the 011 record (and others) disappears
altogether.) <sigh>
Any thoughts? TIA
 
V

Victor Bazarov

I'm experiencing strange behavior from both a std::list and
std::vector - it's consistently returning data out of the order I placed
the data. I have the following declaration:

vector<string> l2Vect;
vector<string>::const_iterator l2Iter;

I'm adding the following data to it:
[...]
Any thoughts? TIA

Your question seems to be one of the frequently asked, and I think it's
been answered already and placed in the FAQ list. You can find it in
the section 5, q 5.8 here: http://www.parashift.com/c++-faq-lite/

V
 
W

woodbrian77

I'm experiencing strange behavior from both a std::list and
std::vector - it's consistently returning data out of the order I placed
the data. I have the following declaration:

[snip]


HOWEVER, when I retrieve the data it comes out like this(!):

004 1 M 1-19 Males Age Group 1-19
004 3 M25-29 Males Age Group 25-29
004 5 M35-39 Males Age Group 35-39
004 7 M45-49 Males Age Group 45-49
004 9 M55-59 Males Age Group 55-59
004 11 M65-69 Males Age Group 65-69

The second column is missing the even numbers.
Perhaps you're indexing is off.

Brian
Ebenezer Enterprises - Because children deserve
to have a father and a mother.

http://webEbenezer.net
 

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,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top