부스트 스피리트 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