I never thought of it myself though I use it PHP, so I decided to write an implementation:
Code:
bool split(std::string sep, std::string str, std::vector<std::string> &array) {
int seplen = sep.length();
if (seplen < 1)
return false;
int len = str.length();
int find = 0, prev = 0;
for (;;) {
if (find = str.find(sep, find) == std::string::npos) {
array.push_back(str.substr(prev, len - prev));
return true;
}
else {
array.push_back(str.substr(prev, find - prev));
find += seplen;
prev = find;
}
}
return false;
}
probably could be optimised though.
The difference would be that we would be passing a vector of strings by reference, and then accessing the elements of the vector, rather than directly assigning the values to individual variables.