title: Most efficient way to get bounding rectangle of vector of rectangles
body: <p>I have an <code>std::vector</code> of <code>cv::Rects</code> (from the opencv framework) called <code>rects</code>, but I suppose the same logic would be applied to any rectangle object, so I'm not going to tag opencv in this question.</p>
  
  <p>I am trying to find the smallest rectangle that encompasses all the rectangles in the vector, this is what I have so far:</p>
  
  <pre><code>int t = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().y &lt; b.tl().y;})-&gt;tl().y;
  int l = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().x &lt; b.tl().x;})-&gt;tl().x;
  int b = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().y &lt; b.br().y;})-&gt;br().y;
  int r = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().x &lt; b.br().x;})-&gt;br().x;
  cv::Rect smallest_encompassing_rect = cv::Rect(cv::Point(t, l),cv::Point(b, r)))
  </code></pre>
  
  <p>If it's not clear, the <code>.tl()</code> function gets the top left point of the rectangle, and <code>.br()</code> gets the bottom right.</p>
  
  <p>I know this will work, but I am wondering if there is a more efficient way to solve the problem. I am working with image processing, so my code is time sensitive.</p>
  
  <p>Any help would be appreciated,
  Thanks</p>