problem with vector

A

Alan

I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >> sensed_data.time >> sensed_data.sensor_who >>
sensed_data.aircraft_who {
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}
 
V

Victor Bazarov

Alan said:
I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >> sensed_data.time >> sensed_data.sensor_who >>
sensed_data.aircraft_who
{
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}

And how are we supposed to use it? The data file is unavailable, the
definition of 'sensed_data' type is unavailable, how 'sensor_reports' is
used before this loop is unavailable... If the printout indicates the
size of the vector is 2, it is 2. It most likely means that if you
think there was no elements before the loop is entered, you must be
mistaken, provided that the printout happens right after the first call
to 'push_back' in this loop.

Add 'assert(sensor_reports.empty());' before this code in your program.

I am not sure how else I could help with the information you've given.

V
 
A

Alan

Victor,
You are right. I did not provide more info in the post. I
found the problem, which was not even in the fragment I posted. Next
time I`ll post the whole thing or simplify to narrow in on the problem,
so folks can understand the complete problem.

Thanks, Alan
 
S

Salt_Peter

Alan said:
I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >> sensed_data.time >> sensed_data.sensor_who >>
sensed_data.aircraft_who
{
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}

Can you tell me what time it is at the North Pole right now?
Because that would probably be easier to determine rather than guessing
at your problem as it stands.

We know nothing of your structs/classes nor the constants (like
last_clock) nor do we know how the data is formatted in the file
(newlines?).
sensed_data's type looks like what? Is it copyable?
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top