解析为向量<向量<double>>与 boost::spirit
Parse into a vector<vector<double>> with boost::spirit
我的意图是将逗号分隔的值列表解析为嵌套向量。这个列表是二维的。基本问题是:
有可能用boost::spirit解析成向量的向量吗
类似于"牵引"下的表格:
'
' RPM
0,5000,10000,15000,20000,25000
'
' Temp
'
-40.,0.,20.,40.
'
' Traction
200.,175.,170.,165.,160.,150.
200.,175.,170.,165.,160.,150.
165.,165.,160.,155.,145.,145.
160.,155.,150.,145.,145.,140.
'
在下一步中,我想读入4维数据,但目前我正在为第二维数据而挣扎。数据结构如下:
struct table {
std::vector<double> index;
std::vector<double> index2;
std::vector<std::vector<double> > base;
};
语法非常简单,如下所示:
comment %= qi::lexeme[ ''' >> *(qi::standard::char_ - qi::eol)] >> qi::eol;
commentblock = comment >> *(comment);
doublevector = qi::double_ % ',' >> qi::eol ;
vectorblock = *doublevector;
start = commentblock >>
doublevector >>
commentblock >>
doublevector >>
commentblock >>
vectorblock >>
commentblock >>
qi::eoi
;
到目前为止,我解析两个向量index
和index2
没有问题。但问题开始于base
。我认为关键部分是我定义vectorblock
的地方:
vectorblock = *doublevector;
我已经尝试过这种说法的几种变体。同样,这个问题中的%=
运算符没有改变任何内容。尽管属性传播可能是正确的方向。
如果我遵循boost文档中的"with style"示例,结果完全相同:
vectorblock = doublevector % qi::eps;
使用push_back()
的List Redux示例:
vectorblock = doublevector[phoenix::push_back(qi::_val, qi::_1)] % qi::eps;
引发大量编译错误,从开始
错误C2039:"push_back":不是"boost::spirit::unused_type"的成员
更新:问题出现在vectorblock
的声明上。我忘记了属性类型后面的()
。因此,定义应该是这样的:
qi::rule<Iterator, std::vector<std::vector<double> >(), Skipper> vectorblock;
(更新的)工作示例如下:
#include <iostream>
#include <string>
#include <vector>
#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted.hpp>
struct table {
std::vector<double> index;
std::vector<double> index2;
std::vector<std::vector<double> > base;
};
BOOST_FUSION_ADAPT_STRUCT(
table,
(std::vector<double> , index)
(std::vector<double> , index2)
(std::vector<std::vector<double> >, base)
)
const std::string contents =
"'n"
"' RPMn"
"'n"
"0,5010,10000,15000,20000,25000n"
"'n"
"' Tempn"
"'n"
"-40.,0.,20.,40.n"
"'n"
"' Tractionn"
"200.,175.,170.,165.,160.,150.n"
"200.,175.,170.,165.,160.,150.n"
"165.,165.,160.,155.,145.,145.n"
"160.,155.,150.,145.,145.,140.n"
"'n"
;
int main()
{
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
typedef std::string::const_iterator Iterator;
typedef boost::spirit::ascii::blank_type Skipper;
qi::rule<Iterator, std::string(), Skipper> comment;
qi::rule<Iterator, Skipper> commentblock;
qi::rule<Iterator, std::vector<double>(), Skipper> doublevector;
qi::rule<Iterator, std::vector<std::vector<double> >, Skipper> vectorblock;
qi::rule<Iterator, table(), Skipper> start;
comment %= qi::lexeme[ ''' >> *(qi::standard::char_ - qi::eol)] >> qi::eol;
commentblock = comment >> *(comment);
doublevector = qi::double_ % ',' >> qi::eol ;
vectorblock = *doublevector;
start = commentblock >>
doublevector >>
commentblock >>
doublevector >>
commentblock >>
vectorblock >>
commentblock >>
qi::eoi
;
BOOST_SPIRIT_DEBUG_NODES((start)(doublevector)(vectorblock));
table tref;
bool rv = qi::phrase_parse(
std::begin(contents), std::end(contents),
start,
boost::spirit::ascii::blank,
tref
);
std::cout << "parse " << ((char *)rv?"success":"failure") << ".n";
for (auto i : tref.index)
std::cout << i << ", ";
std::cout << "n";
for (auto i : tref.index2)
std::cout << i << ", ";
std::cout << "nBase:n";
for (auto & i : tref.base)
{
for(auto & j : i)
std::cout << j << ", ";
std::cout << "n";
}
std::cout << std::endl;
}
答案是肯定的。实际上,解析成vector<vector<double> >
是非常琐碎的
规则定义需要一个函数类型,而不是直接使用该类型。这里简单解释一下。更彻底的解释可能在boost::phoenix 的文档中找到
上面程序的输出现在很好地显示了解析后的值:
parse success.
0, 5011, 10000, 15000, 20000, 25000,
-40, 0, 20, 40,
Base:
200, 175, 170, 165, 160, 150,
200, 175, 170, 165, 160, 150,
165, 165, 160, 155, 145, 145,
160, 155, 150, 145, 145, 140,
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中