【问题标题】:Read 2D vector from binary File C++从二进制文件 C++ 中读取 2D 矢量
【发布时间】:2017-03-01 20:34:25
【问题描述】:

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

例如:

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

243524
764756
746384

现在我想创建一个与 bin 文件完全相同的 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);
}

希望某人能帮助我:)

【问题讨论】:

  • 我的建议是使用单维向量,使其成为类的成员,并使用该类通过重载operator() 来假装具有二维的向量。这应该有助于加快阅读速度,因为您可以将整个文件读入单个向量。
  • @NathanOliver 所写内容的示例(没有vector,但很容易修改为使用vector)。
  • 对不起,我不明白..:( 我现在所做的是将 .bin 文件的所有元素存储在一个名为 Elements 的向量中。这个向量是一个类的一部分名称测试。我现在如何通过重载 operator() 创建 2dVector?你有可能有代码示例吗?抱歉我的愚蠢问题,但我从未使用过重载运算符:/

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


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的帮助,我真的很感激。但我还有另一个问题。使用这种方法,我正在创建一个向量并假装它是一个二维向量......因为我的问题是我的代码(最终结果)的主要目的应该是 6 个不同的向量。每个向量都包含二进制文件中的一列矩阵。而且我不确定我是否会用这种方法得到这个。你是否也知道我是如何意识到这一点的……对不起:/
  • 也许我在问题中解释错了我的意图.. 我创建了一个新问题,并给出了更好的解释。我没有删除这个问题,因为答案是一个很好的解释,也许其他人会需要它
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2011-05-08
  • 1970-01-01
  • 2017-10-01
  • 2011-09-03
相关资源
最近更新 更多