#why isnt it possible? why not you stupid bastard??
24 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
why cant i insert with range based loop?
!f
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<int> v(n);
for (int i : v)
std::cin >> i;
for (int i : v)
std::cout << i;
return 0;
}
for (int i : v)
std::cin >> i;
```Sets the local `i` variable, not anything in the vector
Use a reference
ohhhh
same way functions do?
for (int &i : v)
std::cin >> i;```
aaand, as i understand, this is better as well?
for (const int &i : v)
std::cout << i;```
For the second loop it doesn't mater how you do it
:(
but why
as i see it, you dont create temporary copy of variable, access it directly, additionally, its const (which makes it faster (??) )
That's micro-optimization really
References aren't magic, they're implemented as pointers under the hood, which is often something to avoid actually. But the optimizer will optimize it away in this case
So reference, const reference, or no reference won't make any difference performance wise
const won't make things faster, just prevents you from modifying