我有下列形式 a = x + y
或abc = xyz + 5
或6 + 5
或f(p)
串標記化++包括定界符
我需要的是來標記串,使得我讀取每個operator
和operand
所以對於a = x + y
令牌返回的串應該是a,=,x,+,y
,如果是abc=xyz+5
則應該返回abc,=,xyz,+,5
。請注意,有可能會或可能不會是空間operator
和operands
之間,這是我曾嘗試
void tokenize(std::vector<std::string>& tokens, const char* input, const char* delimiters) {
const char* s = input;
const char* e = s;
while (*e != 0) {
e = s;
while (*e != 0 && strchr(delimiters, *e) == 0) {
++e;
}
if (*e != ' ' && strchr(delimiters, *e) != 0){
std::string op = "";
op += *e;
tokens.push_back(op);
}
if (e - s > 0) {
tokens.push_back(std::string(s,e - s));
}
s = e + 1;
}
}
這似乎是功課 – CAbbott 2012-03-22 13:30:18
如果沒有家庭作業,我希望看到一個[Boost.Spirit(HTTP:// WWW .boost.org/doc/libs/1_49_0/libs/spirit/doc/html/index.html)回答。我從來沒有完全明白如何使用它。 – 2012-03-22 13:32:34
找到了答案? – Michael 2012-06-09 22:30:24