创建并写入动态矢量
Create and write to vector on the fly
我想在一个精神规则中创建向量并向其附加值(如果有的话)。有可能吗?我尝试了下面这样的方法,但没有成功。请阅读代码注释了解详细信息。谢谢
typedef std::vector<double> number_array;
typedef std::vector<std::string> string_array;
typedef boost::variant<number_array, string_array> node
template<typename Iterator>
struct parser
: qi::grammar<Iterator, node(), ascii::space_type> {
parser(parser_impl* p)
: parser::base_type(expr_, ""),
error_handler(ErrorHandler(p)) {
// Here I want to create vector on the fly
// and append values to newly created vector.
// but this doesn't compile, specifically phoenix::push_back(...)
number_array_ = qi::lit('n[')[qi::_val = construct<number_array>()] >>
-(qi::double_ % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';
// this doesn't compile too
string_array_ = qi::lit('s[')[qi::_val = construct<string_array>()] >>
-(quoted_string % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';
quoted_string %= "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
expr_ = number_array_[qi::_val = qi::_1] | string_array_[[qi::_val = qi::_1]];
}
qi::rule<Iterator, number_array(), ascii::space_type> number_array_;
qi::rule<Iterator, string_array(), ascii::space_type> string_array_;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, node(), ascii::space_type> expr_;
};
这里最重要的一点是,我认为您可以不使用所有的语义操作。
它们只执行默认属性规则已经执行的操作(基本上,标量属性为_val = _1
,conainer属性为insert(_val, end(_val), _1)
)。
这意味着你可以只将整个shebang写为
number_array_ = "n[" >> -(qi::double_ % ',') >> ']';
string_array_ = "s[" >> -(quoted_string % ',') >> ']';
quoted_string = "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
expr_ = number_array_ | string_array_;
这会奏效的。注意,我修复了多字节文字'n['
和's[n'
。
另见Boost Spirit:";语义行为是邪恶的"?
相关文章:
- 从C++中的数字输入动态创建矩阵
- 如何从QToolBox中动态创建的QLineEdit中获取文本
- 如何在C++中为堆栈动态创建结构?
- 如何使用类型级函数动态创建静态类型?
- 如何在动态创建的CMFCToolbar的工具提示中添加描述?
- 如何设置动态创建的CMFCToolbar的原始状态?
- 我可以动态创建新地图并作为函数参数传递吗?
- 如何在循环中动态创建变量(c++)
- 如何在C++中使用 new 运算符创建对动态创建的数组的引用?
- 在堆或堆栈上分配的动态创建的字符串 - C
- "动态创建的事件处理程序"复选框
- 在字符串数组中动态创建和存储数据
- 第一次尝试使用new动态创建结构数组,程序挂起没有错误
- 无法将动态创建的对象数组从 qml 发送到 c++ 作为方法参数
- 为动态创建的数组使用静态关键字有什么意义吗?
- 有没有办法基于文本文件动态创建对象?
- 从类 c++ 动态创建数组
- 如何在循环中动态创建类对象?
- 动态创建 std::vector 并将其传递给另一个函数的各种方法
- 使用 createComponent 动态创建 QML 对象