搜索对象矢量?

时间:2017-03-12 16:03:21

标签: c++ vector find

我想要一种搜索​​我的矢量的方法。从用户输入中查找匹配的字符串。然后使用第二个代码块输出。

这是我的代码:

void Ingredient::database(Ingredient &object){

    Ingredient item1("White Cap", "Weakness to Frost", "Fortify Heavy Armor", "Restore Magicka", "Ravage Magicka");

    Ingredient item2("Wisp Wrappings", "Restore Stamina", "Fortify Destruction", "Fortify Carry Weight", "Resist Magic");

    Ingredient item3("Yellow Mountain Flower", "Resist Poison", "Fortify Restoration", "Fortify Health", "Damage Stamina Regen");


    std::vector< Ingredient* > satchel;

    satchel.push_back(&item1);  

    satchel.push_back(&item2);

    satchel.push_back(&item3);


}

这是我希望显示的输出:

void Ingredient::displayItems(){

    std::cout << '\n';

    std::cout << "Ingredient: " << iName << '\n';

    std::cout << "Primary Effect: " << pAttr << '\n';

    std::cout << "Secondary Effect: " << sAttr << '\n';

    std::cout << "Tertiary Effect: " << tAttr << '\n';

    std::cout << "Quaternary Effect: " << qAttr << '\n';
}

变量

Ingredient::Ingredient(){iName = "", pAttr = "", sAttr = "", tAttr = "", qAttr = "";}

Ingredient::Ingredient(std::string name, std::string first, std::string second, std::string third, std::string fourth)
{

                     iName = name;
                     pAttr = first;
                     sAttr = second;
                     tAttr = third;
                     qAttr = fourth;
}

class Ingredient{

    public:
        Ingredient();
        Ingredient(std::string name, std::string first, std::string second, std::string third, std::string fourth);
        void displayItems();
        void database(Ingredient &object);

    private:
        std::string iName, pAttr, sAttr, tAttr, qAttr;
};

1 个答案:

答案 0 :(得分:0)

一些事情。你可能不希望你的成分数据库在成分类中(似乎很奇怪,一个对象将包含一个自身副本的数据库..通常,显然有时候这是有道理的)。此外,如果您需要保留指针的副本,您至少需要unique_ptr,但在这种情况下,对象本身的副本将会执行(此处不显示继承,否则您可能需要使用指向基类对象的指针)。

这是我在ideone上编译的一个例子,只按成分名称实现了矢量搜索,但如果需要,你应该可以将其推广到其他字段。至少它显示了你想要做的一个例子。

https://ideone.com/YjVytV

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Ingredient
{
public:
    Ingredient() = default;
    Ingredient(std::string name, std::string first, std::string second, std::string third, std::string fourth) :
        iName(std::move(name)),
        pAttr(std::move(first)),
        sAttr(std::move(second)),
        tAttr(std::move(third)),
        qAttr(std::move(fourth))
    {
    }

    const std::string& getName() const
    {
        return iName;
    }

    void displayIngredient() const
    {
        std::cout << '\n';
        std::cout << "Ingredient: " << iName << '\n';
        std::cout << "Primary Effect: " << pAttr << '\n';
        std::cout << "Secondary Effect: " << sAttr << '\n';
        std::cout << "Tertiary Effect: " << tAttr << '\n';
        std::cout << "Quaternary Effect: " << qAttr << '\n';
    }

private:
    std::string iName;
    std::string pAttr;
    std::string sAttr;
    std::string tAttr;
    std::string qAttr;
};

int main()
{
    std::vector<Ingredient> satchel;
    satchel.reserve(3);
    satchel.emplace_back("White Cap", "Weakness to Frost", "Fortify Heavy Armor", "Restore Magicka", "Ravage Magicka");  
    satchel.emplace_back("Wisp Wrappings", "Restore Stamina", "Fortify Destruction", "Fortify Carry Weight", "Resist Magic");
    satchel.emplace_back("Yellow Mountain Flower", "Resist Poison", "Fortify Restoration", "Fortify Health", "Damage Stamina Regen");

    const auto ingredientFoundByName = std::find_if(satchel.begin(), satchel.end(),
                                                    [&](const Ingredient& currIngredient)
                                                    {
                                                        return currIngredient.getName() == "Yellow Mountain Flower";
                                                    });

    if (ingredientFoundByName != satchel.end())
    {
        ingredientFoundByName->displayIngredient();
    }
    else
    {
        std::cout << "Could not find ingredient\n";
    }

    return EXIT_SUCCESS;
}

输出:

Ingredient: Yellow Mountain Flower
Primary Effect: Resist Poison
Secondary Effect: Fortify Restoration
Tertiary Effect: Fortify Health
Quaternary Effect: Damage Stamina Regen
相关问题