B
B
badproger12years2020-02-22 14:01:06
C++ / C#
badproger12years, 2020-02-22 14:01:06

How to implement space-separated input in C++?

How to count space-separated values ​​into a vector in C++.
the first line contains a number equal to the number of characters to be read, and the second line contains these numbers.
Input example:
5
1 2 3 4 5
How to implement such an input in C ++ so that these numbers fall into a vector?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-02-22
@badproger12years

Guys, I found the most elegant solution to this problem

vector<int> v(n);
copy_n(istream_iterator<int>(cin), n, v.begin());
copy_n(v.begin(), n, ostream_iterator<int>(cout, " "));

generate_n(v.begin(), n, []{int x = 0; cin >> x; return x;});

R
Ronald McDonald, 2020-02-22
@Zoominger

Implement your split().
Take a string and in a loop from the first and last character, check if the character is a space. If not, add this character to the temporary string. If so, then add this string to the dynamic array of strings, and clear the temporary string. At the end, you return a dynamic array. Fini.
Or even switch to Qt.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question