ブーストスピリットqi :: symbolsはキーと値のペアのマップを実装しています。文字列のキーを指定すると、特定の値を返すことができます。私の質問は次のとおりです:ブーストスピリット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