Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- //Функция разбивает строку на слова и складывает в вектор
- //Короче на итераторах он работает, даже если в конце нет пробела!!! Хорошо
- //Однако мне кажется, что реализацию на итераторах можно упростить она громоздкая
- //На итераторах
- vector<string> SplitIntoWordsIt(const string& s)
- {
- string word;
- vector<string> strs;
- auto Itbeg = s.begin();
- auto Itend = s.end();
- while(Itbeg<=Itend)
- {
- if (Itbeg == Itend)
- {
- strs.push_back(word);
- word.clear();
- break;
- }
- else if (*Itbeg != ' ')
- {
- word.push_back(*Itbeg);
- }
- else if (*Itbeg == ' ')
- {
- strs.push_back(word);
- word.clear();
- }
- Itbeg++;
- }
- return strs;
- }
- //Без итераторов и нужен пробел в конце
- vector<string> SplitIntoWords(const string& s)
- {
- string word;
- vector<string> strs;
- for(const auto& ch : s)
- {
- if(ch != ' ')
- {
- word.push_back(ch);
- }
- else if(ch == ' ')
- {
- strs.push_back(word);
- word.clear();
- }
- }
- return strs;
- }
- int main()
- {
- vector<string> strvec = SplitIntoWordsIt("Do you think we are animals");
- for(auto i : strvec)
- {
- cout << i << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement