Seredenko-V

Untitled

Aug 5th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include "string_processing.h"
  2.  
  3. using namespace std;
  4.  
  5. //vector<string> SplitIntoWords(const string& text) {
  6. //    vector<string> words;
  7. //    string word;
  8. //    for (const char c : text) {
  9. //        if (c == ' ') {
  10. //            if (!word.empty()) {
  11. //                words.push_back(word);
  12. //                word.clear();
  13. //            }
  14. //        } else {
  15. //            word += c;
  16. //        }
  17. //    }
  18. //    if (!word.empty()) {
  19. //        words.push_back(word);
  20. //    }
  21. //    return words;
  22. //}
  23.  
  24. vector<string_view> SplitIntoWords(string_view text) {
  25.     vector<string_view> result;
  26.     text.remove_prefix(min(text.find_first_not_of(" "), text.size())); // до первого символа
  27.     while (!text.empty()) {
  28.         size_t position_first_space = text.find(' ');
  29.         string_view tmp_substr = text.substr(0, position_first_space);
  30.         result.push_back(tmp_substr);
  31.         text.remove_prefix(tmp_substr.size());
  32.         text.remove_prefix(min(text.find_first_not_of(" "), text.size())); // до первого символа
  33.     }
  34.     return result;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment