解析普通的Python双精度列表

发布于 2024-11-18 14:35:50 字数 1308 浏览 0 评论 0 原文

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

[  0.0125,  2.9518e+02,  1.2833e+00,  -3.5302e-04,  1.2095e+01,  1.0858e-01,  1.2112e-04,  1.1276e+03  ] # comments

在 C++ 中获取 doublestd::vector ?我已经这样做了:

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;
}

非提升,但欢迎简单的解决方案

What's the best way to parse a line like this (the number of elements is not fixed):

[  0.0125,  2.9518e+02,  1.2833e+00,  -3.5302e-04,  1.2095e+01,  1.0858e-01,  1.2112e-04,  1.1276e+03  ] # comments

to obtain a std::vector of double in c++? I've done this:

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;
}

non-boost, but easy solutions are welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2024-11-25 14:35:50

考虑到格式,我认为使用 IO Streams 就足够简单了。

#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;
} 

打印:(

1.23
1240
3
0.0144

ideone 上运行)

它不是一个完整的解析器,并且会接受不正确的输入(值得注意的是,它但我想说它已经相当不错了。

Given the format, I would think it is simple enough to use IO Streams.

#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;
} 

Prints:

1.23
1240
3
0.0144

(In action at ideone)

It is not a full blown parser, and will accept incorrect inputs (notably, it does not check for the ] at the end. But I would say it's pretty decent already.

忱杏 2024-11-25 14:35:50

我发现您已经在使用 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;
}

我从 spirit 的文档中获取了 parse_numbers() 函数,并对其进行了一些修改。当解析失败时(即不是格式良好的列表),它返回false,但当该行在列表之后有任何文本时返回true:第一个迭代器(main() 中的 >sb) 将指向号码列表结束的位置。

请在此处查看完整文档:
http://www.boost.org /doc/libs/1_46_1/libs/spirit/doc/html/spirit/qi.html

I see you're already using boost. You should definitely try boost.spirit.qi for that purpose.

#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;
}

I've taken the parse_numbers() function from spirit's documentation and adapted it a bit. It returns false when parsing has failed (i.e. not a well-formed list), but returns true when the line has any text after the list: the first iterator (sb in main()) will point to where the number list has ended.

See the full docs here:
http://www.boost.org/doc/libs/1_46_1/libs/spirit/doc/html/spirit/qi.html

静待花开 2024-11-25 14:35:50

如果你的编译器支持C++0x,那么你可以使用AX来解析这个字符串:

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;
}

PS我还没有测试过它,所以可能会出现表面错误。

If your compiler supports C++0x, then you can use AXE to parse this string:

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;
}

P.S. I haven't tested it, so superficial errors are possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文