fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. string getElement(const char* &c)
  9. {
  10. string t;
  11. while(*c &&(*c == ' ' || *c == ',' || *c == '}')) ++c;
  12. if (*c == 0) return "";
  13. if (*c == '{')
  14. {
  15. int cnt = 0;
  16. do {
  17. t += *c;
  18. if (*c == '{') cnt++;
  19. if (*c == '}') cnt--;
  20. c++;
  21. } while(cnt);
  22. }
  23. else
  24. {
  25. while(*c != ',' && *c != '}' )
  26. {
  27. if (*c != ' ') t += *c;
  28. c++;
  29. }
  30. }
  31. return t;
  32. }
  33.  
  34. vector<string> parse(const string& s)
  35. {
  36. vector<string> v;
  37. const char * c = s.data();
  38. for(;*c && *c != '{'; ++c);
  39. if (*c++ == 0) return v;
  40. string t;
  41.  
  42. while((t = getElement(c)).size())
  43. v.push_back(t);
  44. return v;
  45. }
  46.  
  47. int main(int argc, char * argv[])
  48. {
  49. for(auto s: parse("{ a, b, c }" )) cout << "[" << s << "]"; cout << endl;
  50. for(auto s: parse("{ {a}, {b}, {c} }")) cout << "[" << s << "]"; cout << endl;
  51. for(auto s: parse("{ {a, b} , c }" )) cout << "[" << s << "]"; cout << endl;
  52. for(auto s: parse("{ {a, {b}} , c }" )) cout << "[" << s << "]"; cout << endl;
  53. }
  54.  
Success #stdin #stdout 0s 5568KB
stdin
Standard input is empty
stdout
[a][b][c]
[{a}][{b}][{c}]
[{a, b}][c]
[{a, {b}}][c]