Advertisement
Korotkodul

split_string

Oct 10th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <sstream>
  7.  
  8.  
  9.  
  10. vector<string> split(const string& s) {
  11.     vector <string> res;
  12.     string cur = "";
  13.     int n = s.size();
  14.     for (int i = 0; i < n; ++i) {
  15.         if (s[i] == ' ') {
  16.             if (cur != "") {
  17.                 res.push_back(cur);
  18.                 cur = "";
  19.             }
  20.         } else {
  21.             cur += s[i];
  22.         }
  23.     }
  24.     return res;
  25. }
  26.  
  27. int main()
  28. {
  29.     string s;
  30.     getline(cin, s);
  31.     s += ' ';
  32.     auto v = split(s);
  33.     for (string i: v) {
  34.         cout << i << ' ';
  35.     } cout << "\n";
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement