C++ 解析双线程的普通python列表

C++ 解析双线程的普通python列表,c++,string,parsing,tokenize,C++,String,Parsing,Tokenize,解析这样一行的最佳方法是什么(元素的数量不是固定的): 获取C++中的 STD::双< /代码>的向量我已经做到了: vector<double> read_line(string line) { vector<double> coefficients_line; // erase all before [ and all after ] size_t found1 = line.find("["); if (found1 == strin

解析这样一行的最佳方法是什么(元素的数量不是固定的):

获取C++中的<代码> STD::<代码>双< /代码>的向量<代码>我已经做到了:

vector<double> read_line(string line)
{
    vector<double> coefficients_line;
    // erase all before [ and all after ]
    size_t found1 = line.find("[");
    if (found1 == string::npos) cerr << "line not valid: " << line;
    line.erase(line.begin(), line.begin() + found1 + 1);
    size_t found2 = line.find("]");
    if (found2 == string::npos) cerr << "line not valid: " << line;
    line.erase(line.begin() + found2, line.end());

    vector<string> coefficients_string;
    boost::split(coefficients_string, line, boost::is_any_of(","));
    for (vector<string>::const_iterator ic=coefficients_string.begin();
     ic!=coefficients_string.end(); ++ic)
    {
      cout << "c string \"" << *ic << "\"" << endl;
      string s = *ic;
      boost::trim(s);
      double c = boost::lexical_cast<double>(s);
      cout << "c double: " << c << endl;
      coefficients.push_back(c);
   }
   return coefficients;
}
矢量读取线(字符串线)
{
矢量系数_线;
//删除所有之前[和之后]
size\u t found1=line.find(“[”);

如果(found1==string::npos)cerr我发现您已经在使用
boost
。您应该为此目的尝试
boost.spirit.qi

#include <vector>
#include <string>
#include <iostream>

#include <boost/spirit/include/qi.hpp>

namespace qi = ::boost::spirit::qi;

template <typename Iterator>
bool parse_numbers(Iterator & first, Iterator last, std::vector<double> & v)
{
    using qi::double_;
    using qi::phrase_parse;
    using qi::_1;
    using boost::spirit::ascii::space;

    return phrase_parse(first, last, ('[' >> double_ % ',' >> ']'), space, v);
}

int main()
{
    std::string s = "[  0.0125,  2.9518e+02,  1.2833e+00,  -3.5302e-04,  1.2095e+01,  1.0858e-01,  1.2112e-04,  1.1276e+03  ] # comments";
    std::vector<double> v;
    std::string::iterator sb = s.begin();
    parse_numbers(sb, s.end(), v);

    std::cout << "Parsed numbers:" << std::endl;
    for (int i = 0; i < v.size(); ++i) std::cout << v[i] << std::endl;
    std::cout << "Rest of line:" << std::endl;
    std::cout << std::string(sb, s.end()) << std::endl;
}
#包括
#包括
#包括
#包括
名称空间qi=::boost::spirit::qi;
模板
布尔解析_数(迭代器&first、迭代器last、std::vector&v)
{
使用qi::double;
使用qi::短语解析;
使用气::_1;
使用boost::spirit::ascii::space;
返回短语_parse(first,last,('['>>double.%','>>']'),空格,v);
}
int main()
{
标准::字符串s=“[0.0125,2.9518e+02,1.2833e+00,-3.5302e-04,1.2095e+01,1.0858e-01,1.2112e-04,1.1276e+03]#注释”;
std::向量v;
std::string::迭代器sb=s.begin();
解析_数(sb,s.end(),v);

给定格式,我认为使用IO流足够简单

#include <iostream>
#include <sstream>
#include <vector>

int main() {
  std::istringstream line("[ 1.23, 1.24e+3, 3, 1.44e-2 ]");

  char c;
  while ((line >> c) && c != '[');
  if (!line) { return 1; }

  std::vector<double> v;

  double d;
  while ((line >> d)) { v.push_back(d); line >> c; if (c != ',') { break; } }

  for (std::vector<double>::const_iterator i = v.begin(), e = v.end();
       i != e; ++i)
  {
    std::cout << *i << "\n";
  }

  return 0;
} 
(在行动中)


它不是一个完整的解析器,将接受不正确的输入(值得注意的是,它不会在最后检查
]
。但我想说它已经相当不错了。

如果您的编译器支持C++0x,那么您可以使用AXE来解析此字符串:

std::vector<double> read_line(std::string line)
{
    std::vector<double> v;
    auto spaces = *axe::r_any(" \t\n\r");
    double d;
    auto double_rule = spaces & axe::r_double(d) 
        >> axe::e_ref([](...){ v.push_back(d); });
    auto array_rule = '[' & double_rule % ',' & ']';
    array_rule(line.begin(), line.end());
    return v;
}
std::vector read_行(std::string行)
{
std::向量v;
自动空格=*axe::r_any(“\t\n\r”);
双d;
自动双精度规则=空格和斧头::r双精度(d)
>>斧头:e_ref([](…){v.push_back(d);});
自动数组_规则='['&双_规则%','&'];
数组_规则(line.begin(),line.end());
返回v;
}

另外,我还没有测试过它,所以表面错误是可能的。

谢谢,你的解决方案非常干净。问题是在我们的集群上
错误:boost/spirit/include/qi.hpp:没有这样的文件或目录
,我们有严格的添加软件的政策。我们安装了
spirit
库,但没有
qi
@wiso:啊,这就是可能是旧的
spirit
,在被拆分为
qi
lex
karma
(抱歉,我不太熟悉)。但无论如何,
spirit
只是标题!您可以将其标题复制到源代码树:)
1.23
1240
3
0.0144
std::vector<double> read_line(std::string line)
{
    std::vector<double> v;
    auto spaces = *axe::r_any(" \t\n\r");
    double d;
    auto double_rule = spaces & axe::r_double(d) 
        >> axe::e_ref([](...){ v.push_back(d); });
    auto array_rule = '[' & double_rule % ',' & ']';
    array_rule(line.begin(), line.end());
    return v;
}