4.2.10 使用empty()成员函数

2013-10-07 14:44:48 · 作者: · 浏览: 56

4.2.10  使用empty()成员函数

最后,程序检查主人公物品栏中是否还有任何物品。

  1. if (inventory.empty())  
  2. {  
  3. cout << "\nYou have nothing.\n";  
  4. }  
  5. else  
  6. {  
  7. cout << "\nYou have at least one item.\n";  
  8. }  

vector的成员函数empty()的作用和string的成员函数empty()一样。如果vector对象为空,则返回true;否则返回false。因为inventory在此处为空,所以程序显示消息"You have nothing."。