从二进制文件C ++中读取2D向量

时间:2017-03-01 20:34:26

标签: c++ visual-c++ vector binary

我在从二进制文件中读取2D矢量时遇到了一些问题:

例如:

我的二进制文件结构如下:

243524
764756
746384

现在我想创建一个看起来与bin文件完全相同的2D Vector。

我迄今为止做了什么:

我创建了一个包含所有元素的1D Vector。然后我创建了一个循环并填充了2D Vector。

我的问题是我有一个巨大的.bin文件和for循环花了很多时间。 是否有可能更快地获得2DVector?

我的代码:

ifstream file("C:\\XXX.bin", ios::in | ios::binary | ios::ate);
char * buffer;
long size;
file.seekg(0, std::ios::end); 
size = file.tellg();  

buffer = new char[size];
file.read(buffer, size);
file.close();   


double* double_values = (double*)buffer;//reinterpret as doubles
vector<double> buffer2(double_values, double_values + (size / sizeof(double)));

//cout << "Filling matrix with test numbers.";
int h = 0;
for (int i = 0; i < (size / sizeof(double)) / row; i++)
{
    vector<double> temp;
    for (int j = 0; j < row; j++)
    {
        if (h<size / sizeof(double))
        {
            temp.push_back(buffer2[h]);
            h++;
        }

    }

    bin_file.push_back(temp);
}

希望某人可以帮助我:)

1 个答案:

答案 0 :(得分:0)

建议:直接阅读into the vector with std::vector::data

消除复制
ifstream file("C:\\XXX.bin", ios::in | ios::binary | ios::ate);
char * buffer;
long size;
file.seekg(0, std::ios::end); 
size = file.tellg();  
//allocate a properly sized vector of doubles. Probably should test that size 
// is evenly divisible
vector<double> buffer(size/sizeof(double));
// read into the vector of doubles by lying and saying it's an array of char 
file.read((char*)buffer.data(), size);
file.close();   

请注意,矢量是1D。访问此向量需要一些数学。你可以

buffer[map2dto1d(row, column)];

,其中

size_t map2dto1d(size_t row, size_t column)
{
    return row * numberColumns + column;
}

但最好将vector包装在一个类中,该类存储行和列信息以及大小合适的vector并强制用户正确索引。

这样的事情:

class Matrix
{
private:
    size_t rows, columns;
    std::vector<double> matrix;
public:
    // zero initialized matrix of row by column
    Matrix(size_t numrows, size_t numcols):
        rows(numrows), columns(numcols), matrix(rows * columns)
    {
         // can place file reading logic here, but make sure the file matches 
         // numrows * numcols when reading and throw an exception if it doesn't
    }

    // matrix of row by column using provided in vector. in will be 
    // consumed by this constructor 
    // make sure in is big enough to support rows * columns
    Matrix(size_t numrows, size_t numcols, std::vector<double> && in):
        rows(numrows), columns(numcols), matrix(std::move(in))
    {

    }

    double & operator()(size_t row, size_t column)
    {
        // check bounds here if you care
        return matrix[row * columns + column];
    }

    double operator()(size_t row, size_t column) const
    {
        // check bounds here if you care
        return matrix[row * columns + column];
    }

    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }

};
// convenience function for printing matrix
std::ostream & operator<<(std::ostream &  out, const Matrix & in)
{
    for (int i = 0; i < in.getRows(); i++)
    {
        for (int j = 0; j < in.getColumns(); j++)
        {
            out << in(i,j) << ' ';
        }
        out << std::endl;
    }

    return out;
}