解析这样一行的最佳方法是什么(元素数量不固定):
[ 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 ++中获取std::vector
double
?我做到了这一点:
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;
}
不鼓励,但欢迎简单的解决方案
答案 0 :(得分:2)
我发现你已经在使用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;
}
我从parse_numbers()
的文档中获取了spirit
函数并稍微调整了一下。当解析失败时(即不是格式良好的列表),它返回 false ,但当行在列表后面有任何文本时返回 true :第一个迭代器({{ 1}} sb
}将指向数字列表的结束位置。
请参阅此处的完整文档:
http://www.boost.org/doc/libs/1_46_1/libs/spirit/doc/html/spirit/qi.html
答案 1 :(得分:2)
鉴于格式,我认为使用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行动中)
它不是一个完整的解析器,并且会接受不正确的输入(特别是,它不会在最后检查]
。但我会说它已经相当不错了。
答案 2 :(得分:0)
如果您的编译器支持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;
}
P.S。我没有测试过,所以表面上的错误是可能的。