fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. template <typename OutputInserter>
  8. void split(const string &line, OutputInserter inserter, char delim)
  9. {
  10. register unsigned pos_start = 0;
  11. register unsigned pos_end = 0;
  12.  
  13. while((pos_end = line.find(delim, pos_start)) != string::npos)
  14. {
  15. *inserter = line.substr(pos_start, pos_end-pos_start);
  16. pos_start = pos_end+1;
  17. ++inserter;
  18. }
  19. if (line.back() == delim) return;
  20. *inserter = line.substr(pos_start);
  21. ++inserter;
  22. }
  23. int main()
  24. {
  25. string test1 = "1;2;3;4;5;6";
  26. vector<string> splited;
  27. split(test1, back_inserter(splited), ';');
  28. for (auto it: splited)
  29. cout<<it<<endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6