我有一个格式为“$number_of_elements $e1 $e2 $e3”的文件。我创建了以下解析器:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
int main(int argc, char *argv[])
{
std::string input("2 3 3\n");
using boost::phoenix::at_c;
using boost::spirit::qi::_1;
using boost::spirit::qi::_r1;
using boost::spirit::qi::double_;
using boost::spirit::qi::omit;
using boost::spirit::qi::int_;
using boost::spirit::qi::repeat;
using boost::spirit::qi::rule;
using boost::spirit::qi::space;
using boost::spirit::qi::space_type;
using boost::spirit::qi::_val;
rule<std::string::iterator, double(), space_type> r0 = double_;
r0.name("r0");
rule<std::string::iterator, std::vector<double>(size_t), space_type> r1 = repeat(_r1)[r0];
r1.name("r1");
rule<std::string::iterator, boost::tuple<size_t, std::vector<double> >(), space_type> r2
= int_ >> r1(at_c<0>(_val));
r2.name("r2");
rule<std::string::iterator, std::vector<double>(), space_type> r3
= r2[_val = at_c<1>(_1)];
r3.name("r3");
debug(r0);
debug(r1);
debug(r2);
debug(r3);
std::vector<double> res;
bool success = boost::spirit::qi::phrase_parse(input.begin(),
input.end(),
r3,
space,
res);
if (success) {
for(std::vector<double>::iterator it = res.begin(); it != res.end(); it++) {
std::cout << *it << " " << std::endl;
}
}
return !success;
}
我想知道是否有机会避免复制。我不知道 boost phoenix (或 gcc )说编译器是否会应用临时对象删除优化(数据量可能非常大,因此可能会影响性能)。
另外 - 是否可以将r1
规则更改为类似的内容(除非通过重复这样做):
rule<std::string::iterator, std::vector<double>(size_t), space_type> r1
= eps[_val.reserve(_r1)] >> repeat(_r1)[r0];
(此行不编译)。
附言。存储的数量可能非常大,因此虽然此时复制/重新分配可能会产生一些影响并不重要 - 但是我希望在完全致力于设计之前知道这种优化涉及什么。
聚苯乙烯。我正在使用 gcc 4.4,所以我可以访问std::move
但没有很多其他 C++11 功能。