ES.71: 如果可以,使用范围for语句代替普通的for语句。
Readability. Error prevention. Efficiency.
可读性,防错和效率。
Example(示例)
for (gsl::index i = 0; i < v.size(); ++i) // bad
cout << v[i] << '\n';
for (auto p = v.begin(); p != v.end(); ++p) // bad
cout << *p << '\n';
for (auto& x : v) // OK
cout << x << '\n';
for (gsl::index i = 1; i < v.size(); ++i) // touches two elements: can't be a range-for
cout << v[i] + v[i - 1] << '\n';
for (gsl::index i = 0; i < v.size(); ++i) // possible side effect: can't be a range-for
cout << f(v, &v[i]) << '\n';
for (gsl::index i = 0; i < v.size(); ++i) { // body messes with loop variable: can't be a range-for
if (i % 2 == 0)
continue; // skip even elements
else
cout << v[i] << '\n';
}
A human or a good static analyzer may determine that there really isn't a side effect on v in f(v, &v[i]) so that the loop can be rewritten.
"Messing with the loop variable" in the body of a loop is typically best avoided.
程序员或者好的静态分析软件或许可以判断f(v,&v[i])中的v实际上并不存在副作用,因此该循环可以被重写。通常情况下,最好避免在循环体中“乱用循环变量”。
Note(注意)
Don't use expensive copies of the loop variable of a range-for loop:
不要在循环体中进行代价高昂的循环变量拷贝。
for (string s : vs) // ...
This will copy each elements of vs into s. Better:
这会导致vs的每个元素都被拷贝。较好的做法是:
for (string& s : vs) // ...
Better still, if the loop variable isn't modified or copied:
如果循环变量不会被修改或拷贝,下面的做法更好。
for (const string& s : vs) // ...
Look at loops, if a traditional loop just looks at each element of a sequence, and there are no side effects on what it does with the elements, rewrite the loop to a ranged-for loop.
检查循环代码,如果一个传统的循环只是按照顺序读取每个元素,而且对元素的操作不存在副作用,使用范围for语句重写循环代码。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es71-prefer-a-range-for-statement-to-a-for-statement-when-there-is-a-choice