title: Parse comma-separated ints/int-ranges in C++
body: <p>Given a string in C++ containing ranges and single numbers of the kind:</p>
  <pre><code>&quot;2,3,4,7-9&quot;
  </code></pre>
  <p>I want to parse it into a vector of the form:</p>
  <pre><code>2,3,4,7,8,9
  </code></pre>
  <p>If the numbers are separated by a <code>-</code> then I want to push all of the numbers in the range. Otherwise I want to push a single number.</p>
  <p>I tried using this piece of code:</p>
  <pre class="lang-cpp prettyprint-override"><code>const char *NumX = &quot;2,3,4-7&quot;;
  std::vector&lt;int&gt; inputs;
  std::istringstream in( NumX );
  std::copy( std::istream_iterator&lt;int&gt;( in ), std::istream_iterator&lt;int&gt;(),
             std::back_inserter( inputs ) );
  </code></pre>
  <p>The problem was that it did not work for the ranges. It only took the numbers in the string, not all of the numbers in the range.</p>