C++ Iterator
Page 1 of 1
TheDiggler




Posts: 564

PostPosted: Sat, 8th Apr 2017 01:44    Post subject: C++ Iterator
I have the following book code which is throwing up a "Expression: iterator not decrementable". It tries to read past vect.begin() after it completes going backwards, but why?

Code:

#include <iostream>
#include <vector>   // Include the vector header
using namespace std;

int main()
{
   int count;   // Loop counter

   // Define a vector object.
   vector<int> vect;

   // Define an iterator object.
   vector<int>::iterator iter;

   // Use push_back to push values into the vector.
   for (count = 0; count < 10; count++)
      vect.push_back(count);

   // Step the iterator through the vector, and use it to display the vector's contents.
   cout << "Here are the values in vect: ";
   for (iter = vect.begin(); iter < vect.end(); iter++)
   {
      cout << *iter << " ";
   }

   // Step the iterator through the vector backwards.
   cout << "\nand here they are backwards: ";
   for (iter = vect.end() - 1; iter >= vect.begin(); iter--)
   {
      cout << *iter << " ";
   }

   return 0;
}
Back to top
TheDiggler




Posts: 564

PostPosted: Sat, 8th Apr 2017 03:41    Post subject:
Made the following change:

Code:

for (iter = vect.end() - 1; iter != vect.begin(); iter--)


Why >= not work but != work, what am I not understanding about vect.begin()? Thanks.


Sorry, never mind that will not get the first element.
Back to top
[mrt]
[Admin] Code Monkey



Posts: 1338

PostPosted: Tue, 18th Apr 2017 18:32    Post subject:
Hey,

Its quite simple. When you are at your last item, namely when iter = v.begin your postloop expression iter-- executes one last time pushing your iterator past its bounds.

Here is a little tip. To iterate backwards simply replace v.begin() with v.rbegin() and use your normal loop so basically it looks like this:

for(iter = v.rbegin(); iter != v.rend(); iter++)

Have fun!


teey
Back to top
TheDiggler




Posts: 564

PostPosted: Tue, 18th Apr 2017 18:47    Post subject:
Thanks mrt!

After I realized there was no way it was ever going to work I broke down and used a reverse_iterator and a FOR loop as you posted. I sent an e-mail to the author of the book and informed him of his codes oversight.

Thanks again!
Back to top
[mrt]
[Admin] Code Monkey



Posts: 1338

PostPosted: Tue, 18th Apr 2017 18:53    Post subject:
You're welcome. You did the right thing Smile

Which book had this blatant bug laying inside?

Must have been awkward hehe, did you ever recieve a reply?


teey
Back to top
TheDiggler




Posts: 564

PostPosted: Sat, 22nd Apr 2017 00:28    Post subject:
Starting Out with C++: Early Objects (8th Edition)
https://www.amazon.com/Starting-Out-Early-Objects-8th/dp/013336092X

I am using the 7th Edition but the code was confirmed in the 8th. I never heard back after reporting the issue.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group