Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <string_view>
- bool NextToken(std::string_view& str, const char delim, std::string_view& token) {
- if (str.empty()) {
- return false;
- }
- size_t delim_pos = str.find(delim);
- if (delim_pos == std::string::npos) {
- delim_pos = str.length();
- }
- token = str.substr(0, delim_pos);
- if (delim_pos != str.length()) {
- ++delim_pos;
- }
- str.remove_prefix(delim_pos);
- return true;
- }
- #include <iostream>
- int main() {
- std::string_view sv = "Hello world and good bye";
- const char delimiter = ' ';
- std::string_view token;
- while (NextToken(sv, delimiter, token)) {
- // обрабатываем очередной token
- // например, печатаем его на экране:
- std::cout << token << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment