非常簡單,就是 for 循環的精簡寫法
1 2 3int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;
對於容器同樣可用,比如與 auto 共用時
1 2 3 4 5 6std::map<std::string, std::string> mm;
for( auto i : mm )
{
if ( (*i).first.size() < 3 )
(*i).second.append(" ");
}
可用的容器有<array>, <deque>, <forward_list>, <list>, <map>, <pair>, <regex>, <set>, <string>, <tuple>, <unordered_map>, <unordered_set>,<valarray>,<vector>
for (for-range-declaration:expression)statement
等效於
12345678{auto && __range = ( expression );for(auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ){for-range-declaration = *__begin;statement}}