Advertisement
RobertDeMilo

RB4.10 Удобное использование string view

Jun 9th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. vector<string_view> SplitIntoWords(const string& s)
  2. {
  3.     string_view str = s;
  4.     vector<string_view> result;
  5.    
  6.     while (true)
  7.     {
  8.         size_t space = str.find(' ');
  9.         result.push_back(str.substr(0,space));
  10.  
  11.         if (space == str.npos)
  12.         {
  13.             break;
  14.         }
  15.         else
  16.         {
  17.            str.remove_prefix(space+1);
  18.         }
  19.     }
  20.     return result;
  21. }
  22.  
  23. int main()
  24. {
  25.  
  26.     for (const string& word : SplitIntoWords("      Red       belt C++     "))
  27.     {
  28.         cout << word << "\n";
  29.     }
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement