title: Trying to erase all the undefined nodes in a vector C++
body: <p>I have a dictionary program that supposed to delete from the <code>vector</code> in it all the undefined nodes (nodes that I did not declare the value to them, keys only);</p>
  
  <hr>
  
  <p><strong>Dict.h</strong>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "Node.h"
  using namespace std;
  
  template &lt;class K, class V&gt;
  class Dict {
  public:
      Dict();
      vector&lt;Node&lt;K,V&gt;&gt; vec;
      V&amp; operator[](const K &amp;str);
  
  
  };
  
  template class Dict&lt;string, int&gt;;
  </code></pre>
  
  <hr>
  
  <p><strong>Dict.cpp:</strong></p>
  
  <pre><code>#include "Dict.h"
  
  template &lt;typename  K, typename V&gt;
  Dict&lt;K,V&gt;::Dict() {};
  
  template &lt;typename K, typename V&gt;
  V &amp; Dict&lt;K,V&gt;::operator[](const K &amp;str) {
      int i = 0,len = vec.size();
      while(i &lt; len){
          if(this-&gt;vec[i].value == 0)
          {
              this-&gt;vec.erase(this-&gt;vec.begin() + i);
              len = this-&gt;vec.size();
          }
          else
          {
              i++;
          }
      }
      Node&lt;K,V&gt; n;
      n.key = str;
      this-&gt;vec.push_back(n);
  
      return this-&gt;vec.back().value;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Node.h</strong>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  template &lt;class K, class V&gt;
  class Node {
  public:
      K key;
      V value;
      Node();
  };
  
  template class Node&lt;string, int&gt;;
  </code></pre>
  
  <hr>
  
  <p><strong>Node.cpp:</strong></p>
  
  <pre><code>#include "Node.h"
  
  template &lt;typename K, typename  V&gt;
  Node&lt;K,V&gt;::Node() {};
  </code></pre>
  
  <hr>
  
  <p><strong>main.cpp:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "Dict.h"
  using namespace std;
  
  int main() {
      Dict&lt;string,int&gt; d;
      d["nod1"] = 5;
      d["node2"] = 7;
      d["node3"];
      d["node4"] = 8;
      d["node5"];
      for(int i = 0; i &lt; d.vec.size(); i++)
      {
          cout &lt;&lt; d.vec[i].value;
      }
      return 0;
  }
  </code></pre>
  
  <p>in this main example, the output will be <code>5780</code>, but if I will declare <code>node5</code> to <code>9</code> for example, the output will be <code>5789</code>. that means the <code>[]</code> operator overloading function erases all the undefined nodes in the <code>vector</code> unless the last node if it's not defined.
  I have tried to do the erase loop after adding the current node but it throws me an exception: </p>
  
  <pre><code>Process finished with exit code 139
  </code></pre>