Question

In: Computer Science

One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56")....

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.

Solutions

Expert Solution

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
}


Related Solutions

(1) Prompt the user for a string that contains two strings separated by a comma.
(In C)(1) Prompt the user for a string that contains two strings separated by a comma. Examples of strings that can be accepted:Jill, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. Ex:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string and remove any spaces. Store...
(1) Prompt the user for a string that contains two strings separated by a comma. (1...
(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex: Enter input...
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
So I need to parse tweets with the String class and I have no idea what...
So I need to parse tweets with the String class and I have no idea what should I do for this lab and how to start my coding. It will be nice if anyone guide me what should I do exactly and how I start this lab. Use the Scanner class (as discussed in lecture) to read in a tweet entered by the user and store it in a String variable named tweet. You will be splitting up (parsing) the...
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write...
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write a program which reads the file using the Python CSV module, creates a list from the contents, then divides the first number in each line by the second. Define a function in your program that performs the division operations; the function must accept two numeric parameters, divide the first parameter by the second, and return the result. Before dividing, your function must validate the...
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] and ('34',67', '55', '33', '12', '98').
Write a program which accepts a sequence of comma-separated numbers from console and generate a list...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] and ('34',67', '55', '33', '12', '98'). Input can be comma separated in one line.
2-Find the critical numbers of the function. (Enter your answers as a comma-separated list. Use n...
2-Find the critical numbers of the function. (Enter your answers as a comma-separated list. Use n to denote any arbitrary integer values. If an answer does not exist, enter DNE.) f(θ) = 6 cos(θ) + 3 sin2(θ) 3- Consider the following. f(x) = x5 − x3 + 9,    −1 ≤ x ≤ 1 (a) Use a graph to find the absolute maximum and minimum values of the function to two decimal places. maximum     minimum (b) Use calculus to find the exact...
Using C++ Many software applications use comma-separated values to store and transfer data. In this program,...
Using C++ Many software applications use comma-separated values to store and transfer data. In this program, you are asked to implement the movie_call function that will parse a string of the above structure into the corresponding movie name, rating, and movie length. ex: In "movie name, rating, length", the movie name is "movie name", the rating is "rating", and the length of the movie is "lenght". This is the code: #include <iostream> #include <string> using namespace std; /* Replace the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT