久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我應該使用 std::for_each 嗎?

Should I use std::for_each?(我應該使用 std::for_each 嗎?)
本文介紹了我應該使用 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) 是一個 lambda 函數).而不是:

(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 range-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 循環,您必須閱讀循環的整個主體才能知道它的作用.它可能有 continuebreakreturn 語句來改變控制流.它可能有修改迭代器或索引變量的語句.如果不檢查整個循環,就無法知道.

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 表達式的優勢 在最近向西北 C++ 用戶組進行的演示中.

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, "
"));

這篇關于我應該使用 std::for_each 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 欧美一区2区三区4区公司二百 | 亚洲一视频 | 国产真实精品久久二三区 | 99亚洲精品 | 中文字幕丁香5月 | 亚洲免费片 | 久www| 男女羞羞视频在线看 | 国产成人一区二区三区精 | 成人精品在线视频 | 日韩中文字幕在线观看视频 | 精品电影 | 亚洲欧美视频一区 | 天天夜夜操 | www日本高清视频 | 瑞克和莫蒂第五季在线观看 | 国产免费一区二区三区 | 热99在线 | 欧美精品一区二区免费视频 | 国产电影一区二区 | 天天干夜夜操 | 免费av观看| 亚洲成人自拍 | 插插插干干干 | a级大片免费观看 | 久久精品一区二区三区四区 | 蜜臀91视频 | 日本一区二区不卡 | 欧美日韩国产在线观看 | 91传媒在线观看 | 亚洲欧美激情精品一区二区 | 日韩欧美高清 | 玖玖在线精品 | 成人在线视频免费播放 | 麻豆视频国产在线观看 | 男人天堂色 | 久久国产精品免费 | 中文字幕国产视频 | 亚洲精品成人 | 中文字幕综合 | avtt国产 |