In: Computer Science
One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56").
stringstream ss("23,4,56"); char ch; int a, b, c; ss >> a >> ch >> b >> ch >> c; // a = 23, b = 4, c = 56
You have to complete the function vector parseInts(string str). str will be a string consisting of comma-separated integers, and you have to return a vector of int representing the integers.
Note If you want to know how to push elements in a vector, solve the first problem in the STL chapter.
Input Format
The first and only line consists of n integers separated by commas.
Output Format
Print the integers after parsing it.
P.S.: I/O will be automatically handled. You need to
complete the function only.
Sample Input
23,4,56
Sample Output
23 4 56
How do I input a string to then convert the string into a integer vector.
vector<int> parseInts(string str){
stringstream ss1(str);//convert string to stringstream
vector<int> ints;//vector to hold int
int num;//variable to read integer from stringstream
char ch;//variable to read the comma
while(ss1>>num){//keep reading from the string untill an
integer is read
ints.push_back(num);//insert it into the vector
ss1>>ch;//read the comma
}
return ints;//return the vector
}