如何在向量中查找特定字符串并在 if 语句中使用它

how to find a specific string in a vector and use it in an if statement

这是我的向量的开头:

vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");

此游戏的玩家将能够使用此代码块从三个项目中选择两个:

while(paid < 2)
{
    cout << "choice: ";
    cin >> equip;
    ++paid;
    inventory.insert(inventory.begin(), equip);
}

if(paid == 2)
{
    cout << "\nYou are out of money\n";
}

如果您购买了其中一个库存物品,那么会有一个角色与它互动。

if((bazaar == "woman") || (bazaar == "WOMAN"))
{
    cout << "you approach the woman, and she gives you a look.";

    if(...)
    {

    }
}

在第二个 if 语句中,我希望能够检查清单向量中的字符串,并根据该字符串是否在向量中的某处发生两件不同的事情。

(附带说明,这是在控制台中玩的基于文本的游戏的代码 window)

if(std::find(inventory.begin(), inventory.end(), std::string("sword")) != inventory.end())
    ...

别忘了包括 <algorithm>