title: Avoid making copies with vectors of vectors
body: <p>I want to be able to have a vector of vectors of some type such as:</p>
  
  <pre><code>vector&lt;vector&lt;MyStruct&gt; &gt; vecOfVec;
  </code></pre>
  
  <p>I then create a vector of MyStruct, and populate it.</p>
  
  <pre><code>vector&lt;MyStruct&gt; someStructs;
  // Populate it with data
  </code></pre>
  
  <p>Then finally add someStructs to vecOfVec;</p>
  
  <pre><code>vecOfVec.push_back(someStructs);
  </code></pre>
  
  <p>What I want to do is avoid having the copy constructor calls when pushing the vector.  I know this can be accomplished by using a vector of pointers, but I'd like to avoid that if possible.</p>
  
  <p>One strategy I've thought of seems to work, but I don't know if I'm over-engineering this problem.</p>
  
  <pre><code>// Push back an empty vector
  vecOfVec.push_back(vector&lt;MyStruct&gt;());
  
  // Swap the empty with the filled vector (constant time)
  vecOfVec.back().swap(someStructs);
  </code></pre>
  
  <p>This seems like it would add my vector without having to do any copies, but this seems like something a compiler would already be doing during optimization.</p>
  
  <p>Do you think this is a good strategy?</p>
  
  <p><b>Edit:</b> Simplified my swap statement due to some suggestions.</p>