fork download
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. std::vector<std::string> tokenize(const std::string& line, const std::string& delimiters)
  7. {
  8. std::vector<std::string> tokens;
  9. std::size_t token_start = 0;
  10.  
  11. auto const npos = std::string::npos;
  12. while ((token_start = line.find_first_not_of(delimiters, token_start)) != npos)
  13. {
  14. auto token_end = line.find_first_of(delimiters, token_start);
  15. auto len = token_end == npos ? line.size() - token_start : token_end - token_start;
  16.  
  17. tokens.push_back(line.substr(token_start, len));
  18.  
  19. token_start = token_end;
  20. }
  21.  
  22. return tokens;
  23. }
  24.  
  25. int main()
  26. {
  27. auto tokens = tokenize("one.two three,4.5 six seven,eight.9.ten", " ,.");
  28.  
  29. for (auto& token : tokens)
  30. std::cout << token << '\n';
  31. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
one
two
three
4
5
six
seven
eight
9
ten