使用spirit::karma从元组向量中重新排序元组
#include <tuple>
#include <vector>
#include <string>
#include <iostream>
//-------------------------------------------------------------------------
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
//-------------------------------------------------------------------------
namespace ph = boost::phoenix;
namespace karma = boost::spirit::karma;
typedef std::back_insert_iterator<std::string> Sink;
typedef std::tuple<double,int> Data;
typedef std::vector<Data> Container;
struct Generator : karma::grammar<Sink,Container()>
{
Generator(void) : Generator::base_type(start,"Generator")
{
start = data % karma::eol;
//data = karma::delimit[???];
return;
}
karma::rule<Sink,Container()> start;
karma::rule<Sink,Data()> data;
};
//-------------------------------------------------------------------------
int main(int argc,char** argv)
{
Generator generator;
Container container;
container.push_back(Data(3.1415,100500));
container.push_back(Data(2.7183,9000));
std::string result;
Sink sink(result);
bool b = boost::spirit::karma::generate(sink,generator,container);
std::cerr << (b == true ? result : std::string("Error!")) << std::endl;
return 0;
}
在规则 data 中(例如),我需要在 double 之前生成 int 并用它进行算术运算。如何在 data 规则的语义操作中访问合成属性(元组)的元素?
#include <tuple>
#include <vector>
#include <string>
#include <iostream>
//-------------------------------------------------------------------------
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
//-------------------------------------------------------------------------
namespace ph = boost::phoenix;
namespace karma = boost::spirit::karma;
typedef std::back_insert_iterator<std::string> Sink;
typedef std::tuple<double,int> Data;
typedef std::vector<Data> Container;
struct Generator : karma::grammar<Sink,Container()>
{
Generator(void) : Generator::base_type(start,"Generator")
{
start = data % karma::eol;
//data = karma::delimit[???];
return;
}
karma::rule<Sink,Container()> start;
karma::rule<Sink,Data()> data;
};
//-------------------------------------------------------------------------
int main(int argc,char** argv)
{
Generator generator;
Container container;
container.push_back(Data(3.1415,100500));
container.push_back(Data(2.7183,9000));
std::string result;
Sink sink(result);
bool b = boost::spirit::karma::generate(sink,generator,container);
std::cerr << (b == true ? result : std::string("Error!")) << std::endl;
return 0;
}
in rule data (as example) I need generate int before double and make with it arithmetical operation. How can I get access to elements of synthesized attribute (tuple) in semantic actions of data rule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在能想到的最快解决方案很简单:
因此,完整的示例如下所示:
输出:
The quickest solution I can come up with at this instant is simply:
So, a full sample would look like:
Output: