title: Adding an element to a Vector while iterating over it
body: <p>As the title says, I want to add an element to a <code>std::vector</code> in certain cases while iterating through the vector.  With the following code, I'm getting an error "Debug assertion failed".  Is it possible to achieve what I want to do?</p>
  
  <p>This is the code I have tested:</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class MyClass
  {
  public:
      MyClass(char t_name)
      {
          name = t_name;
      }
      ~MyClass()
      {
      }
      char name;
  };
  
  int main()
  {
      std::vector&lt;MyClass&gt; myVector;
      myVector.push_back(MyClass('1'));
      myVector.push_back(MyClass('2'));
      myVector.push_back(MyClass('3'));
  
      for each (MyClass t_class in myVector)
      {
          if (t_class.name == '2')
               myVector.push_back(MyClass('4'));
      }
      return 0;
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Well, I thought <code>for each</code> was standard C++, but it seems that it's a Visual Studio feature:</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/ms177202.aspx" rel="noreferrer">for each, in</a></p>
  
  <p><a href="https://stackoverflow.com/questions/197375/">Visual c++ &quot;for each&quot; portability</a></p>