title: Iterating through container - when to use reference and when not
body: <p>In the code below, we don't dereference <code>y</code> (it is error in fact), but we do dereference <code>z</code>. Can someone explain why so? </p>
  
  <pre><code> #include &lt;iostream&gt;  
   #include &lt;vector&gt;  
  
  
  int main()   
  {  
  std::vector &lt;int&gt; x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  
   for( auto y : x ) 
       std::cout &lt;&lt; y &lt;&lt; " ";  
  
  std::cout&lt;&lt;std::endl;
  
  for(auto z = x.begin();z!=x.end();++z)
      std::cout&lt;&lt;*z&lt;&lt;" ";
  }
  </code></pre>