Класс Boost tokenizer может сделать эту вещь довольно простой:
#include
#include
#include
#include
using namespace std;
using namespace boost;
int main(int, char**)
{
string text = "token, test string";
char_separator sep(", ");
tokenizer< char_separator > tokens(text, sep);
BOOST_FOREACH (const string& t, tokens) {
cout << t << "." << endl;
}
}
Обновлен для C ++ 11:
#include
#include
#include
using namespace std;
using namespace boost;
int main(int, char**)
{
string text = "token, test string";
char_separator sep(", ");
tokenizer> tokens(text, sep);
for (const auto& t : tokens) {
cout << t << "." << endl;
}
}