gpt4 book ai didi

c++ - 用 C++ 解析

转载 作者:太空狗 更新时间:2023-10-29 23:42:16 25 4
gpt4 key购买 nike

解析文件最简单的方法是什么

[[1, 2, 3, 4], [8, 9, 10, 11], ...]

进入 QRectF 的 vector (具有四个 float 的结构)?

最佳答案

你看过boost的灵库吗?我认为这是一个了不起的库,如果我没记错的话,教程中的示例与您想要的非常相似。

编辑:这让您找到了正确的位置:http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/tutorials/warming_up.html

编辑:唉……我已经一年多没看过 c++(我也有 4 年多没看过 spirit 了)所以这花了大约一个小时来整理。这是工作示例:

# include <boost/spirit/include/qi.hpp>
using boost::spirit::qi::int_;
using boost::spirit::qi::char_;
using boost::spirit::qi::lit;
using boost::spirit::ascii::space;
using boost::spirit::ascii::space_type;
using boost::spirit::qi::rule;
using boost::spirit::qi::phrase_parse;

#include <boost/fusion/include/adapt_struct.hpp>

#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::vector;

struct holder {
int n1;
int n2;
int n3;
int n4;
};
BOOST_FUSION_ADAPT_STRUCT(::holder, (int,n1) (int,n2) (int, n3) (int, n4))

int main() {
string s = "[[1,2,3,4], [4,3,2,1]]";
vector<holder> v;

// I admit it, I was wrong. It's 3 lines of parsing.
rule<string::iterator, holder(), space_type> holder_p =
lit("[") >> int_ >> ',' >> int_ >> ',' >> int_ >> ',' >> int_ >> ']';
rule<string::iterator, vector<holder>(), space_type > holder_list_p =
char('[') >> (holder_p % ',') >> ']';
bool r = phrase_parse(s.begin(), s.end(), holder_list_p, space, v);

if (r) {
for (vector<holder>::const_iterator it = v.begin(); it != v.end(); it++) {
cout << "n1: " << it->n1 << ", n2: " << it->n2 <<
", n3: " << it->n3 << ", n4: " << it->n4 << endl;
}
}
return 0;
}

关于c++ - 用 C++ 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5162922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com