使用std :: find从向量中选择一个项目

时间:2012-11-12 18:49:38

标签: c++ syntax if-statement vector find

我遇到了使基于矢量的库存系统工作的问题。我能够列出清单中的项目,但不能允许访问用户选择的项目。这是代码:

struct aItem
{
    string  itemName;
    int     damage;

    bool operator==(aItem other)
    {
        if (itemName == other.itemName)
            return true;
        else
            return false;
    }
};

int main()
{
    int selection = 0;


    aItem healingPotion;
    healingPotion.itemName = "Healing Potion";
    healingPotion.damage= 6;

    aItem fireballPotion;
    fireballPotion.itemName = "Potion of Fiery Balls";
    fireballPotion.damage = -2;

    aItem testPotion;
    testPotion.itemName = "I R NOT HERE";
    testPotion.damage = 9001;
    int choice = 0;
    vector<aItem> inventory;
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(fireballPotion);

    cout << "This is a test game to use inventory items. Woo!" << endl;
    cout << "You're an injured fighter in a fight- real original, I know." << endl;
    cout << "1) Use an Item. 2) ...USE AN ITEM." << endl;

switch (selection)
    {
    case 1:
        cout << "Which item would you like to use?" << endl;
        int a = 1;
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {

            cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

        cin >> choice;

^^^^ 这条线以上的一切都有效。我假设我的问题是if语句,但是我无法弄清楚我的语法在哪里出错,或者是否有更好的方法来做我正在做的事情。

        if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
            cout << "You used a healing potion!";
        else
            cout << "FIERY BALLS OF JOY!";
        break;

    case 2:
        cout << "Such a jerk, you are." << endl;
            break;

    }
编辑:我想我没有正确地代表这一点。我需要玩家的选择才能影响显示的信息。以下是第1段摘录的示例输出:

Item 1: Healing Potion
Item 2: Healing Potion
Item 3: Healing Potion
Item 4: Potion of Fiery Balls

MAKE YOUR CHOICE. 
Choice: 

从那里,玩家可以输入1-4,我想要的是数字(减1,以反映从零开始的向量)传递给find,然后确定(在这个小的例如)如果库存[choice - 1]中的物品是治疗药水。如果是这样,请显示“你使用了治疗药水!”如果不是,则展示“欢乐的火球”。

3 个答案:

答案 0 :(得分:2)

三个问题。

一,您的运营商应声明为:

bool operator==(const aItem& other) const

两,在此代码中:

find(inventory.begin(), inventory.at(choice), healingPotion) != inventory.end())

您无法从begin()end()搜索整个向量 - 您只需从begin()搜索到at(choice) {{1} }指向搜索集的一端。所以你要么应该这样做:

at(choice)

或者这......

find(&inventory.at(0), &inventory.at(choice), healingPotion) != &inventory.at(choice))

编辑三,您正在尝试将苹果与橙子进行比较。您正在搜索find(inventory.begin(), inventory.end(), healingPotion.itemName) != inventory.end()) vector个对象以查找匹配的aItem对象,但您发送给aItem的参数不是find个对象,它是aItem数据成员之一。

您应该搜索匹配的项目,如下所示:

aItem

在C ++ 03中,您可以提供一个仿函数:

find( inventory.begin(), inventory.end(), healingPotion ) != inventory.end() )
                                            ^^^^^^^^

...然后使用#include <functional> struct match_name : public std::unary_function<aItem, bool> { match_name(const string& test) : test_(test) {} bool operator()(const aItem& rhs) const { return rhs.itemName == test_; } private: std::string test_; }; 搜索匹配:

find_if

在C ++ 11中,您可以使用闭包简化此混乱:

find_if( inventory.begin(), inventory.end(), match_name(healingPotion.itemName) ) // ...

答案 1 :(得分:0)

要添加John Dibling的答案,最后一部分是你要找的是名字,而不是aItem。

所以它要么是:

find(inventory.begin(), inventory.end(), healingPotion) != inventory.end();

其中operator ==定义为:

bool operator==(const aItem& other) const
{
   return itemName == other.itemName;
}

或者你需要让你的算子==取一个字符串:

find(inventory.begin(), inventory.end(), healingPotion.itemName) != inventory.end();

其中operator ==定义为:

bool operator==(const std::string& name) const
{
   return itemName == name;
}

答案 2 :(得分:0)

而不是:

case 1:
        cout << "Which item would you like to use?" << endl;
        int a = 1;
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {

            cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

        cin >> choice;
        if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
            cout << "You used a healing potion!";
        else
            cout << "FIERY BALLS OF JOY!";
        break;

    case 2:
        cout << "Such a jerk, you are." << endl;
            break;

    }

我没有意识到矢量的奇迹之一就是直接获取价值的能力--Ryan Guthrie在他的评论中提到了这一点,但我找到了一个更简单的答案&#34;。即:

case 1:
    cout << "Which item would you like to use?" << endl;
    //TODO: Learn what the hell the following line actually means.
    for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
    {
        //Makes a numerical list.
        cout << "Item " << index + 1 << ": " <<  inventory[index].itemName << endl;
        a+= 1;
    }
    cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

    cin >> choice;
    //Cannot define this outside of the statement, or it'll initialize to -1
    invVecPos = (choice - 1);

    //This checks for an invalid response. TODO: Add in non-int checks. 
    if ((invVecPos) >= inventory.size())
    {
        cout << "Choice out of bounds. Stop being a dick." << endl;
    }
    //If the choice is valid, proceed.
    else
    {
        //checking for a certain item type.
        if(inventory[invVecPos].itemType == "ITEM_HEALTHPOT")
        {
            cout << "You used a healing potion!" << endl;
            //this erases the potion, and automagically moves everything up a tick.
            inventory.erase (inventory.begin() + (invVecPos));
        }

        else if(inventory[invVecPos].itemType == "ITEM_FIREPOT")
        {
            cout << "FIERY BALLS OF JOY!" << endl;
        }

        else
        {
            //error-handling! Whee!
            cout << "Invalid Item type" << endl;
        }
    }


    break;
case 2:
    cout << "Why do you have to be so difficult? Pick 1!" << endl;
    break;

谢谢你,Ryan-在你的刺激下,我能够到别处找到我需要的代码! &#34;固定&#34;代码被大量评论,因此遇到问题的任何人都应该能够收集他们需要的内容!