视觉读取来自二进制文件C 的2D向量

visual Read 2D vector from binary File C++

本文关键字:2D 向量 二进制文件 读取 视觉      更新时间:2023-10-16

我从二进制文件中读取2D向量的问题:

例如:

我的二进制文件是这样的:

243524
764756
746384

现在,我想创建一个看起来完全像bin文件的2D向量。

我到目前为止所做的:

我创建了一个1D向量,其中包含所有元素。然后我创建了一个循环并填充了2D向量。

我的问题是我有一个庞大的.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);
}

希望SB可以帮助我:)

建议:通过直接读取vector使用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;
}