To me, this doesn't produce the expected results:
int main() {
int i[12] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (auto v : i)
std::cout << v << std::endl;
for (auto v : i)
v = v+1;
for (auto v : i)
std::cout << v << std::endl;
return 0;
}
The second for loop doesn't seem to do anything. Are range-based for loops read-only, or am I missing something?
In your second loop,
auto v : i,vis a copy of eachi.To change the
ivalues, you need a reference:Demo: https://ideone.com/DVQllH.