Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
std::stringstream ss("22|23|53"); char delimiter = '|'; std::string str; while(std::getline(ss, str, delimiter)) { std::cout << str << std::endl; }
29th Jun 2022, 8:42 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 3
30th Jun 2022, 6:21 AM
Ipang
+ 2
By default getline(ss, str) would read until new line Default delimiter = '\n' but if you say you want to read until a slash You have to set the delimiter yourself The arguments it takes are input stream, string and delimiter It reads input stream while it doesn't get to the delimiter and stores it in the string It can be used with std::cin and std::ifstream because those are input streams as well
30th Jun 2022, 6:54 AM
Мартин 😑🎵
Мартин 😑🎵 - avatar
30th Jun 2022, 9:45 AM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
There is no option like that but you can do something else std::stringstream ss("2-2|24|25"); char del1 = '|', del2 = '-'; std::string str; while(std::getline(ss, str, del1)) { std::stringstream temp(str); while(std::getline(temp, str, del2)) { std::cout << str << std::endl; } }
30th Jun 2022, 9:18 AM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
Manav Roy if you don't mind using C functions in C++, strtok supports searching for multiple delimiters to break up a string.
1st Jul 2022, 1:23 PM
Brian
Brian - avatar
+ 1
Here is an example where I used strtok to look for any of three delimiters: https://code.sololearn.com/cZj8IdY69SYu/?ref=app And here is how I did the same task in C++, but without strtok. I used string find instead, after determining which single delimiter to look for: https://code.sololearn.com/c134MzCtfx80/?ref=app
1st Jul 2022, 5:44 PM
Brian
Brian - avatar