I have found this the code example while searching for code snippet to split(similar to PHP's explode) std::string to a vector of substrings using delimiter of ' '(a Space :). String example - "one two three".
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
My problem is with scope of variable 'tokens'. Will it be an error to use such a split function because the scope of local variable ends once the function returns. I have an idea how to correct the problem, i am just not sure in my c++ skill. I am curious in ways of doing it in standards up to C++0x for use of such: explode(string, delimiter).
tokensis being returned by value (thus creating a copy), so I'm unsure which scope problem you are referring totokens(the compiler might optimize the copying, but the observable behavior will stay the same)