Boost Spirit语法用于识别从一行到向量的一系列数字
Boost Spirit syntax for recognising series of numbers from a line into vector?
我有一个字符串,有这样的味道:
SCALE FACTORS 16. 0.0 0.0 0.0 0.0 .
或
SCALE FACTORS 30. . . . . .
或
SCALE FACTORS 256. 10.0 20.0 30.0 .
所以,几个末尾有点的数字,只有点和空格。这是一些历史数据格式,我有太多的文件需要手动调整,以使其更易于阅读。
行首始终为"SCALE FACTORS",可作为固定模板"读取"。
我需要一个boost spirit表达式来帮助我在向量中识别这个字符串。单独的点必须被丢弃(或者至少被读为0)。数字必须以vector形式存储。
一行的有效位数在1到数之间(不固定)。我的主要问题是,如何在一个向量中存储一串数字。原则上,我可以在没有Boost Spirit的帮助下移除杂散点。
使用blank_type
船长,您可以
start = line % eol;
line = lit("SCALE") >> "FACTORS" >> *(double_|'.')
这将导致'.'
项为零:
Parsed 3 lines
[SCALE FACTORS] 16 0 0 0 0 0
[SCALE FACTORS] 30 0 0 0 0 0
[SCALE FACTORS] 256 10 20 30 0
Remaining input: '
'
查看Live On Coliru
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/format.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <cstdint>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef std::vector<double> line_t;
typedef std::vector<line_t> lines_t;
template <typename Iterator>
struct sfparser : public qi::grammar<Iterator, lines_t(), qi::blank_type>
{
sfparser () : sfparser::base_type(start)
{
using namespace qi;
start = line % eol;
line = lit("SCALE") >> "FACTORS" >> *(double_|'.');
}
private:
qi::rule<Iterator, lines_t(), qi::blank_type> start;
qi::rule<Iterator, line_t(), qi::blank_type > line;
};
int main()
{
std::string const input = " SCALE FACTORS 16. 0.0 0.0 0.0 0.0 .n"
"SCALE FACTORS 30. . . . . .n"
" SCALE FACTORS 256. 10.0 20.0 30.0 .n";
std::string::const_iterator f = input.begin();
std::string::const_iterator l = input.end();
sfparser<std::string::const_iterator> grammar;
lines_t data;
if (qi::phrase_parse (f, l, grammar, qi::blank, data))
{
std::cout << "Parsed " << data.size() << " linesn";
for (auto& line : data)
{
std::cout << "[SCALE FACTORS] ";
for (auto d : line)
std::cout << d << " ";
std::cout << "n";
}
}
else
{
std::cout << "Failedn";
}
if (f!=l)
std::cout << "Remaining input: '" << std::string(f,l) << "'n";
}
相关文章:
- 在一行中初始化和引入数组向量的正确方法?
- 我可以在一行代码中将向量中与条件匹配的所有元素插入到集合中吗?
- 在 2D 向量中从每一行添加最大元素,以使行元素 $A_i$ 小于 $A_(i+1)$
- OPENCV:将不同的向量附加为一行
- 根据某一行对整个 2D 向量进行排序
- 如何有效地从文件一行又一行地读取数据(相同类型)到多个向量(vectros的向量)
- 输出一行向量
- 如何将文本文件的每一行分配给新向量
- 一行 std::vector ctor 来自映射另一个向量
- 犰狳C ++:将矩阵的每一行乘以向量的高效简洁的方法
- 如何在一行中初始化 C++11 中的 n 个相等元素的向量
- 如何在只有一行输入的向量中输入多个输入
- 是否可以在一行中初始化一个值递增的向量
- cout 字符串和向量在同一行中
- 基于一行对 3D 向量进行排序(C++,std::sort)
- 一个向量中仅一行向量的迭代器
- Opencv-Mat向量赋值给一行矩阵,最快的方式
- 希望我的代码逐行将矩阵元素转换为向量,它只将矩阵的最后一行显示为向量
- 从文件中读取数据类型string, int double.将每一行数据放入对象向量中
- c++检查向量中一行中有多少相同的元素