title: Elegant solution to take input to a vector<int>
body: <p>I am trying to create an <code>vector &lt;int&gt;</code> whose size is not pre-defined. It should take in numbers as long as there are numbers in the input terminal and should stop reading when I hit <code>Enter</code>. I tried many solutions including the ones given <a href="https://stackoverflow.com/a/24583408/4834108">here</a> and <a href="https://stackoverflow.com/a/24585197/4834108">here</a>. In the second case, I can enter a non-integer to terminate the input to the vector. If I use the first solution (code added below), it listens to the input indefinitely.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iterator&gt;
  
  using std::cout;
  using std::cin;
  using std::vector;
  using std::string;
  using std::istringstream;
  
  int main()
  {
      //cout &lt;&lt; "Enter the elements of the array: \n";
      //vector &lt;int&gt; arr ( std::istream_iterator&lt;int&gt;( std::cin ), std::istream_iterator&lt;int&gt;() );
      vector &lt;int&gt; arr;
      string buf;
      cout &lt;&lt; "Enter the elements of the array: \n";
      while(getline(cin, buf))
      {
          istringstream ssin(buf);
          int input;
          while(ssin &gt;&gt; input)
          {
              arr.push_back(input);
          }
      }
  
      cout &lt;&lt; "The array: \n";
      for(size_t i = 0; i &lt; arr.size(); i++)
      {
          cout &lt;&lt; arr[i] &lt;&lt; " ";
      }
      return 0;
  }
  </code></pre>
  
  <p>1) I don't feel that typing in a character or a very large number to end listening to input is very elegant. However, the solution with <code>istringstream</code> seems to be the way to go. I am not sure why it doesn't work. </p>
  
  <p>2) Is there any way to detect the <code>Enter</code> from keyboard to terminate listening to input? I tried using <code>cin.get()</code>, but it changed the numbers in the vector.</p>
  
  <p>3) Any other methods or suggestions?</p>