我的目的是将逗号分隔的值列表解析为嵌套向量。这个清单是二维的。基本问题是:
类似于" Traction":
下的表格'
' 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;
我已尝试过该声明的几种变体。来自this问题的%=
运算符也没有改变任何内容。虽然属性传播可能是正确的方向。
如果我按照提升文档示例"with style",结果完全相同:
vectorblock = doublevector % qi::eps;
使用push_back()
的List Redux示例:
vectorblock = doublevector[phoenix::push_back(qi::_val, qi::_1)] % qi::eps;
引发了一系列编译错误,从:
开始错误C2039:&#39; push_back&#39; :不是&#39; boost :: spirit :: unused_type&#39;
的成员
更新:问题发生在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"
"' RPM\n"
"'\n"
"0,5010,10000,15000,20000,25000\n"
"'\n"
"' Temp\n"
"'\n"
"-40.,0.,20.,40.\n"
"'\n"
"' Traction\n"
"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;
}
答案 0 :(得分:3)
答案是肯定的。
解析vector<vector<double> >
规则定义需要函数类型,而不是直接类型。这只是解释here。可以在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,