iterator problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

iterator problem

 
0
  #1
Jan 21st, 2009
I would expect this to ouput the 5 doubles, but instead I get the output below.

  1. vector<double> a(5);
  2. a.push_back(1.2);
  3. a.push_back(1.3);
  4. a.push_back(1.4);
  5. a.push_back(1.5);
  6. a.push_back(1.6);
  7.  
  8. vector<double>::iterator i;
  9.  
  10. i = a.begin();
  11.  
  12. while( i != a.end() )
  13. {
  14. cout << *i << endl;
  15. i++;
  16. }

  1. 0
  2. 0
  3. 0
  4. 0
  5. 0
  6. 1.2
  7. 1.3
  8. 1.4
  9. 1.5
  10. 1.6

Any ideas why?

Thanks,

Dave
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: iterator problem

 
0
  #2
Jan 21st, 2009
>>vector<double> a(5);
That creates an array of 5 doubles.

>> a.push_back(1.2);
That adds an additional element to the array, so after this line executes the array will have 6 elements.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: iterator problem

 
0
  #3
Jan 21st, 2009
oh hahahahaha geez I thought it was something to do with the iterator.. but it's just PEBCAK (isn't that the acronym? problem exists between chair and keyboard?) haha
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC