title: Why does this update to a map in a vector<map<int, int>> fail?
body: <p>I have a data structure represented by a vector of maps, all of which have the same template type. Inserting and reading works fine - however, for some reason, updates do nothing. I tried the methods described <a href="https://stackoverflow.com/questions/4527686/how-to-update-stdmap-after-using-the-find-method">here</a>, and they work fine - if I just use the map itself. However, when the map is in the vector, the element is found but not updated. Below, I have provided a minimal example.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;optional&gt;
  
  std::vector&lt;std::map&lt;int, int&gt;&gt; vec = std::vector&lt;std::map&lt;int, int&gt;&gt;();
  
  void insert_or_update( int key, int value ) {
      for ( std::map&lt;int, int&gt; map: vec ) {
          auto location = map.find( key );
          if ( location != map.end()) {
              location-&gt;second = value;
              std::cout &lt;&lt; "This should update the value, but doesn't" &lt;&lt; std::endl;
              return;
          }
      }
  
      // Insert, if no map currently contains the key
      std::cout &lt;&lt; "This value is new" &lt;&lt; std::endl;
      vec.back().insert( {key, value} );
  }
  
  int get_key( int key ) {
      for ( std::map&lt;int, int&gt; map: vec ) {
          auto location = map.find( key );
          if ( location != map.end()) {
              return location-&gt;second;
          }
      }
  
      std::cout &lt;&lt; "This value doesn't exist yet" &lt;&lt; std::endl;
      return 0;
  }
  
  int main()
  {
     std::map&lt;int, int&gt; map = std::map&lt;int, int&gt;();
     vec.push_back( map ); 
     std::cout &lt;&lt; get_key(3) &lt;&lt; std::endl;
     insert_or_update(3, 3);
     std::cout &lt;&lt; get_key(3) &lt;&lt; std::endl;
     insert_or_update(3, 5);
     std::cout &lt;&lt; get_key(3) &lt;&lt; std::endl;
     std::cout &lt;&lt; "Update in list failed, do it manually..." &lt;&lt; std::endl;
     auto location = map.find( 3 );
     location-&gt;second = 5;
     std::cout &lt;&lt; location-&gt;second &lt;&lt; std::endl;
  
     return 0;
  }
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>Why does this fail? I'm sure it's some kind of pointer logic I don't understand.</li>
  <li>What do I have to change to make it work?</li>
  </ol>