fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. int main()
  6. {
  7. std::string s = "whatever\nyou want\nto have";
  8. std::vector<std::string> vec;
  9.  
  10. std::size_t pos;
  11. while( (pos = s.find('\n')) != std::string::npos )
  12. {
  13. vec.push_back( s.substr(0,pos) );
  14. s = s.substr(pos+1);
  15. }
  16. vec.push_back(s);
  17.  
  18. // print out
  19. for(pos = 0; pos < vec.size(); ++pos)
  20. {
  21. std::cout << "vec[" << pos << "] = \"" << vec[pos] << "\"" << std::endl;
  22. }
  23. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
vec[0] = "whatever"
vec[1] = "you want"
vec[2] = "to have"