c++0x 学习笔记之 range-based for loop wording
非常简单,就是 for 循环的精简写法
123int
array[5] = { 1, 2, 3, 4, 5 };
for
(
int
& x : array)
x *= 2;
对于容器同样可用,比如与 auto 共用时
123456std::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
}
}
补充:软件开发 , C++ ,