对于带有结构的 std::list,使用 find_if 查找结构内部的 int

发布于 2024-09-28 14:29:22 字数 301 浏览 0 评论 0原文

如果列表包含结构,如何将 find_if 与 std::list 一起使用?我的第一次伪代码尝试如下所示:

typename std::list<Event>::iterator found = 
    find_if(cal.begin(), cal.last(), predicate); 

这里的问题是谓词在列表中不直接可见,而是在 event.object.return_number() 内部可见。我应该如何引用嵌套在结构内部并且需要 get 方法才能访问的 int 。

How can I use find_if with a std::list if the list contains structs? My first pseudo code attempt at this looks like this:

typename std::list<Event>::iterator found = 
    find_if(cal.begin(), cal.last(), predicate); 

The problem here is that the predicate is not directly visible in the list but inside event.object.return_number(). How am I suppose to refer to an int that is nested inside the struct and needs a get method to be accessed.

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

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

发布评论

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

评论(4

回首观望 2024-10-05 14:29:22

您可以使用函子类(它类似于函数,但允许您拥有状态,例如配置):

class Predicate
{
public:
    Predicate(int x) : x(x) {}
    bool operator() (const Cal &cal) const { return cal.getter() == x; }
private:
    const int x;
};

std::find_if(cal.begin(), cal.end(), Predicate(x));

You can use a functor class (which is like a function, but allows you to have state, such as configuration):

class Predicate
{
public:
    Predicate(int x) : x(x) {}
    bool operator() (const Cal &cal) const { return cal.getter() == x; }
private:
    const int x;
};

std::find_if(cal.begin(), cal.end(), Predicate(x));
饮惑 2024-10-05 14:29:22

在 C++0x 中(您的编译器可能已经部分实现了),您可以执行以下操作:

find_if(cal.begin(), cal.last(), [&](const Event& e) 
        { 
            return e.object.return_number() == value_to_find;
        });

In C++0x, which your compiler probably already partially implements, you can do the following:

find_if(cal.begin(), cal.last(), [&](const Event& e) 
        { 
            return e.object.return_number() == value_to_find;
        });
盗心人 2024-10-05 14:29:22

您可以像这样设置谓词:

struct IsEventObjectReturnNumber
{
   int num;
   explicit IsEventObjectReturnNumber( int n ) : num( n ) {}

   bool operator()(const Event & event ) const
   {
      return event.object.return_number() == num;
   }
};

std::list<Event>::iterator = std::find_if(cal.begin(), cal.end(), IsEventObjectReturnNumber(x));

You set up your predicate something like this:

struct IsEventObjectReturnNumber
{
   int num;
   explicit IsEventObjectReturnNumber( int n ) : num( n ) {}

   bool operator()(const Event & event ) const
   {
      return event.object.return_number() == num;
   }
};

std::list<Event>::iterator = std::find_if(cal.begin(), cal.end(), IsEventObjectReturnNumber(x));
落花随流水 2024-10-05 14:29:22

(不是那么简单,但是)最简单的方法(在没有 C++11 的情况下)是自定义比较器:

struct CompareMyStruct {
    int n_;
    CompareMyStruct(int n) : n_(n) { }
    bool operator()(const Event& a) const {
        return a.object.return_number() == n_;
    }
};

typename std::list<Event>::iterator found =
    find_if(cal.begin(), cal.last(), CompareMyStruct(123));

The (not so simple, but) simplest way (in the absence of C++11) is a custom comparator:

struct CompareMyStruct {
    int n_;
    CompareMyStruct(int n) : n_(n) { }
    bool operator()(const Event& a) const {
        return a.object.return_number() == n_;
    }
};

typename std::list<Event>::iterator found =
    find_if(cal.begin(), cal.last(), CompareMyStruct(123));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文