Simply, there is a string, words in it, separated by spaces. How to walk on this line, alternately recorded in the buffer these words. That is, first we write the first word into the buffer, then the second, and so on until the end of this line. Interested in how to do this based on string methods, or in another most rational way.
1 answer
Here is a demo program. The "buffer" is the std::vector container.
#include <iostream> #include <sstream> #include <algorithm> #include <iterator> #include <vector> #include <string> int main() { char s[] = "one two three"; std::cout << s << std::endl; std::vector<std::string> v; std::istringstream is( s ); std::copy( std::istream_iterator<std::string>( is ), std::istream_iterator<std::string>(), std::back_inserter( v ) ); for ( const auto &item : v ) std::cout << item << ' '; std::cout << std::endl; return 0; } Its output to the console
one two three one two three You can replace the declaration of a character array with a declaration of an object of type std::string .
std::string s( "one two three" ); |
operator>>not suitable? - αλεχολυτ