title: convert OpenCV 2 vector<Point2i> to vector<Point2f>
body: <p>The OpenCV 2 contour finder returns a <code>vector&lt;Point2i&gt;</code>, but sometimes you want to use these with a function that requires a <code>vector&lt;Point2f&gt;</code>. What is the fastest, most elegant way to convert?</p>
  
  <p>Here are some ideas. A very general conversion function for anything that can be converted to a <code>Mat</code>:</p>
  
  <pre><code>template &lt;class SrcType, class DstType&gt;
  void convert1(std::vector&lt;SrcType&gt;&amp; src, std::vector&lt;DstType&gt;&amp; dst) {
    cv::Mat srcMat = cv::Mat(src);
    cv::Mat dstMat = cv::Mat(dst);
    cv::Mat tmpMat;
    srcMat.convertTo(tmpMat, dstMat.type());
    dst = (vector&lt;DstType&gt;) tmpMat;
  }
  </code></pre>
  
  <p>But this uses an extra buffer, so it's not ideal. Here's an approach that pre-allocates the vector then calls <code>copy()</code>:</p>
  
  <pre><code>template &lt;class SrcType, class DstType&gt;
  void convert2(std::vector&lt;SrcType&gt;&amp; src, std::vector&lt;DstType&gt;&amp; dst) {
    dst.resize(src.size());
    std::copy(src.begin(), src.end(), dst.begin());
  }
  </code></pre>
  
  <p>Finally, using a <code>back_inserter</code>:</p>
  
  <pre><code>template &lt;class SrcType, class DstType&gt;
  void convert3(std::vector&lt;SrcType&gt;&amp; src, std::vector&lt;DstType&gt;&amp; dst) {
    std::copy(src.begin(), src.end(), std::back_inserter(dst));
  }
  </code></pre>