Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. vector<string> split_string(string input_string) {
  2. string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
  3. return x == y and x == ' ';
  4. });
  5.  
  6. input_string.erase(new_end, input_string.end());
  7.  
  8. while (input_string[input_string.length() - 1] == ' ') {
  9. input_string.pop_back();
  10. }
  11.  
  12. vector<string> splits;
  13. char delimiter = ' ';
  14.  
  15. size_t i = 0;
  16. size_t pos = input_string.find(delimiter);
  17.  
  18. while (pos != string::npos) {
  19. splits.push_back(input_string.substr(i, pos - i));
  20.  
  21. i = pos + 1;
  22. pos = input_string.find(delimiter, i);
  23. }
  24.  
  25. splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
  26.  
  27. return splits;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement