c++ tokenize a string code example
Example 1: how to tokenize a string in c++
auto const str = "The quick brown fox"s;
auto const re = std::regex{R"(\s+)"};
auto const vec = std::vector<std::string>(
std::sregex_token_iterator{begin(str), end(str), re, -1},
std::sregex_token_iterator{}
);
Example 2: tokenize string c++
std::vector<std::string> tokenise(const std::string &str){
std::vector<std::string> tokens;
int first = 0;
while(first<str.size()){
int second = str.find_first_of(',',first);
if(second==std::string::npos){
second = str.size();
}
std::string token = str.substr(first, second-first);
tokens.push_back(token);
first = second + 1;
}
return tokens;
}