是否可以使用具有独立输入参数的函数来使用STL for_each算法?

发布于 2024-10-07 16:01:34 字数 376 浏览 0 评论 0原文

目前,我正在运行一个 for 循环,在其中对 STL 容器中的每个元素进行调用,类似于以下内容。

void AddToUpdate(iterator iter, Update& Update) {...};

...

Update update;
for(iterator iter = container.begin(); iter != container.end(); ++iter)
    AddToUpdate(iter, update);

我正在研究 for_each STL 算法,因为它似乎适合我的需要。

我想知道,考虑到应用到容器的函数使用了第二个输入参数,是否可以重构它以使用标准 STL 算法而不使用成员变量或其他漏洞?

At the moment I am running a for loop where I make a call on each element in an STL container, similar to the following.

void AddToUpdate(iterator iter, Update& Update) {...};

...

Update update;
for(iterator iter = container.begin(); iter != container.end(); ++iter)
    AddToUpdate(iter, update);

I am looking at the for_each STL algorithm as it seems to fit my needs.

I was wondering if, given the use of a second input parameter to the function being applied to the container, whether it's possible to refactor this to use a standard STL algorithm without using member variables or other loopholes?

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

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

发布评论

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

评论(2

§普罗旺斯的薰衣草 2024-10-14 16:01:34

各种 std::bind1st/std::bind2ndBoost.bind 库是为了解决您的问题而创建的(这对于几乎所有使用 STL 算法的人来说都是常见的),但是通常它们看起来只是一种解决方法而不是解决方案。

幸运的是,随着即将推出的 C++ 标准,期待已久的 lambda 函数 的添加应该可以实现彻底解决问题。但是,请注意,由于 std::for_each 调用传递取消引用的迭代器(即正在考虑的实际值)的函子,因此您的 AddToUpdate 函数不应接受迭代器,但值。

在这种情况下,它会是这样的:

Update update;
std::foreach(container.begin(); container.end(); [ & update](TypeOfTheValue Value) {
    AddToUpdate(Value, update);
});

The various std::bind1st/std::bind2nd and the Boost.bind library were created to solve your problem (that is common to almost anyone who used the STL algorithms), but often they just seem a workaround instead of a solution.

Fortunately, with the upcoming C++ standard the long-awaited addition of lambda functions should solve definitively the problem. However, notice that, since std::for_each calls the functor passing the dereferenced iterator (i.e. the actual value being considered) your AddToUpdate function shouldn't accept the iterator, but the value.

In such case, it would be something like this:

Update update;
std::foreach(container.begin(); container.end(); [ & update](TypeOfTheValue Value) {
    AddToUpdate(Value, update);
});
野心澎湃 2024-10-14 16:01:34

您想要使用 std::bind2nd() - http: //www.cplusplus.com/reference/std/function/bind2nd/

基本上它从具有 2 个参数的函数返回一个一元函数对象,其中第二个参数是固定的。

使用 for_eachbind2nd 时,您的代码应如下所示:

Update update;
for_each(container.begin(), container.end(), bind2nd(ptr_fun(AddToUpdate), update));

编辑。 正如 Matteo 注意到 AddToUpdate 的第一个参数一样> 必须是容器中值的类型,而不是迭代器。

You want to use std::bind2nd() - http://www.cplusplus.com/reference/std/functional/bind2nd/

Basically it return a unary function object from function with 2 arguments, where the second argument is fixed.

This is how your code should look like with for_each and bind2nd:

Update update;
for_each(container.begin(), container.end(), bind2nd(ptr_fun(AddToUpdate), update));

Edit. As Matteo noticed the first argument of AddToUpdate has to be type of value in container, not an iterator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文