Grab unknown number of integers out of a C++ string

I have a string that looks something like "Cell 4,102,210:13" The problem is that I don't necessarily know how many numbers are separated by a comma and how many digits they have. What would be the best way to get the integers out of the string?
You can use stringstreams to get numbers from a string and a vector to store them
eg:
1
2
3
4
5
6
7
8
9
10
11
stringstream ss("Cell 4,102,210:13");
vector<int> Numbers;
int Temp;
while (!ss.eof()  && !isdigit(ss.peek()) ) //discard all the non numeric characters at the beginning
    ss.get();
while(true)
{
    ss >> Temp;//Read next number
    Numbers.push_back(Temp);//Add the number to the vector
    if ( ss.get()!=',' || ss.eof() ) break; // if next character is not a comma or the stream has no more characters, exit from the loop
}
Last edited on
Topic archived. No new replies allowed.