gpt4 book ai didi

c++ - 从二进制文件 C++ 中读取二维 vector

转载 作者:行者123 更新时间:2023-11-28 05:16:30 24 4
gpt4 key购买 nike

我在从二进制文件中读取二维 vector 时遇到了一些问题:

例如:

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

243524
764756
746384

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

到目前为止我做了什么:

我创建了一个包含所有元素的一维 vector 。然后我创建了一个 Loop 并填充了 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);
}

希望某人能帮助我:)

最佳答案

建议:直接阅读杜绝抄袭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();

注意 vector 是一维的。访问这个 vector 需要一些数学知识。你可以

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;
}

关于c++ - 从二进制文件 C++ 中读取二维 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42541533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com