日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【转】foreach for each for_each引发的探讨:c++世界中的循环语句

發布時間:2023/12/10 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】foreach for each for_each引发的探讨:c++世界中的循环语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:foreach for each for_each引發的探討:c++世界中的循環語句_w_419675647的博客-CSDN博客

一 背景:

代碼中看到?for each,注意,兩個單詞中間沒有下劃線,有同事問這個是不是和?for_each一樣?和foreach呢?我回答應該一樣,但是內心很不安,尤其是作為一個c++的多年用戶。

二 資料收集整理:

1 首先來看看我們最熟悉的 for_each。

他的全名是 std::for_each,來源c++的stl。頭文件<algorithm>.
當時是個模板函數了
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
標準庫中的例子:

// for_each example #include <iostream> // std::cout #include <algorithm> // std::for_each #include <vector> // std::vectorvoid myfunction (int i) { // function:std::cout << ' ' << i; }struct myclass { // function object type:void operator() (int i) {std::cout << ' ' << i;} } myobject;int main () {std::vector<int> myvector;myvector.push_back(10);myvector.push_back(20);myvector.push_back(30);std::cout << "myvector contains:";for_each (myvector.begin(), myvector.end(), myfunction);std::cout << '\n';// or:std::cout << "myvector contains:";for_each (myvector.begin(), myvector.end(), myobject);std::cout << '\n';return 0; }

2 再來看看foreach。

搜索引擎里可以搜到很多相關標題,但是點進去后會發現,幾乎都是在講 for_each。僅有的幾篇異同點其實是個錯誤的宏定義。
后來試著在QT的幫助文檔里找了下,果然找到了。
他是: - Global Qt Declarations
foreach(variable, container)
This macro is used to implement Qt’s foreach loop. The variable parameter is a variable name or variable definition; the container parameter is a Qt container whose value type corresponds to the type of the variable. See The foreach Keyword for details.
Note: Since Qt 5.7, the use of this macro is discouraged. It will be removed in a future version of Qt. Please use C++11 range-for, possibly with qAsConst(), as needed.
See also qAsConst().
大致的翻譯:從Qt 5.7開始,不建議使用此宏。 它將在Qt的將來版本中刪除。 請根據需要使用C ++ 11 range-for,可能與qAsConst()一起使用。
另請參見qAsConst()。

是的,不再建議使用。

3 最后找找for each。

真難找,好像沒有這個語法,但是代碼里確實有使用,代碼里的關鍵字是兩個分開的單詞,也無法到到對應的定義文件。 換到bing引擎,終于還是找到了。 《https://docs.microsoft.com/en-us/cpp/dotnet/for-each-in?view=vs-2019》 原來是微軟自己的定義。 Iterates through an array or collection. This non-standard keyword is available in both C++/CLI and native C++ projects. However, its use is not recommended. Consider using a standard Range-based for Statement (C++) instead. 翻譯:遍歷數組或集合。 此非標準關鍵字在C ++ / CLI和本機C ++項目中均可用。 **但是,不建議使用它。** 考慮改為使用標準的基于范圍的語句(C ++)。 是的,不建議使用了。

三 聊聊c++ 世界的循環語句

1 對容器中的每一個元素都調用函數的方法有以下:

第一種:基于迭代器

for (std::vector<int>::iterator it = ve.begin(); it < ve.end(); ++it)f(*it); for (std::vector<int>::const_iterator it = ve.cbegin(); it < ve.cend(); ++it)f(*it); for (std::vector<int>::iterator it = ve.begin(); it != ve.end(); ++it)f(*it); for (std::vector<int>::const_iterator it = ve.cbegin(); it != ve.cend(); ++it)f(*it);

第二種:基于下標

for (int i = 0; i != ve.size(); ++i)f(ve[i]); for (int i = 0; i < ve.size(); ++i)f(ve[i]); for (std::size_t i = 0; i != ve.size(); ++i)f(ve[i]); for (std::size_t i = 0; i < ve.size(); ++i)f(ve[i]); for (std::vector<int>::size_type i = 0; i != ve.size(); ++i)f(ve[i]); for (std::vector<int>::size_type i = 0; i < ve.size(); ++i)f(ve[i]);

第三種:stl模板函數

std::for_each(ve.begin(), ve.end(), f); std::for_each(ve.cbegin(), ve.cend(), f); std::for_each(std::begin(ve), std::end(ve), f); std::for_each(std::cbegin(ve), std::cend(ve), f); // C++14

第四種:基于范圍的循環

for (auto val : ve)f(val); for (auto &val : ve)f(val); for (const auto &val : ve)f(val);

2 分析:

第一種方法:書寫太復雜并不是所有容器的迭代器都支持小于操作容易誤寫成 it <= ve.end()
第二種方法:調用 ve.size()可能有效率的問題 調用 ve[i]可能有效率的問題 并不是所有的容器都支持下標操作下標的標準規范是無符號的數,所以使用int 是錯誤的標準并未說明下標具體用何種類型說明,所以使用std::size_t可能有可移植性的問題使用 std::vector::size_type正確,但書寫太復雜了容易誤寫為 i <= ve.size()
第三種方法:相比前兩種,不容易出錯相比前兩種,書寫也簡單多了如果在函數調用處寫函數的話還是比較復雜
第四種方法:相比前三種,不容易出錯相比前三種,書寫更簡單了直接在函數調用處寫函數也很簡單需要 C++11 或之后

總結

以上是生活随笔為你收集整理的【转】foreach for each for_each引发的探讨:c++世界中的循环语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。