Getting input numbers from a user

How can I get input from a user if they need to separate their inputs with a space (e.g. 1 2 3 4 5) and I want to put it in an array? Thanks.

Hmmm. I see that most of the answers use a vector, which I think I will need to do research. I thought there would be a simpler, but possibly more messianic answer, since we are not looking at vectors like using sscanf or something like that. Thanks for the input.

+4
source share
3 answers
#include <vector> #include <iostream> using namespace std; int main() { vector<int> num; int t; while (cin >> t) { num.push_back(t); } } 
+2
source

Alternatively and in a more general form:

 #include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; int main() { vector<int> num; copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(num)); } 
+1
source
 #include <iostream> #include <iterator> #include <vector> std::istream_iterator< int > iterBegin( std::cin ), iterEnd; std::vector< int > vctUserInput( iterBegin, iterEnd ); 
+1
source

Source: https://habr.com/ru/post/1301667/


All Articles