fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. bool nextToken(const string &s, string::size_type &start, string &token)
  7. {
  8. token.clear();
  9.  
  10. start = s.find_first_not_of(" \t", start);
  11. if (start == string::npos)
  12. return false;
  13.  
  14. string::size_type end;
  15.  
  16. if (s[start] == '\'')
  17. {
  18. ++start;
  19. end = s.find('\'', start);
  20. }
  21. else
  22. end = s.find_first_of(" \t,", start);
  23.  
  24. if (end == string::npos)
  25. {
  26. token = s.substr(start);
  27. start = s.size();
  28. }
  29. else
  30. {
  31. token = s.substr(start, end-start);
  32. if ((s[end] != ',') && ((end = s.find(',', end + 1)) == string::npos))
  33. start = s.size();
  34. else
  35. start = end + 1;
  36. }
  37.  
  38. return true;
  39. }
  40.  
  41. int main() {
  42. string s = "1, 10, 'abc', 'test, 1'", token;
  43. vector<string> v;
  44.  
  45. string::size_type start = 0;
  46. while (nextToken(s, start, token))
  47. v.push_back(token);
  48.  
  49. for (auto &token2 : v)
  50. cout << token2 << endl;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 4756KB
stdin
Standard input is empty
stdout
1
10
abc
test, 1