Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there's anything in the STL or Boost that does this.</p> <p>One thing you can do is to make your algorithm a little bit more general:</p> <pre><code>template&lt;class InputIterator, class OutputIterator&gt; void setToRanges(InputIterator first, InputIterator last, OutputIterator dest) { typedef std::iterator_traits&lt;InputIterator&gt;::value_type item_type; typedef typename std::pair&lt;item_type, item_type&gt; pair_type; pair_type r(-std::numeric_limits&lt;item_type&gt;::max(), -std::numeric_limits&lt;item_type&gt;::max()); for(; first != last; ++first) { item_type i = *first; if (i != r.second + 1) { if (r.second &gt;= 0) *dest = r; r.first = i; } r.second = i; } *dest = r; } </code></pre> <p>Usage:</p> <pre><code>std::set&lt;int&gt; set; // insert items typedef std::pair&lt;int, int&gt; Range; std::vector&lt;Range&gt; ranges; setToRanges(set.begin(), set.end(), std::back_inserter(ranges)); </code></pre> <p>You should also consider using the term <code>interval</code> instead of <code>range</code>, because the latter in STL parlance means "any sequence of objects that can be accessed through iterators or pointers" (<a href="http://www.cplusplus.com/reference/algorithm/" rel="nofollow noreferrer">source</a>).</p> <p>Finally, you should probably take at look at the <a href="http://www.boost.org/doc/libs/1_37_0/libs/numeric/interval/doc/interval.htm" rel="nofollow noreferrer">Boost Interval Arithmetic Library</a>, which is currently under review for Boost inclusion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload