0

if I only want to print int array in CPP , what is the better impelemnt (and why) ?

for (const auto& x : { 1,2,3,4 })
{
    cout << x<< endl;
}

or

for (int x : { 1, 2, 3, 4 })
{
    cout << x << endl;
}
Streamer
  • 25
  • 4
  • If you just want to `cout` then the latter is better in this case (primitive type), for non-trivial type then `const&`, and `&` if you want mutable object – Cory Kramer Jul 29 '21 at 19:05
  • For `int`s, the second is marginally more efficient - it's one less dereference per pass trough the loop, and copying an `int` is cheap. – Paul Sanders Jul 29 '21 at 20:49

0 Answers0