c++ - Ignoring whitespaces/tabs/newlines with getline() -
c++ - Ignoring whitespaces/tabs/newlines with getline() -
i went through lot of resources on web still not able this. didn't understand how std::skipws works ignore whitespaces , tabs , newlines.
following simple code
vector<string> vec; while(1){ getline(cin, s); if( s.compare("#") == 0) break; else vec.push_back(s); }
i come in line of strings newlines, whitespaces , tabs. after input want store strings vector , stop when "#" string encountered. tried above code store spaces along strings in vector though terminates after enterting "#".
the purpose of std::getline
read entire line, including whitespace, string buffer.
if want read tokens stream, skipping whitespace, utilize standard input operator >>
.
std::vector<std::string> vec; std::string s; while(std::cin >> s && s != "#") { vec.push_back(s); }
live example
c++ stl
Comments
Post a Comment