Mooophy / Cpp-Primer

C++ Primer 5 answers
Creative Commons Zero v1.0 Universal
8.12k stars 3k forks source link

ex3_5b Why check whether str is empty? #709

Open jokerkeny opened 5 years ago

jokerkeny commented 5 years ago

https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_5b.cpp In line 13, for (string buff; cin >> buff; str += (str.empty() ? "" : " ") + buff); why should it check whether str is empty? Could it be empty?.

razeimmortal commented 5 years ago

str is empty at first. When you input some words, There should be a blank space between each two words, so it's neccessary check whether str is empty, If not ,insert " ".