< Previous | Contents | Next >

Changing the Value of a Vector Element

Next, I change the first element in the vector from the string object equal to "sword" to the string object equal to "battle axe". First I set myIterator to reference the first element of inventory.

myIterator = inventory.begin();

Then I change the value of the first element.

Using Iterators 129


*myIterator = "battle axe";

Remember, by dereferencing myIterator with *, the preceding assignment statement says, Assign "battle axe" to the element that myIterator references.It does not change myIterator. After the assignment statement, myIterator still refers to the first element in the vector.

Just to prove that the assignment worked, I then display all of the elements in

inventory.