-2

I want to read integers from STDIN, 1,2,3,4

vector<int> r;
cin >> is;
stringstream iss(is);
int n;
while(iss >> n)
{
    r.push_back(n);
}

but stops reading after "," is there a way other than splitting and directly read Integers only.

1 Answers1

1

Here you have to consume and skip the , after every digit as shown here:

vector<int> r;
cin >> is;
stringstream iss(is);
int n;
while(iss >> n)
{
    r.push_back(n);
    char c;
    iss >> c;
}

See running example here.

Community
  • 1
  • 1
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28