В Visual C ++ 11 вы можете использовать regex_token_iterator из TR1.
sregex_token_iterator::regex_type white_space_separators("[[:space:]]+",regex_constants::optimize);
for(sregex_token_iterator i(s.begin(),s.()end,white_space_separators,-1),end; i!=end; i++)
{
cout << *i << endl;
// or use i.start, i.end which is faster access
}
Если вы обеспокоены производительностью (и накладными расходами, такими как копирование строк), вы можете написать собственную подпрограмму:
#include <ctype.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s = "Text for tokenization ";
const char *start = s.c_str();
const char *end = start + s.size();
const char *token = start;
while (start!=end)
{
if(isspace(*start))
{
if (token < start)
{
// Instead of constructing string, you can
// just use [token,start] part of the input buffer
cout << string(token,start) << ' ';
}
start++;
token = start;
}
else
{
start++;
}
}
if (token < start)
{
cout << string(token,start) << ' ';
}
}