简体   繁体   English

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

[英]Accessing members of a class within a vector of vectors

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);
};

Here is where I am having trouble. 这是我遇到麻烦的地方。 For my function list::addItem() I want the user to enter a string in order to search the notepad vector's first row elements ONLY in order to find a match. 对于我的函数list :: addItem(),我希望用户输入一个字符串,以便仅搜索记事本矢量的第一行元素以找到匹配项。

Something like this... 像这样

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

... match found ...找到匹配

Any ideas on how I could do this? 关于如何执行此操作的任何想法?

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

[i] gets the 'i'th vector from notepad . [i]notepad获取“ i”向量。

[0] gets the first item from that result. [0]从该结果中获得第一item

.getName() is called on that result. 在该结果上调用.getName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM