Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <string_view>
- #include <vector>
- using namespace std;
- vector<string_view> SplitIntoWordsView(string_view str) {
- vector<string_view> result;
- str.remove_prefix(std::min(str.find_first_not_of(" "), str.size()));
- while (!str.empty())
- {
- auto space = str.find(' ');
- result.push_back(str.substr(0, space));
- str.remove_prefix(std::min(str.find_first_of(" "), str.size()));
- str.remove_prefix(std::min(str.find_first_not_of(" "), str.size()));
- }
- return result;
- }
- int main() {
- assert((SplitIntoWordsView("") == vector<string_view>{}));
- assert((SplitIntoWordsView(" ") == vector<string_view>{}));
- assert((SplitIntoWordsView("aaaaaaa") == vector{ "aaaaaaa"sv }));
- assert((SplitIntoWordsView("a") == vector{ "a"sv }));
- assert((SplitIntoWordsView("a b c") == vector{ "a"sv, "b"sv, "c"sv }));
- assert((SplitIntoWordsView("a bbb cc") == vector{ "a"sv, "bbb"sv, "cc"sv }));
- assert((SplitIntoWordsView(" a bbb cc") == vector{ "a"sv, "bbb"sv, "cc"sv }));
- assert((SplitIntoWordsView("a bbb cc ") == vector{ "a"sv, "bbb"sv, "cc"sv }));
- assert((SplitIntoWordsView(" a bbb cc ") == vector{ "a"sv, "bbb"sv, "cc"sv }));
- cout << "All OK" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement