访问向量向量中的类的成员
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);
};
这是我遇到麻烦的地方。 对于我的函数列表::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()
被调用该结果。
相关文章:
- 向量成员在管理类指针C++队列时丢失
- C++:更新类的向量成员变量
- C++ 入门 12.1:我可以在类中使用静态向量成员实现 StrBlob<string> 吗?
- 具有向量成员的基类<int>
- C++ 向量::使用类对象迭代器擦除不擦除向量成员
- 无法将类构造函数中新创建的对象复制到 C++ 中的向量成员
- 修改另一个类的向量成员时出错
- 返回对私有向量成员元素的非常量引用是否是一种不好的做法
- 如何在 C++ 中将结构类型向量数据输入到结构向量成员(嵌套结构向量)中
- std::向量成员变量 EXC_BAD_ACCESS
- 如何使用其定义附近的显式长度/值构造函数初始化向量成员变量
- "Empty"数组\向量成员 C++
- 如何使用初始值设定项列表初始化 2D 向量成员
- 如何在类定义中初始化向量成员变量
- 如何初始化结构的向量成员
- 使用向量<向量>成员实例化对象时的隔离错误<int>
- 为向量成员和更新元素指定默认值
- 保持类的向量成员与类实例连续
- C++:在类构造函数中填充向量后,向量成员被销毁
- 通过引用另一个类方法传递向量成员变量