stl算法课堂
有没有办法在对象容器中使用像 find() 和 find_if() 这样的 stl 算法? 例如:使用 find() 在 Alfhabetic 类向量中查找名称为“abc”的元素。
Is there a way to use stl algorithms like find() and find_if() in a container of objects?
Ex.:With find() find the element whit name "abc" in a vector of class Alfhabetic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义比较谓词(函子)。 这是一个通用实现:
在字母元素向量中,
您可以运行如下搜索:
You can define a comparing predicate (functor). Here is a generic implementation:
In a vector of Alphabetic elements
you can run a search like:
您可以为
Alfhabetic
类定义一个operator==()
,它仅与数据成员abc
匹配,如下所示:
然后查找
>Alfhabetic
实例使用abc
初始化为您想要的值。You can define an
operator==()
for classAlfhabetic
that matches only the data memberabc
something like that:
and then finding an
Alfhabetic
instance initialized withabc
as the value you want.