0

I wanted to know the difference between :

for(auto i : vector ){
     cout << i << " ";
}

And :

for( auto &i : vector){
    cout << i << " ";
}

It would be helpful if someone points out the difference between these two Thanks ! :)

Sudheera Y S
  • 59
  • 1
  • 6
  • One uses values (so makes copies), other one uses references. Depending on the contents of `vector` one might be better than the other in certain cases. – DeiDei Jan 11 '20 at 12:00
  • Also [What is the correct way of using C++11's range-based for?](https://stackoverflow.com/questions/15927033/what-is-the-correct-way-of-using-c11s-range-based-for) – walnut Jan 11 '20 at 12:00
  • 1
    In generic code `auto&&` may be useful. – Evg Jan 11 '20 at 12:01

0 Answers0