调用向量内对象的成员函数

发布于 2024-11-04 08:06:06 字数 506 浏览 0 评论 0原文

这是我之前关于检索和编辑向量中对象的私有成员

我有一个充满对象的向量,这些对象具有我需要访问的私有成员。 我已经创建了用于返回每个私有成员的访问器函数,现在正在寻找迭代对象向量的最佳方法,从中返回特定的私有成员并将其与给定变量进行比较,直到找到匹配项。< /strong>

我曾考虑过使用 find_if 但没有成功地为向量中的每个对象使用成员函数返回值作为标准。

我也无法使用 for_each 来完成这项工作。

我创建一个附加问题的原因是我已经在这个问题上停留的时间比我应该的要长,而且我找到问题的解决方案变得非常紧迫。任何朝着正确方向的推动将不胜感激!

This is a continuation on my previous question here regarding retrieving and editing private members of objects in a vector.

I have a vector full of objects that have private members that I need to access. I have created accessor functions for returning each private member and am now looking for the best way to iterate through the vector of objects, returning a specific private member from it and comparing it to a given variable until a match is found.

I have considered using find_if but have had no luck with using a member functions return value, for each object in the vector, as criteria.

Neither have I been able to use for_each to do the job.

The reason I am creating an additional question is that I've been dwelling for this longer than I should and it's getting to become quite urgent that I find a solution to the problem. Any nudge in the right direction would be greatly appreciated!

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

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

发布评论

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

评论(1

单挑你×的.吻 2024-11-11 08:06:06

创建一个函子:

struct CompareTo{
    CompareTo(const AnotherObject& aValue) : theValue(aValue){}

    bool operator()(const Object& anObject) const{
        return anObject.getMemberVar() == theValue;
    }

    const AnotherObject& theValue;
};

可以将其放入 find_if 中。

Create a functor:

struct CompareTo{
    CompareTo(const AnotherObject& aValue) : theValue(aValue){}

    bool operator()(const Object& anObject) const{
        return anObject.getMemberVar() == theValue;
    }

    const AnotherObject& theValue;
};

that can be put into the find_if.

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