我有一个数据文件,它是由一系列嵌套的双精度数组成的单行,例如。
[[0.127279,0.763675,0.636396],[0.254558,0.890955,0.636396],
[0.127279,0.636396,0.763675],[0.254558,0.763675,0.763675],
[0.381838,0.890955,0.763675],[0.127279,0.509117,0.890955],
[0.254558,0.636396,0.890955],[0.509117,0.890955,0.890955]]
我希望能够将其读入 STL vector >
使用流运算符,该运算符是在 A 的内部类型中模板化的:
vector<vector<double> > A;
FIN >> A;
我已经找到了一种在向量未嵌套时执行此操作的方法,即。一个简单的 vector
如下:
template <class T>
istream& operator>>(istream& s, vector<T> &A){
T x;
string token; char blank;
s >> blank; // Gobble the first '['
while( getline(s, token, ',') ) {
istringstream input(token);
input >> x;
A.push_back(x);
}
s >> blank; // Gobble the last ']'
return s;
}
但是我在使用 istream& 时遇到了问题。 operator>>(istream& s, vector >&A)
部分,因为我似乎无法正确捕获内部 ]
。我确信 Boost 有办法做到这一点,但我希望看到一个用于教学目的的 STL 解决方案。
注意:我知道重载 vector
的流运算符可能会产生深远的不良后果,并且该实现应该包含在它自己的类中 - 我正在使用上面的示例就这样澄清了这个问题。
编辑:
我希望该方法足够强大,能够处理输入数组,其大小(和内部数组)大小事先不知道,而是从读取流中推断出来。
I've got a data file that is a single line consisting of a nested series of doubles eg.
[[0.127279,0.763675,0.636396],[0.254558,0.890955,0.636396],
[0.127279,0.636396,0.763675],[0.254558,0.763675,0.763675],
[0.381838,0.890955,0.763675],[0.127279,0.509117,0.890955],
[0.254558,0.636396,0.890955],[0.509117,0.890955,0.890955]]
I'd like to be able to read this into a STL vector<vector<double> >
using the stream operator which is templated across the inner type of A:
vector<vector<double> > A;
FIN >> A;
I've figured out a way to do this when the vector is not nested, ie. a simple vector<T>
as so:
template <class T>
istream& operator>>(istream& s, vector<T> &A){
T x;
string token; char blank;
s >> blank; // Gobble the first '['
while( getline(s, token, ',') ) {
istringstream input(token);
input >> x;
A.push_back(x);
}
s >> blank; // Gobble the last ']'
return s;
}
But I'm having problem with the istream& operator>>(istream& s, vector<vector<T> >&A)
part as I can't seem to catch the inner ]
's properly. I'm sure that Boost has a way of doing this, but I'd like to see a solution with the STL for pedagogical purposes.
Note: I'm aware that overloading the stream operator for vector<T>
can have far-reaching undesirable consequences and that the implementation should be wrapped up in its own class - I'm using this example above as it stands to clarify the question.
EDIT:
I'd like the method to be robust enough to handle a input array whose size (and inner array) size is not known in advance, but inferred from reading the stream.
发布评论
评论(3)
实际上,您的代码的问题是您希望对两者使用相同的函数,当
T
为:vector
double
但是需要将数据读入vector和double的逻辑略有不同。所以你不能这样做,至少不能用那么简单的逻辑:
我宁愿编写两个函数来分别处理这两种情况。毕竟,即使在您的情况下,编译器也会为
T
的每个值生成两个不同的函数。测试代码:
输入:
输出:
在线演示: http://ideone.com/iBbmw
Actually, the problem with your code that you want to use the same function for both, when
T
is:vector<double>
double
But the logic which needs to read the data into vector and double is slightly different. So you cannot do that, at least not with that simple logic:
I would prefer to write two functions, to handle both cases separately. After all, even in your case, the compiler will generate two different functions for each value of
T
.Test code:
Input:
Output:
Online demo : http://ideone.com/iBbmw
在您的特定示例中,这非常简单。
现在,在读取三个双精度数后,您可以有一个简单的循环
和一些特殊的逻辑,将它们插入到一个新数组中。
In your particular example which is very simple.
Now you can have a a simple loop of
And some special logic after reading three doubles to insert them them into a new array.
几年后,我在这里为同样的问题而苦苦挣扎。
根据您的贡献,我开发了原始模板的修改版本。它能够解析多维数组,即使它们分布在几行中。
希望这对某人有用。
Some years later, and I was here struggling with the same problem.
Based on your contribution, I developed a modified version of the original template. This one is able to parse multidimensional arrays, even if they are spread across several lines.
Hope this can be useful for someone.