我在努力學習boost::spirit
。作爲一個例子,我試圖將一系列的單詞解析成一個vector<string>
。我嘗試這樣做:如何使用boost :: spirit將一個單詞序列解析成一個向量?
#include <boost/spirit/include/qi.hpp>
#include <boost/foreach.hpp>
namespace qi = boost::spirit::qi;
int main() {
std::vector<std::string> words;
std::string input = "this is a test";
bool result = qi::phrase_parse(
input.begin(), input.end(),
+(+qi::char_),
qi::space,
words);
BOOST_FOREACH(std::string str, words) {
std::cout << "'" << str << "'" << std::endl;
}
}
,給了我這樣的輸出:
'thisisatest'
,但我想下面的輸出,其中每個字分別匹配:
'this'
'is'
'a'
'test'
如果可能的話,我d喜歡避免爲這個簡單情況定義我自己的qi::grammar
子類。
很好解釋,+1 – sehe