stl::find_if 与用户搜索
我想知道是否有一种方法可以使用 stl::find_if 来搜索用户输入的值,
我不知道如何在不使用任何不良约定(全局)或添加扩展代码负载的情况下做到这一点。
例如,如果用户输入 10 的 int x,那么我想搜索一个整数向量
iterator = find_if(begin,end,pred) //but how does pred know the user inputted value?
I was wondering if there was a way to use the stl::find_if to search for a user inputted value
I don't know to do that without using any bad conventions(globals) or adding loads of extended code.
For example, if a user inputs a int x for 10, then I want to search an vector of ints
iterator = find_if(begin,end,pred) //but how does pred know the user inputted value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
equal_to
:You can use
equal_to
:pred
必须是具有重载 () 运算符的类型的实例,因此可以像函数一样调用它。(为了简洁起见,这里使用
struct
)编写这样的自定义类(带有“大量”代码!)至少可以说很麻烦,但在 C++0x 中使用 lambda 语法时会得到很大改进已添加。
The
pred
must be an instance of a type that has the overloaded () operator, so it can be called like a function.(Using a
struct
for brevity here)Writing custom classes like this (with "loads" of code!) is cumbersome to say the least, but will be improved a lot in C++0x when lambda syntax is added.
您可以使用 boost::bind 来获得更通用的解决方案,例如:
将找到一个 x 等于 xValue 的点
You can use boost::bind, for more general solution, for example:
will find a point whose x equals xValue