< Previous | Contents | Next >

Using the push_back() Member

Function

Next I give the hero the same three starting items as in the previous version of the program.

inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield");

The push_back() member function adds a new element to the end of a vector. In the preceding lines I added "sword", "armor", and "shield" to inventory. As a result, inventory[0] is equal to "sword", inventory[1] is equal to "armor", and inventory[2] is equal to "shield".