title: Returning a std::vector - right approach
body: <p>I am trying to create a class method that will return a std::vector, and am a bit confused about the best way to do this.</p>
  
  <p>The approach I've used is to define the following method:</p>
  
  <pre><code>std::vector&lt;double&gt;* GetBins(void);
  </code></pre>
  
  <p>and in the method, allocate a new std::vector, which I fill with data. I'm returning a pointer to this ie.</p>
  
  <pre><code>std::vector&lt;double&gt;* Frequency::GetBins(void) {
      std::vector&lt;double&gt; *rtnVec = new std::vector&lt;double&gt;();
      for (_itMap = _mapFreq.begin(); _itMap != _mapFreq.end(); _itMap++ ) {
          rtnVec-&gt;push_back((*_itMap).first);
      }
      return rtnVec;
   }
  </code></pre>
  
  <p>(_itMap is a class-defined iterator).</p>
  
  <p>In my main.cpp, I've done the following:</p>
  
  <pre><code> std::vector&lt;double&gt;* myBins;
   myBins = myFreq3-&gt;GetBins();
   delete myBins;
  </code></pre>
  
  <p>I know with this approach, I'm going to get a dangling pointer unless I delete the pointer in the main.cpp code, so it's already a bit 'dangerous'. What's the best way to return a new std::vector from a class method?</p>
  
  <p>Thanks guys
  Pete</p>