检索和编辑向量中对象的私有成员

发布于 2024-11-03 19:59:22 字数 1207 浏览 2 评论 0原文

我有一些代码将一些对象添加到向量中。然后,我希望从向量中检索特定对象,并能够写出和编辑其私有成员变量。

这是我当前拥有的代码:

class Product {
public:
    Product(int n, const string& na, int p)
            : number(n), name(na), price(p) {};
    void info() const;
private:
    string name;
    int number, price;
};

成员函数如下所示:

void Product::info() const {
    cout << number << ". " << name << " " price << endl;
}

然后我创建一个向量并向其中推入一些对象,如下所示:

vector<Product> range;
range.push_back(Product(1, "Bagpipe", 25));

要检索并列出有关所有对象的信息,我有以下函数:

void listProducts (const vector<Product>& range1) {
    for_each (range1.begin(), range1.end(), mem_fun_ref(&Product::info));
}

但这就是我被卡住了。

归结为我的问题:我不知道如何从向量中检索单个对象并编辑它们。我需要能够在向量中搜索包含特定数字或名称的对象,并且能够检索有关其所有私有成员的信息,并且还能够编辑所有成员。

到目前为止我对解决方案的想法是:

  • 创建额外的成员函数 可以返回单个成员

  • 创​​建函数,该函数与我上面已有的函数类似,可以搜索向量中的每个对象,并使用这些附加成员函数的返回值与我正在寻找的内容进行比较

  • 我不太清楚知道我将如何编辑对象的私有成员,但我目前的猜测是我也需要为此的成员函数,以及与这些函数相关的函数

任何帮助将不胜感激!甚至模糊地推动正确的方向!

I have some code which adds a few objects to a vector. I then wish to retrieve a specific object from the vector and be able to both write out and edit its private member variables.

This is the code I currently have:

class Product {
public:
    Product(int n, const string& na, int p)
            : number(n), name(na), price(p) {};
    void info() const;
private:
    string name;
    int number, price;
};

The member function looks like this:

void Product::info() const {
    cout << number << ". " << name << " " price << endl;
}

I then create a vector and push into it some objects, like so:

vector<Product> range;
range.push_back(Product(1, "Bagpipe", 25));

To retrieve and list the information about all objects, I have the following function:

void listProducts (const vector<Product>& range1) {
    for_each (range1.begin(), range1.end(), mem_fun_ref(&Product::info));
}

But this is where I get stuck.

To boil my problem down: I have no idea how to retrieve individual objects from the vector and edit them. I need to be able to search my vector for objects containing a specific number or name, and be able to retrieve either the information about all its private members, and also be able to edit all members.

Ideas I have about solutions thusfar are:

  • to create additional member functions
    that can return individual members

  • to create functions that, similar to the function I already have above, can search through each object in the vector and use the return from these additional member functions to compare with what I'm looking for

  • I don't quite know how I would go about editing an objects private members, but my current guess is that I would need members functions for this as well, and functions that tie in to those

Any help would be greatly appreciated! Even vague nudges in the right direction!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上芳菲 2024-11-10 19:59:22

如果成员变量是私有的,那么根据定义,您无法从外部世界访问它们!您需要执行以下操作之一:

  • 将它们更改为public
  • 访问器函数添加到类中(即int MyClass::getFoo() constvoid MyClass::setFoo(int))。
  • 让您的函数成为该类的友元

这与存储在向量中无关。

If the member variables are private, then by definition, you cannot access them from the outside world! You will need to do one of the following:

  • Change them to public.
  • Add accessor functions to the class (i.e. int MyClass::getFoo() const and void MyClass::setFoo(int)).
  • Make your function a friend of the class.

This is nothing to do with being stored in a vector.

我是有多爱你 2024-11-10 19:59:22

您可以使用 std:find_if 根据您希望的任何条件在向量中查找项目。

然后,您可以向 Product 添加公共接口,以允许您根据需要更新其状态。请注意,此接口不必是直接“将此项目设置为此值”映射。

You can use std:find_if to find items in a vector based on any criteria you wish.

Then you can add a public interface to Product to allow you to update its state as needed. Note that this interface need not be a direct "set this item to this value" mapping.

耳钉梦 2024-11-10 19:59:22

Boost.MultiIndex,每个可搜索的索引都有一个字段可能比使用带有您自己的搜索代码的向量更容易。

Boost 多索引容器
库提供了类模板
命名为 multi_index_container 其中
实现容器的建造
维护一个或多个索引
不同的排序和访问
语义。指数提供接口
与STL容器类似,
使使用它们变得熟悉。这
多重索引的概念
相同的元素集合是
从关系数据库借用
术语并允许
复杂数据规范
本着乘法精神的结构
索引关系表很简单
集和地图还不够。宽
提供了索引的选择,
以类似的 STL 容器为模型
像 std::set、std::list 和 hashed
套。

我还在其他答案中回应了有关私人成员访问的评论。

Boost.MultiIndex with an index for each searchable field might be easier than using a vector with your own search code.

The Boost Multi-index Containers
Library provides a class template
named multi_index_container which
enables the construction of containers
maintaining one or more indices with
different sorting and access
semantics. Indices provide interfaces
similar to those of STL containers,
making using them familiar. The
concept of multi-indexing over the
same collection of elements is
borrowed from relational database
terminology and allows for the
specification of complex data
structures in the spirit of multiply
indexed relational tables where simple
sets and maps are not enough. A wide
selection of indices is provided,
modeled after analogous STL containers
like std::set, std::list and hashed
sets.

I also echo comments about private member access in other answers.

掩饰不了的爱 2024-11-10 19:59:22

您需要为 Product 类提供允许更改 Product 属性的成员函数,或者更好地将向量中的 Product 对象替换为编辑现有对象的结果,但要做到这一点,您需要提供访问器函数,或者合适的Product 类中的构造函数。

You need to either provide member functions for your Product class that allow the Product properties to be changed OR BETTER replace the Product object in the vector with the results of editing an existing one, but to do that you need to provide accessor functions, or suitable constructors in your Product class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文