title: Why this code works fine? (vector.clear(), vector<vector<int> >)
body: <p>Here's a snippet of a code I submitted. It worked but it made me think how exactly <code>clear()</code> works for a vector.</p>
  
  <pre><code>vector&lt;vector&lt;int&gt; &gt; result;
  vector&lt;int&gt; temp;
  temp.push_back(1);
  temp.push_back(2);
  result.push_back(temp);
  temp.clear();
  temp.push_back(3);
  temp.push_back(4);
  result.push_back(temp);
  
  //print result vector here.
  </code></pre>
  
  <p>How come the result vector is <code>[[1, 2], [3, 4]]</code>. </p>
  
  <p>Here's what <a href="http://en.cppreference.com/w/cpp/container/vector/clear" rel="nofollow">cppreference</a> has to say about <code>vector.clear()</code>:</p>
  
  <blockquote>
    <p>Removes all elements from the container. Invalidates any references,
    pointers, or iterators referring to contained elements. May invalidate
    any past-the-end iterators.   Leaves the capacity() of the vector
    unchanged.</p>
  </blockquote>
  
  <p>I suspect that this behavior should not be relied upon. </p>
  
  <p>Should clearing the temp vector not destroy the contents? Also, the description on <code>cppreference</code> makes me feel like inserting <code>3</code> and <code>4</code> will cause them to overwrite <code>1</code> and <code>2</code> since the <code>temp</code> vector was not allocated any new memory?</p>
  
  <p>Here's the code on ideone: <a href="https://ideone.com/Onwlro" rel="nofollow">https://ideone.com/Onwlro</a></p>