input multiple integers in c++ line in single line with whitespace

input multiple integers in c++ line in single line with whitespace

Please format your question properly. Make it more detailed.

In case you’re asking on how to store multiple integers which are given in a single line.
e.g 1 2 3 4 25 78 96 52
You can simply do it using below code by using istringstream. Make sure to include header file.


    vector< int >arr;
    string input;
    getline(cin, input);
    istringstream is(input);
    int num;
    while(is>>num) arr.push_back(num);

and if you know the number of integers on a given line, you can do it simply by looping for given no of times and keep on storing them.

for a more complicated scenario like integers separated by some character or something similar, you should play with below code


\#include <iostream>
\#include <vector>
\#include <sstream>
using namespace std;
int main()
{
    vector< int >arr;
    string input;
    getline(cin, input);
    istringstream is(input);
    int num;
    char c;
    while(true)
    {
        is>>num;
        arr.push_back(num);
        if(!is.eof()) is>>c;
        else break;
    }
    for(int x: arr)
    cout<< x <<" ";
}