< Previous | Contents | Next >
Next, I loop through the contents of the vector and display the hero’s inventory.
cout << "Your items:\n";
for (iter = inventory.begin(); iter != inventory.end(); ++iter)
{
cout << *iter << endl;
}
In the preceding code, I use a for loop to move from the first to the last element of inventory. At this general level, this is exactly how I looped through the
contents of the vector in Hero’s Inventory 2.0. But instead of using an integer and the subscripting operator to access each element, I used an iterator. Basically, I moved the Post-it note through the entire sequence of elements and displayed the value of each element to which the note was stuck. There are a lot of new ideas in this little loop, so I’ll tackle them one at a time.
In the initialization statement of the loop, I assign the return value of inventory. begin() to iter. The begin() member function returns an iterator that refers to a container’s first element. So in this case, the statement assigns an iterator that refers to the first element of inventory (the string object equal to "sword") to iter. Figure 4.3 shows an abstract view of the iterator returned by a call to inventory.begin(). (Note that the figure is abstract because the vector inventory doesn’t contain the string literals "sword", "armor", and "shield"; it contains string objects.)
Figure 4.3
A call to inventory.begin() returns an iterator that refers to the first element in the vector.
In the test statement of the loop, I test the return value of inventory.end() against iter to make sure the two are not equal. The end() member function returns an iterator one past the last element in a container. This means the loop will continue until iter has moved through all of the elements in inventory. Figure 4.4 shows an abstract view of the iterator returned by a call to this member function. (Note that the figure is abstract because the vector inventory doesn’t contain the string literals "sword", "armor", and "shield"; it contains string objects.)
128 Chapter 4 n The Standard Template Library: Hangman
Figure 4.4
A call to inventory.end() returns an iterator one past the last element of the vector.
The end() vector member function returns an iterator that’s one past the last element in the vector—not the last element. Therefore, you can’t get a value from the iterator returned by end(). This might seem counter-intuitive, but it works well for loops that move through a container.
The action statement in the loop, ++iter, increments iter, which moves it to the next element in the vector. Depending upon the iterator, you can perform other mathematical operations on iterators to move them around a container. Most often, though, you’ll find that you simply want to increment an iterator.
In the loop body, I send *iter to cout. By placing the dereference operator (*) in front of iter, I display the value of the element to which the iterator refers (not the iterator itself). By placing the dereference operator in front of an iterator, you’re saying, “Treat this as the thing that the iterator references, not as the iterator itself.”