0

C++ Question re stringstream

Still new to string functions. I realize while (getline(ss, T, ',')) will find commas and create tokens(T). In the case the string has a few chars I want to create tokens around, is there a way to adjust for this so it not only finds ',' but say '/' and ' ' as well? So for example modifying it to find ispunct()? I've attempted to look this up (for ex.) https://cplusplus.com/reference/sstream/stringstream/ but find some of the descriptions are still slightly over my head. At the moment I've found ways around this but it's sloppy and ends up using multiple stringstreams, end code is messier than I'd like. I can easily remove all punctuation from a string but then I have no tokens. Any suggestions appreciated.

2nd Dec 2022, 12:50 AM
Scott D
Scott D - avatar
6 Answers
+ 3
Scott D you can use a for loop to split str1 if it encounters any character in str2 or as Tibor Santa suggested, you can use regex. https://code.sololearn.com/c5Eb211BG9gk/?ref=app
2nd Dec 2022, 6:03 AM
Bob_Li
Bob_Li - avatar
+ 3
That is the ideal way to learn.🤓 Use what you what you have, apply what you know. Refinement comes later. Making it work is the goal.
2nd Dec 2022, 6:37 AM
Bob_Li
Bob_Li - avatar
+ 2
Unfortunately getline can only handle a single delimiter character. I would find it most intuitive to use regular expressions to solve this, but it might be overkill (regex is it's own specialized language). You can find a few examples how you can approach this problem here: https://stackoverflow.com/questions/7621727/split-a-string-into-words-by-multiple-delimiters
2nd Dec 2022, 5:42 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Tibor Santa Cool, thanks Tibor. Bookmarked for future reference. I think for now I'll just accept having to use a few stringstreams. I've been told about vectors (which appear to be part of the upcoming c++ lessons) and things like boost and other external libraries, hopefully will get there in due time.
2nd Dec 2022, 6:07 AM
Scott D
Scott D - avatar
+ 2
Bob_Li Thank you as well, saved a copy for future reference, hope that's ok. I was working on a code coach "US date to Euro date" which requires changing m/d/y to d/m/y : Input Format: A string that contains a date formatting 11/19/2019 or November 19, 2019. Output Format: A string of the same date but in a different format: 19/11/2019. You can see the difficulty is in dealing with two different formats of input. Given my current knowledge I found the easiest way was to start with the while loop stringstream to create tokens around '/', create a counter and then for each var (d,m,y) use an if(count == 1) cout << token approach. Then set up an if(strlen > 2) branch thru an alternate stringstream to deal with changing months from str format into int (January to 1 etc.) depending on the input (see above). A little sloppy but it worked. Now looking at alternate ways that might be been cleaner or more efficient, to gain a little more insight.
2nd Dec 2022, 6:32 AM
Scott D
Scott D - avatar
+ 1
Bob_Li I think so too. If the code coach is marked medium then I should first find a solution using tools already taught. Then once I do I look for other possibilities to, as you say, refine. I trust it will come in time.
2nd Dec 2022, 7:11 AM
Scott D
Scott D - avatar