3
Boost Spirit qi :: symbols實現了鍵值對的映射:給出一個字符串的鍵,它可以返回一個特定的值。我的問題是:Boost Spirit Qi符號默認值和NULL值
1)對於空白字符串,是否可以返回默認值? (代碼中的Q1)
2)對於非空字符串或鍵值對映射中列出的鍵的字符串,是否可以返回表示鍵無效的值? (Q2代碼)
**以下代碼基於BOOST SPIRIT文檔。 **預先感謝您的任何建議。
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/assert.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
template <typename P, typename T>
void test_parser_attr(
char const* input, P const& p, T& attr, bool full_match = true)
{
using boost::spirit::qi::parse;
char const* f(input);
char const* l(f + strlen(f));
if (parse(f, l, p, attr) && (!full_match || (f == l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
int main()
{
using boost::spirit::qi::symbols;
symbols<char, int> sym;
sym.add
("Apple", 1)
("Banana", 2)
("Orange", 3)
;
int i;
test_parser_attr("Banana", sym, i);
std::cout << i << std::endl; // 2
test_parser_attr("", sym, i); // Q1: key is "",
std::cout << i << std::endl; // would like it to be 1 as default
test_parser_attr("XXXX", sym, i); // Q2: key is other than "Apple"/"Banana"/"Orange",
std::cout << i << std::endl; // would like it to be 4
return 0;
}
+1。我完成了80%的工作。 – sehe
太棒了!完美的解決方案,詳細的解釋,快速的反應非常感謝你。 – jianz