Yes. Something along the lines of the following, for example:
std::vector<std::string>
tokenize(std::stringconst& input, char separ)
{
std::vector<std::string> results;
std::string::const_iterator end = input..end();
std::string::const_iterator current = input.begin();
std::string::const_iterator next = std::find(current, end, separ);
while (next != end) {
results.push_back(std::string(current, next));
current = next + 1;
next = std::find(current, end, separ);
}
results.push_back(std::string(current, end));
return results;
}
Thanks all, I've come up with the following (although I'll have
problems in handling encodings where character are not 8 bit...):
template<typename T>
void split(const std::string& str, T container)
{
typedef std::string::const_iterator iter;
const std::locale loc("");
iter i = str.begin();
while (i != str.end())
{
i = std::find_if(i, str.end(), [&loc](char c) { return !
std::isspace(c, loc); });
iter j = std::find_if(i, str.end(), [&loc](char c) { return
std::isspace(c, loc); });
if (i != str.end())
*container++ = std::string(i, j);
i = j;
}
}
template<typename T>
void split(const std::string& str, const std::string& sep, T
container)
{
std::size_t start = 0;
std::size_t end = 0;
while ((end = str.find(sep, start)) != std::string::npos)
{
*container++ = str.substr(start, end - start);
start = end + sep.size();
}
if (start < str.size())
*container++ = str.substr(start);
}