访问向量向量中的类的成员

Accessing members of a class within a vector of vectors

本文关键字:向量 成员 访问      更新时间:2023-10-16
class item
{
private:
    std::string name;
    double price;
    int quantity;
public:
    void item();
    void setName(string itemName);
    std::string getName();
    void setPrice(double itemPrice);
    double getPrice();
    void setQuantity(int itemQuantity);
    int getQuantity();
};
class list
{
private:
    std::vector<std::vector<item>> notepad;
public:
    bool isEmpty();
    void addList();
    void printLists(bool printTotalPrice);
    void addItem();
    void removeItem();
    void editItem();
    void importList(ifstream& iFile);
    void exportList(ofstream& oFile);
};

这是我遇到麻烦的地方。 对于我的函数列表::addItem(),我希望用户输入一个字符串,以便仅搜索记事本向量的第一行元素以找到匹配项。

像这样的东西...

          for (int i = 0; i < notepad.size(); ++i)
             if (user's entered string) == first element of 'i'th vector.getName() 

。找到匹配项

关于我如何做到这一点的任何想法?

if ( user_entered_string == notepad[i][0].getName() )

[i]notepad得到第 i 个向量。

[0]从该结果中获得了第一个item

.getName()被调用该结果。