开发者

Convert a python numpy array to c++ stl vector

I'm looking for a way to read in c++ a text file containing numpy arrays and put the data into vector<开发者_开发知识库; vector< ... > > , can anyone help me out please ?

Thanks a lot. Archy

EDIT: format of the text file

[[[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9]] [[10 11] [12 13] [14 15] [16 17] [18 19]] [[20 21] [22 23] [24 25] [26 27] [28 29]] [[30 31] [32 33] [34 35] [36 37] [38 39]]]

Perhaps more readably:

[
    [
        [ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9]
    ]
    [
        [10 11] [12 13] [14 15] [16 17] [18 19]
    ]
    [
        [20 21] [22 23] [24 25] [26 27] [28 29]
    ]
    [
        [30 31] [32 33] [34 35] [36 37] [38 39]
    ]
]


float val;
::std::vector<float> vals;
ifstream stream("c:/file.txt");
while(stream >> val) {
   vals.push_back(val);
}


It's going to depend on your level of expertise.

If you are experienced, I would suggest something like Boost.Spirit.Qi, which is a true parser library. However it might take some time to get used to.

Otherwise it depends on what information you have at your disposal... I'll edit my answer when you provide us with more details since it's hairy enough to potentially get quite complicated :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜