title: Returning stl containers from functions
body: <p>What is the best way (performance-wise) of returning stl containers from a function? The container returned would usually contain several thousands of items.</p>
  
  <p>Method 1: </p>
  
  <pre><code>typedef std::list&lt;Item&gt; ItemContainer;
  
  ItemContainer CreateManyItems() {
      ItemContainer result;
  
      // fill the 'result' ...
  
      return result;
  }
  
  ItemContainer a = CreateManyItems();
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>void CreateManyItems(ItemContainer &amp;output) {
      ItemContainer result;
  
      // fill the 'result' ...
  
      output.swap(result);
  } 
  
  ItemContainer a;
  CreateManyItems(a);
  </code></pre>
  
  <p>Method 3:</p>
  
  <pre><code>void std::auto_ptr&lt;ItemContainer&gt; CreateManyItems() {
      std::auto_ptr&lt;ItemContainer&gt; result(new ItemContainer);
  
      // fill the 'result' ...
  
      return result;
  }
  
  std::auto_ptr&lt;ItemContainer&gt; a = CreateManyItems();
  </code></pre>
  
  <p>Or is there any better way?</p>