title: colon in for loop in C++
body: <p>In the following code I am trying to edit the contents of an unordered_map:</p>
  
  <pre><code>class A {
  public:
      A() {std::cout &lt;&lt; "Constructor called, address=" &lt;&lt; this &lt;&lt; std::endl;val=1;}
      int val;
  };
  
  void main(void) {
      std::unordered_map&lt;int,A&gt; um;
  
      for (int i=0;i&lt;3;i++)
          std::cout &lt;&lt; "um[" &lt;&lt; i &lt;&lt; "]=" &lt;&lt; &amp;(um[i]) &lt;&lt; ", val=" &lt;&lt; um[i].val &lt;&lt; std::endl;
      for (auto it : um)
          std::cout &lt;&lt; "&amp;it.second=" &lt;&lt; &amp;(it.second) &lt;&lt; ", val=" &lt;&lt; it.second.val &lt;&lt; std::endl;
  
      int index = 1;      
      for (auto um_it : um)
          um_it.second.val = index++;
  
      for (int i=0;i&lt;3;i++)
          std::cout &lt;&lt; "um[" &lt;&lt; i &lt;&lt; "]=" &lt;&lt; &amp;(um[i]) &lt;&lt; ", val=" &lt;&lt; um[i].val &lt;&lt; std::endl;
      for (auto it : um)
          std::cout &lt;&lt; "&amp;it.second=" &lt;&lt; &amp;(it.second) &lt;&lt; ", val=" &lt;&lt; it.second.val &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The output of the above is:</p>
  
  <pre><code>Constructor called, address=0x8dcb2c 
  um[0]=0x8dcb2c, val=1 
  Constructor called, address=0x8dc7ac 
  um[1]=0x8dc7ac, val=1 
  Constructor called, address=0x8dc42c 
  um[2]=0x8dc42c, val=1 
  &amp;it.second=0x7ffc62f24484, val=1 
  &amp;it.second=0x7ffc62f24484, val=1 
  &amp;it.second=0x7ffc62f24484, val=1 
  um[0]=0x8dcb2c, val=1 
  um[1]=0x8dc7ac, val=1 
  um[2]=0x8dc42c, val=1 
  &amp;it.second=0x7ffc62f24484, val=1 
  &amp;it.second=0x7ffc62f24484, val=1 
  &amp;it.second=0x7ffc62f24484, val=1
  </code></pre>
  
  <p>When I replace the editing code with:</p>
  
  <pre><code>int index = 1;      
  for (auto um_it = um.begin(); um_it != um.end(); ++um_it)
      um_it-&gt;second.val = index++;
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Constructor called, address=0x9d8b2c
  um[0]=0x9d8b2c, val=1
  Constructor called, address=0x9d87ac
  um[1]=0x9d87ac, val=1
  Constructor called, address=0x9d842c
  um[2]=0x9d842c, val=1
  &amp;it.second=0x7fffd2201c34, val=1
  &amp;it.second=0x7fffd2201c34, val=1
  &amp;it.second=0x7fffd2201c34, val=1
  um[0]=0x9d8b2c, val=3
  um[1]=0x9d87ac, val=2
  um[2]=0x9d842c, val=1
  &amp;it.second=0x7fffd2201c34, val=1
  &amp;it.second=0x7fffd2201c34, val=2
  &amp;it.second=0x7fffd2201c34, val=3
  </code></pre>
  
  <p>I understand from the results that in the first version the code affects a copy of the objects, but it seems strange. I also expected the copy constructor to be called but it was not. I'd be happy if someone can explain what is going on behind the scenes and what would be the best way to iterate through an unordered_map without making redundant copies.</p>