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)😢😢
7 odpowiedzi
+ 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 ...
+ 1
Ipang i need numbers with multiple digits
+ 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
+ 1
Built-in for std::string? not as I understand it sid but I could be wrong ...
+ 1
Ipang Really appreciated
Stay Safe
+ 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.