title: "error: assignment of read-only location" in unordered_map (C++)
body: <p>I have an awkward hash table (specifically, an unordered_map) with <code>int</code> keys and <code>vector&lt; vector&lt; int &gt;&gt;</code> data. I periodically need to update elements in this two-dimensional vector of ints. There's no intrinsic reason I shouldn't be able to, right? A newer g++ compiler I've switched to complains of an assignment of read-only location on the line designated below.</p>
  
  <pre><code>typedef std::tr1::unordered_map&lt; int, vector&lt; vector&lt; int &gt; &gt; &gt; pimap;
  
  vector&lt; Strain * &gt; liveStrains;
  pimap phenotypeIs;
  int NUM_DEMES = 3;
  
  ...
  vector&lt; Strain * &gt;::const_iterator lsItr;
  for ( lsItr = liveStrains.begin(); lsItr != liveStrains.end(); ++lsItr ) {
   int thisP = (*lsItr)-&gt;getPhenotype();
   pimap::iterator piItr = phenotypeIs.begin();
   piItr = phenotypeIs.find( thisP );
   if ( piItr != phenotypeIs.end() ) {
     for ( int d = 0; d &lt; NUM_DEMES; d++ ) {
        ( piItr -&gt; second )[ thisStep ].at( d ) = (*lsItr)-&gt;getI( d );  // error here
     }
   }
  }
  </code></pre>
  
  <p>I'm new to C++, so nothing's too obvious. Thank you for any help.</p>
  
  <hr>
  
  <p><strong>Following Tim's suggestion</strong></p>
  
  <p>I've replaced the relevant parts of code above with the following:</p>
  
  <pre><code>  pimap::iterator piItr = phenotypeIs.find( thisP );
    if ( piItr != phenotypeIs.end() ) {
      for ( int d = 0; d &lt; NUM_DEMES; d++ ) {
        vector&lt; vector&lt; int &gt; &gt; &amp; thisVec2 = piItr-&gt;second;
        vector&lt;int&gt; &amp; thisVec = thisVec2.at( thisStep );
        int &amp; ii = thisVec.at( d );
        ii = (*lsItr)-&gt;getI( d );
        // ( piItr -&gt; second )[ thisStep ].at( d ) = (*lsItr)-&gt;getI( d ); // error was here
      }
  </code></pre>
  
  <p>This code compiles without the error and appears to run fine. Like Tim, I still don't quite understand why the fix works. The error was previously appearing with gcc version 4.1.2 20080704 (Red Hat 4.1.2-44) but not with gcc version 4.0.1 (Apple Inc. build 5465). I will try to dissect the error more carefully when I'm not under a tight deadline!</p>