< Previous | Contents | Next >

Using the empty() Member

Function

Finally, I check to see whether the hero has any items in his inventory.

if (inventory.empty())

{

cout << "\nYou have nothing.\n";

}

else

{

cout << "\nYou have at least one item.\n";

}

The vector member function empty() works just like the string member function empty(). It returns true if the vector object is empty; otherwise, it returns false. Because inventory is empty in this case, the program displays the message, You have nothing.

Using Iterators 123