我应该使用std :: for_each? [英] Should I use std::for_each?

查看:136
本文介绍了我应该使用std :: for_each?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是试图学习更多关于我使用的语言(不同的风格,框架,模式等)。我注意到,我从未使用过 std :: for_each 所以我想我也许应该开始。在这种情况下的目标是扩大我的心,以某种程度(可读性,表现力,紧凑性等)来改进代码。

I'm always trying to learn more about the languages I use (different styles, frameworks, patterns, etc). I've noticed that I never use std::for_each so I thought that perhaps I should start. The goal in such cases is to expand my mind and not to improve the code in some measure (readability, expressiveness, compactness, etc).

因此,考虑到这种情况,使用 std :: for_each 进行简单任务,例如打印一个向量是一个好主意:

So with that context in mind, is a good idea to use std::for_each for simple tasks like, say, printing out a vector:

for_each(v.begin(), v.end(), [](int n) { cout << n << endl; }

[](int n)

(The [](int n) being a lambda function). Instead of:

for(int i=0; i<v.size(); i++) { cout << v[i] << endl; }

问题似乎没有意义,我想它几乎要问一个更大的问题...如果一个中间程序员使用语言功能,即使他不需要在这个时候,但只是让他可以理解的功能更好的一个时间,可能实际上从中受益很大。虽然这个更大的问题可能已经被问到(例如此处)。

I hope this question doesn't seem pointless. I guess it almost asks a larger question... should an intermediate programmer use a language feature even though he doesn't really need to at this time but just so that he can understand the feature better for a time that may actually greatly benefit from it. Although this larger question has probably already been asked (e.g. here).

推荐答案

使用 std :: for_each 旧学校 for 循环(或者甚至是新的C ++ 0x范围 - for 循环):你可以看看

There is an advantage to using std::for_each instead of an old school for loop (or even the newfangled C++0x range-for loop): you can look at the first word of the statement and you know exactly what the statement does.

当你看到 for_each 时,你知道这个语句的第一个单词对于范围中的每个元素(假设没有抛出异常),对lambda执行一次操作。在每个元素被处理之前,不可能提前断开循环,并且不可能多次跳过元素或评估循环的主体一次。

When you see the for_each, you know that the operation in the lambda is performed exactly once for each element in the range (assuming no exceptions are thrown). It isn't possible to break out of the loop early before every element has been processed and it isn't possible to skip elements or evaluate the body of the loop for one element multiple times.

使用 for 循环,您必须读取循环的整个主体以了解它的作用。它可能有继续 break return 它改变了控制流。它可以具有修改迭代器或索引变量的语句。没有办法知道没有检查整个循环。

With the for loop, you have to read the entire body of the loop to know what it does. It may have continue, break, or return statements in it that alter the control flow. It may have statements that modify the iterator or index variable(s). There is no way to know without examining the entire loop.

Herb Sutter讨论了使用算法和lambda表达式的优点

Herb Sutter discussed the advantages of using algorithms and lambda expressions in a recent presentation to the Northwest C++ Users Group.

注意,你实际上可以使用 std :: copy 算法如果你愿意:

Note that you can actually use the std::copy algorithm here if you'd prefer:

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));

这篇关于我应该使用std :: for_each?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆