fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. bool check(std::size_t pos)
  6. {
  7. return pos != std::string::npos;
  8. }
  9.  
  10. std::vector<std::string> tokenize(const std::string& text, const std::string& delimiters = " ")
  11. {
  12. std::vector<std::string> tokens;
  13.  
  14. std::size_t start_pos;
  15. std::size_t end_pos = 0;
  16.  
  17. while (check(start_pos = text.find_first_not_of(delimiters, end_pos)) )
  18. {
  19. end_pos = text.find_first_of(delimiters, start_pos);
  20. tokens.emplace_back(text.substr(start_pos, end_pos - start_pos));
  21. }
  22.  
  23. return tokens;
  24. }
  25.  
  26. int main()
  27. {
  28. {
  29. auto t = tokenize("8 (4 ounce) fillets salmon,1/2 cup peanut oil,4 tablespoons soy sauce,"
  30. "4 tablespoons balsamic vinegar,4 tablespoons green onions (chopped),3 teaspoons "
  31. "brown sugar,2 cloves garlic (minced),1 1/2 teaspoons ground ginger,2 teaspoons "
  32. "crushed red pepper flakes,1 teaspoon sesame oil,1/2 teaspoon salt", ",");
  33.  
  34. for (auto& token : t)
  35. std::cout << token << '\n';
  36.  
  37. std::cout << '\n';
  38. }
  39.  
  40. {
  41. auto t = tokenize("Test 1,string 1,Test 2,string 2:Test 3:string 3", ",:");
  42.  
  43. for (auto& token : t)
  44. std::cout << token << '\n';
  45.  
  46. std::cout << '\n';
  47. }
  48.  
  49. {
  50. auto t = tokenize("Mary had a little lamb");
  51.  
  52. for (auto& token : t)
  53. std::cout << token << '\n';
  54. }
  55.  
  56. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
8 (4 ounce) fillets salmon
1/2 cup peanut oil
4 tablespoons soy sauce
4 tablespoons balsamic vinegar
4 tablespoons green onions (chopped)
3 teaspoons brown sugar
2 cloves garlic (minced)
1 1/2 teaspoons ground ginger
2 teaspoons crushed red pepper flakes
1 teaspoon sesame oil
1/2 teaspoon salt

Test 1
string 1
Test 2
string 2
Test 3
string 3

Mary
had
a
little
lamb