#why isnt it possible? why not you stupid bastard??

24 messages · Page 1 of 1 (latest)

wispy tapir
#
#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;
}```
dusky merlinBOT
#

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.

wispy tapir
#

why cant i insert with range based loop?

floral vale
#

@wispy tapir What do you mean

#

Are you getting some sort of error?

#

Oh nvm

dusky merlinBOT
#

#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;
}
Programsnow
floral vale
#
  for (int i : v)
    std::cin >> i;
```Sets the local `i` variable, not anything in the vector
#

Use a reference

wispy tapir
#

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;```
floral vale
#

For the second loop it doesn't mater how you do it

wispy tapir
#

but why

#

as i see it, you dont create temporary copy of variable, access it directly, additionally, its const (which makes it faster (??) )

floral vale
#

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

wispy tapir
#

Okie, thanks!

#

!close