绑定的不是一个值,而是一个函数(获取该函数的值)
假设我有以下功能:
int foo (int a)
{
return something;
}
我怎样才能做这样的事情?
vector<int> v;
std::for_each( v.begin(), v.end(), std::bind1st(std::minus<int>(), foo) );
我希望它的工作方式如下: for_each
将当前元素传递到函子中,函子调用 foo
然后减去传递的向量元素和调用 foo
的结果>。是否可以仅使用 std 而不是 boost 或自己编写的函子来实现此目的?
Let's say I have following function:
int foo (int a)
{
return something;
}
How can I do something like this?
vector<int> v;
std::for_each( v.begin(), v.end(), std::bind1st(std::minus<int>(), foo) );
I want this to work like: for_each
passes current element into the functor, functor calls foo
and then subtracts the passed vector element and result of calling foo
. Is it possible to make this using only std, not boost or self-written functors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这有点可怕,但在 C++11 中……
使用 lambda 的方式不那么可怕
Well this is slightly horrible but in C++11...
less horrible way using lambdas
如果您不想修改该向量,那么我假设您想将其转换为另一个向量。
If you do not want to modify the vector, so I would assume you want to transform it to another one.