title: trouble working with map iterators in c++
body: <p>I am trying to go through my map of patient IDs, find the sequential ones that match, and find the difference squared for each pair of results (for the same patient). However I am having some trouble with the "itid" iterator in the line, "for (itid = nx; itid != mymap.end(); itid++) {" and I'm not sure why. When I take out the code with "itid" it works just fine. I am a programming beginner, so any advice would be greatly appreciated!</p>
  
  <pre><code>map &lt;int,struct testInfo&gt; mymap;
  map &lt;int,struct testInfo&gt;:: iterator it;
  pair&lt;map&lt;int,struct testInfo&gt;::iterator,bool&gt; ret;
  map &lt;int,struct testInfo&gt;:: iterator itid;
  
  
  
      int arraySize = 10000;
      double diffsq[arraySize];
      int count = 1;
  
      for ( it=mymap.begin() ; it != mymap.end(); it++ ) {
  
          auto nx = next(it);
  
          //comparing each patientID to the next patientID
          if ((it-&gt;second.patientID == nx-&gt;second.patientID) &amp;&amp; it-&gt;second.patientID != 0) {
  
              for (itid = nx; itid != mymap.end(); itid++) {
  
                  if ((it-&gt;second.patientID == itid-&gt;second.patientID) &amp;&amp; it-&gt;second.patientID != 0) {
  
                      diffsq[count] = pow((it-&gt;second.result - itid-&gt;second.result), 2); 
                      count++;
  
                  } else
                      itid = mymap.end();
  
              }
  
          }
  
      }
  </code></pre>