I have a question on Chapter 4, exercise 1 of Michael Dawson's "Beginning C++ through Game Programming 2nd Edition".
The chapter goes over STL, vectors and iterators. The assignment is to create a program that allows the user to edit games in a list. In the book we learn about member functions .begin, .end, .insert, .erase, etc etc. The thing is I don't want to ONLY allow the user to point to the beginning OR end of the vector object "gameLibrary". Here's what I thought would work:
Create an int variable that allows the user to enter the number in the list they want to select, THEN with that int number, use it to refer to a number in the vector object container "gameLibrary". Ideally then use that number to a).erase the entry, b).insert new entry.
Here's the problems I'm having:
1 - FOR LOOP
This doesn't work:
for (int i = 0; i < gameLibrary.size(); ++i)
counter = i;
cout << counter << "-";
cout << gameLibrary[i] << endl;
but this does:
for (int i = 0; i < gameLibrary.size(); ++i)
cout << gameLibrary[i] << endl;
Is the use of the 'i' throwing off my counter? I keep getting an error that 'i' changed in the first example. Is the for loop automatically counting up? or is the use of a for loop not intended to use the 'i' more than once per cycle in these cases?
2 - Deferencing Iterator
Iterators established earlier:
vector<string>::iterator myIterator;
vector<string>::const_iterator iter;
This works:
cout << "\nThe item name '" << *myIterator << "' has ";
cout << myIterator->size() << " letters in it.\n";
This gives me an error:
cout << "\nThe item name '" << myIterator << "' has ";
cout << myIterator.size() << " letters in it.\n";
I don't understand why the deferencing iterator is ok, but when I try to cout the non deferenced iterator it gives me an error? I would think the non-Def iterator would just cout the number of the container it's referring to.
THANKS in advance to anyone helping.