Advertisement
RobertDeMilo

RB5.4 Использование функции move при разбиении на слова

Apr 17th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. vector<string> SplitIntoWords(const string& text)
  11. {
  12.     vector<string> words;
  13.     string current_word;
  14.    
  15.     for (const char c : text)
  16.     {
  17.         if (c == ' ')
  18.         {
  19.             words.push_back(move(current_word));
  20.             current_word.clear();
  21.         }
  22.         else
  23.         {
  24.             current_word.push_back(c);
  25.         }
  26.     }
  27.     words.push_back(current_word);
  28.  
  29.     return words;
  30. }
  31.  
  32. int main()
  33. {
  34.     for (const string& word : SplitIntoWords("Red belt C++"))
  35.     {
  36.         cout << word << "\n";
  37.     }
  38.    
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement