Ask Your Question
1

how to write two dimensional vector to file

asked 2015-08-12 09:45:33 -0500

fnhdx gravatar image

updated 2015-11-19 08:28:50 -0500

I have two dimensional vector vector<vector<int> > test; I want to save it to file and read it back. I tried to write it to a FileStorage, but after I read it back, it became one dimension vector. Does anybody have an idea? Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-08-12 11:04:27 -0500

theodore gravatar image

here you are:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

// save on file
void writeVectorOfVector(FileStorage &fs, string name, vector<vector<int>> &data)
{
    fs << name;
    fs << "{";
    for (int i = 0; i < data.size(); i++)
    {
        fs << name + "_" + to_string(i);
        vector<int> tmp = data[i];
        fs << tmp;
    }
    fs << "}";
}

// read from file
void readVectorOfVector(FileStorage &fns, string name, vector<vector<int>> &data)
{
    data.clear();
    FileNode fn = fns[name];
    if (fn.empty()){
        return;
    }

    FileNodeIterator current = fn.begin(), it_end = fn.end();
    for (; current != it_end; ++current)
    {
        vector<int> tmp;
        FileNode item = *current;
        item >> tmp;
        data.push_back(tmp);
    }
}


int main()
{
    vector<vector<int> > test(4, std::vector<int>(3, 5));

    FileStorage fs("test.yml", FileStorage::WRITE);;
    writeVectorOfVector(fs, "values", test);
    fs.release();

    vector<vector<int> > tset;
    FileStorage fs2("test.yml", FileStorage::READ);
    readVectorOfVector(fs2, "values", tset);
    fs2.release();

    for(size_t i = 0; i < tset.size(); ++i)
    {
        for(size_t j = 0; j < tset[i].size(); ++j)
            cout << tset[i][j] << " ";

        cout << endl << endl;
    }

    return 0;
}
edit flag offensive delete link more

Comments

This is exactly what I want. Thanks a lot!

fnhdx gravatar imagefnhdx ( 2015-08-12 14:04:42 -0500 )edit

you are welcome ;-)

theodore gravatar imagetheodore ( 2015-08-12 14:19:42 -0500 )edit

@theodore I noticed that loading YML file is very slow. My file is about 180000*10 int (<6Mb on disk). It takes less than 1 sec to write to disk, but more than 20sec to load from disk. Any idea about that?

fnhdx gravatar imagefnhdx ( 2015-08-14 14:05:22 -0500 )edit

regarding the use of FileStorage I do not think you can do something, since I guess it is already well tuned. However, making a small search I saw here that it might be better to save you data in binary form using the STL functionality of ofstream/ifstream. But yes I think that it is a question about what you do prefer, the easiness of yml or the speed of binary files (plus here you might have the issues that it is described regarding different architecture systems).

theodore gravatar imagetheodore ( 2015-08-14 15:01:51 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-08-12 09:45:33 -0500

Seen: 1,851 times

Last updated: Aug 12 '15