简体   繁体   中英

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.

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 .

[0] gets the first item from that result.

.getName() is called on that result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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