解析文件的最简单方法是什么?
[[1, 2, 3, 4], [8, 9, 10, 11], ...]
Run Code Online (Sandbox Code Playgroud)
到QRectF的矢量(一个有四个浮点数的结构)?
你看过boost的精神图书馆吗?我认为这是一个了不起的库,如果我没记错的话,他们在教程中的示例与您想要的非常相似.
编辑:这使您在正确的位置:http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/tutorials/warming_up.html
编辑:叹息......我在一年多的时间里没有看过c ++(我在4年多的时间里没有看过精神)所以这花了大约一个小时才能完成.这是工作示例:
# 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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
351 次 |
最近记录: |