本文介紹了C++中基于范圍的for循環是否會保留索引順序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在 c++11 中,如果我在向量上使用基于范圍的 for 循環,它會保證迭代順序嗎?
In c++11, if I use a range based for loop on vector, will it guarantee the iteration order?
即以下代碼塊是否保證產生相同的輸出?
i.e. are the following code blocks guaranteed to produce the same output?
vector<T> output;
vector<U> V;
for( auto v: V) output.push_back(f(v));
對比
for(int i =0; i < V.size(); ++i) output.push_back(f(V[i]));
如果不是vector
而是map
等怎么辦?
what if it is not vector
but map
, etc?
推薦答案
是的,這兩個代碼保證做同樣的事情.雖然我沒有標準的鏈接,你可以看看這里.我引用:您可以將其讀作對于 v 中的所有 x",從 v.begin() 開始并迭代到 v.end().
Yes the two codes are guaranteed to do the same. Though I don't have a link to the standard you can have a look here. I quote: You can read that as "for all x in v" going through starting with v.begin() and iterating to v.end().
這篇關于C++中基于范圍的for循環是否會保留索引順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!