title: Erase element in vector while iterating the same vector
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3938838/erasing-from-a-stdvector-while-doing-a-for-each">Erasing from a std::vector while doing a for each?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to implement vertice coloring according to this algorithm;</p>
  
  <pre><code>/*
  Given G=(V,E):
  Compute Degree(v) for all v in V.
  Set uncolored = V sorted in decreasing order of Degree(v).
  set currentColor = 0.
  while there are uncolored nodes:
     set A=first element of uncolored
     remove A from uncolored
     set Color(A) = currentColor
     set coloredWithCurrent = {A}
     for each v in uncolored:
        if v is not adjacent to anything in coloredWithCurrent:
           set Color(v)=currentColor.
           add v to currentColor.
           remove v from uncolored.
        end if
     end for
     currentColor = currentColor + 1.
  end while
  */
  </code></pre>
  
  <p>I don't understand "add v to currentColor." line but I supposed, it means assing currentColor to v. Therefore what is the "set"? Anyway the problem is erasing element in vector while iterating it. This is the code.</p>
  
  <pre><code>    vector&lt;struct Uncolored&gt; uc;
      vector&lt;struct Colored&gt; c;   
  
      int currentColor = 0;
      struct Colored A;
      struct Colored B;
  
      vector&lt;struct Uncolored&gt;::iterator it;
      vector&lt;struct Uncolored&gt;::iterator it2;
      vector&lt;struct Colored&gt;::iterator it3;
  
      for(it=uc.begin();it&lt;uc.end();it++){
  
          A.id = (*it).id;        
          uc.erase(uc.begin());
          A.color = currentColor;
          c.push_back(A);
  
          for(it2=uc.begin();it2&lt;uc.end();it2++) {
              it3=c.begin();
              while(it3 != c.end()) {
                  if( adjacencyMatris[(*it2).id][(*it3).id] == 0 ) {
                      B.id = (*it2).id;       
                      it2 = uc.erase(it2);
                      B.color = currentColor;
                      c.push_back(B);
                  }
                  it3++;
              }
          }
          currentColor = currentColor + 1;
      }
  </code></pre>
  
  <p>I think <code>it2 = uc.erase(it2);</code> line is already general use but It gives run time error.</p>