+ 3

Best way to get strings [solved]

I used to use "getline( cin >> ws, str);" but in references I noticed that they use "getline ( cin, str);" So what's the best way of get this strings

23rd Sep 2020, 8:18 PM
Elliot Larez
Elliot Larez - avatar
7 odpowiedzi
+ 3
I played with BroFar's code and arrived to this conclusion: Given " String with withespaces" as input. getline(cin, str); str is now the given string. getline (cin >> ws, str); str is now: "String with withespaces" , notice how the initial withespaces got removed.
24th Sep 2020, 1:36 AM
Kevin ★
+ 4
Elliot Larez if you go to my second code this is actually the way to get rid of all ws thanks to Ipang for flashing a trigger
24th Sep 2020, 5:07 AM
BroFar
BroFar - avatar
+ 3
Elliot Larez not sure if this is what you mean but it includes whitespace between words: ? Are you trying to eliminate whitespace ? https://code.sololearn.com/cE3ul692h6QD/?ref=app https://code.sololearn.com/c9jQe2mAn0r6/?ref=app
23rd Sep 2020, 9:25 PM
BroFar
BroFar - avatar
+ 3
Quoted from https://en.cppreference.com/w/cpp/io/manip/ws std::ws "Discards leading whitespace from an input stream."
24th Sep 2020, 4:34 AM
Ipang
+ 2
Elliot Larez One usecase of `std::ws` I happen to know, is to read a `std::string` after reading a number using `std::cin` like in this code below 👇 There may be other usecases that I just don't know yet. https://code.sololearn.com/c67K7qd8MpGO/?ref=app
24th Sep 2020, 6:06 AM
Ipang