使用for_each循环有什么好处? [英] What's the advantage of using for_each loop?
问题描述
使用for_each比使用
进行循环的正常迭代有什么好处?
是否有任何调整使用将对象作为''函数'参数传递给
for_each循环。似乎复制功能也可以这样做。
What''s the advantage of using for_each than the normal iteration using
for loop?
Is there any ''tweak'' to use stream objects as ''Function'' parameter of
for_each loop. Seems copy function can do the same.
推荐答案
使用for_each比使用$ b $的正常迭代有什么好处? b
What''s the advantage of using for_each than the normal iteration using
for loop?
for loop?
一般来说,for_each应该比for循环更简洁。
关于for_each的一个很酷的事情是:
1)你可以通过引用传递函数对象
2)你可以对函数对象应用副作用
3)你可以使用for_each()算法的返回值
In general for_each is supposed to be less verbose than for-loop.
The cool thing about for_each is:
1) you can pass the function objects by reference
2) you can apply side-effects on the function object
3) you can use the return value of the for_each() algorithm
是否有任何''tweak''使用流对象作为''Function''参数
for_each循环。似乎复制功能也可以这样做。
Is there any ''tweak'' to use stream objects as ''Function'' parameter of
for_each loop. Seems copy function can do the same.
升级库怎么样?
http://www.boost.org/
使用std :: copy,您可以执行以下操作:
copy(a.begin(),a.end(),ostream_iterator< string>(cout,"");
和boost :: lambda你可以做同样的事情:
for_each(a.begin(),a.end(),std :: cout<< _1<<'''');
How about the boost library?
http://www.boost.org/
With std::copy you can do something like:
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " ");
and with boost::lambda you can do the same thing:
for_each(a.begin(), a.end(), std::cout << _1 << '' '');
Sarath< CS ***** @ gmail.comwrote:
Sarath <CS*****@gmail.comwrote:
有什么好处使用for_each比普通迭代使用
for循环?
What''s the advantage of using for_each than the normal iteration using
for loop?
我开始认为这应该是一个FAQ。: - )
标准库算法的一个主要好处是它们可以节省程序员编写显式循环的价值。循环可能很乏味,而且b / b很容易出错。 for_each()算法是
中最简单的算法,它除了消除显式循环之外什么都不做......一个
for_each()的常用用法是提取来自
序列元素的信息。 - Stroustrup
I''m beginning to think this should be an FAQ. :-)
A key benefit of the standard library algorithms is that they save
the programmer from writing explicit loops. Loops can be tedious and
error-prone. The for_each() algorithm is the simplest algorithm in
the sense that it does nothing but eliminate an explicit loop... One
common use of for_each() is to extract information from elements of a
sequence. -- Stroustrup
是否有任何''tweak''将流对象用作
for_each循环的''Function''参数。似乎复制功能也可以这样做。
Is there any ''tweak'' to use stream objects as ''Function'' parameter of
for_each loop. Seems copy function can do the same.
显然,如果更专业的算法有效,那就改用它。
Obviously, if a more specialized algorithm works, then use it instead.
5月30日,10日:上午33点,vrmari ... @ gmail.com写道:
On May 30, 10:33 am, v.r.mari...@gmail.com wrote:
使用for_each的优势是什么?正常迭代使用
for循环?
What''s the advantage of using for_each than the normal iteration using
for loop?
一般来说,for_each应该比for循环更简洁。
关于for_each的一个很酷的事情是:
1)你可以通过引用传递函数对象
2)你可以对函数对象应用副作用
3)你可以使用for_each()算法的返回值
In general for_each is supposed to be less verbose than for-loop.
The cool thing about for_each is:
1) you can pass the function objects by reference
2) you can apply side-effects on the function object
3) you can use the return value of the for_each() algorithm
是否有任何''tweak''使用流对象作为''Function''参数
for_each循环。似乎复制功能也可以这样做。
Is there any ''tweak'' to use stream objects as ''Function'' parameter of
for_each loop. Seems copy function can do the same.
升级库怎么样? http ://www.boost.org/
使用std :: copy,您可以执行以下操作:
copy( a.begin(),a.end(),ostream_iterator< string>(cout,"");
和boost :: lambda你可以做同样的事情:
for_each(a.begin(),a.end(),std :: cout<< _1<<'''');
How about the boost library?http://www.boost.org/
With std::copy you can do something like:
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " ");
and with boost::lambda you can do the same thing:
for_each(a.begin(), a.end(), std::cout << _1 << '' '');
是的,我使用了boost :: lambda来做同样的事情。但是用ISO C ++ STL实现不可能用
Yea I used boost::lambda to do the same. But it''s not possible to do
with ISO C++ STL implementation
这篇关于使用for_each循环有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!