对于带有结构的 std::list,使用 find_if 查找结构内部的 int
如果列表包含结构,如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用函子类(它类似于函数,但允许您拥有状态,例如配置):
You can use a functor class (which is like a function, but allows you to have state, such as configuration):
在 C++0x 中(您的编译器可能已经部分实现了),您可以执行以下操作:
In C++0x, which your compiler probably already partially implements, you can do the following:
您可以像这样设置谓词:
You set up your predicate something like this:
(不是那么简单,但是)最简单的方法(在没有 C++11 的情况下)是自定义比较器:
The (not so simple, but) simplest way (in the absence of C++11) is a custom comparator: