视觉读取来自二进制文件C 的2D向量
visual Read 2D vector from binary File C++
我从二进制文件中读取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;
}
相关文章:
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 哪个更快:在 1d 向量中按字符串搜索还是在 2d 向量中按向量搜索?
- 分配具有 2D 向量大小的变量的内存
- 在统一内存 CUDA C/C++ 中分配 2D 向量
- 索引 2D 向量时'Reference to type const..'编译错误
- 查找 2D 向量的位置,该向量是类C++的一个元素
- 如何在 C++ 中的函数中传递 2D 向量的子向量
- 为 2D 向量类创建标量乘法运算符
- 无法使用push_back将整数插入1D/2D向量
- 如何将列附加到现代C 中的2D向量
- parallel_for lambda 函数填充 2D 向量后出错
- 类对象指针C 的2D向量
- 将1D或2D向量传递给同一类的构造函数
- 如何初始化2D向量C
- 在类的构造函数中初始化 2D 向量
- 对通过引用传递的 2d 向量进行排序
- 如何在C 2D向量中找到对象(而不是int)的位置
- 如何在C 中打印一个对象的2D向量
- 双打分类/编辑问题的2D向量