title: c++ How to remove any empty elements in a two dimension vector
body: <p>I'm trying to remove any empty vectors inside another vector, below is my code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  
  using namespace std;
  
  int main()
  {
      vector&lt; vector&lt;int&gt; &gt; vector(9);
  
      vector[0].push_back(2);
      vector[3].push_back(5);
  
      cout &lt;&lt; vector.size() &lt;&lt; endl; // print 9
      for (int i = 0; i &lt; vector.size(); ++i) {
          if (vector[i].size() == 0) {
              vector.erase(vector.begin() + i);
          }
      }
      cout &lt;&lt; vector.size() &lt;&lt; endl; // prints 5?
  }
  </code></pre>
  
  <p>Now, it prints <code>5</code> in the second print statement, because once I erase, the vector immediately updates its size. So now the size is different than before, and the index is not pointing to the correct element.</p>
  
  <p>Is there anyway to fix this problem, so that I can find the correct size after removing all the empty elements? In this case, it should print <code>2</code>?</p>