TrickmanOff

Untitled

Sep 19th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <string>
  2. #include <string_view>
  3.  
  4. bool NextToken(std::string_view& str, const char delim, std::string_view& token) {
  5. if (str.empty()) {
  6. return false;
  7. }
  8.  
  9. size_t delim_pos = str.find(delim);
  10. if (delim_pos == std::string::npos) {
  11. delim_pos = str.length();
  12. }
  13.  
  14. token = str.substr(0, delim_pos);
  15.  
  16. if (delim_pos != str.length()) {
  17. ++delim_pos;
  18. }
  19. str.remove_prefix(delim_pos);
  20.  
  21. return true;
  22. }
  23.  
  24. #include <iostream>
  25.  
  26. int main() {
  27. std::string_view sv = "Hello world and good bye";
  28. const char delimiter = ' ';
  29. std::string_view token;
  30. while (NextToken(sv, delimiter, token)) {
  31. // обрабатываем очередной token
  32. // например, печатаем его на экране:
  33. std::cout << token << "\n";
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment