title: How to read space separated numbers from console?
body: <p>I'm trying to do a simple task of reading space separated numbers from console into a <code>vector&lt;int&gt;</code>, but I'm not getting how to do this properly. </p>
  
  <p>This is what I have done till now:</p>
  
  <pre><code>int n = 0;
  vector&lt;int&gt; steps;
  while(cin&gt;&gt;n)
  {
      steps.push_back(n);
  }
  </code></pre>
  
  <p>However, this requires the user to press an invalid character (such as <code>a</code>) to break the <code>while</code> loop. I don't want it. </p>
  
  <p>As soon as user enters numbers like <code>0 2 3 4 5</code> and presses <code>Enter</code> I want the loop to be broken. I tried using <code>istream_iterator</code> and <code>cin.getline</code> also, but I couldn't get it working. </p>
  
  <p>I don't know how many elements user will enter, hence I'm using <code>vector</code>. </p>
  
  <p>Please suggest the correct way to do this.</p>