Banged this out. Not sure if it's completely bug-free.
Code:
#include <string>
#include <vector>
std::vector<std::string> SplitStr(const std::string& text, const std::string& delimeter)
{
std::size_t pos = 0;
std::size_t oldpos = 0;
std::size_t delimlen = delimeter.length();
std::vector<std::string> result;
while(pos != std::string::npos)
{
pos = text.find(delimeter, oldpos);
result.push_back(text.substr(oldpos, pos - oldpos));
oldpos = pos + delimlen;
}
return result;
}
Usage:
Code:
#include <string>
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
string s = "a_token another_token more_token last_token";
vector<string> results = SplitStr(s, " "); // split by spaces
for(vector<string>::iterator i = results.begin();
i < results.end();
++i)
{
cout << (*i) << endl;
}
return 0;
}
I'm sure you could also use a stringstream for this. Of course, it would be much faster and efficient to use the c-style strtok.