0

Better altrernative than stringstream?

In cpp , Int n; String str"1 2 3 4 5 6 7" ; Stringstream s(str); While(s>>n) Cout<<n; to split the string and print the digits in it.Although is there any better option for efficiently do the same than this.(i think stringstream is slow)😢😢

25th Aug 2020, 4:30 AM
sid
sid - avatar
7 Antworten
+ 1
If all the numbers are one-digit only, you can iterate the string using for-loop or for-each loop. For each character, check `isdigit(<character>)`, if check yields true, print <character> - '0' casted as `int` or any other integral type. Obviously not an option in case there were numbers having more than 1 digit ...
25th Aug 2020, 6:19 AM
Ipang
+ 1
Ipang i need numbers with multiple digits
25th Aug 2020, 7:47 AM
sid
sid - avatar
+ 1
sid Maybe use strtok() if you don't mind going the C way. Or do the loop like above, but accumulate the number by multiplying previous digit by 10, before adding current digit. I don't even know if that was considered good practice though XD
25th Aug 2020, 7:56 AM
Ipang
+ 1
Ipang is there a split thing in cpp like python
25th Aug 2020, 8:25 AM
sid
sid - avatar
+ 1
Built-in for std::string? not as I understand it sid but I could be wrong ...
25th Aug 2020, 8:28 AM
Ipang
+ 1
Ipang Really appreciated Stay Safe
25th Aug 2020, 8:42 AM
sid
sid - avatar
+ 1
No problem sid 👌 But I'd suggest not to mark an answer too early, maybe others will come with different opinions later on, maybe even better.
25th Aug 2020, 8:45 AM
Ipang