namespace Eigen{
template
void write_binary(const char* filename, const Matrix& matrix){
std::ofstream out(filename, std::ios::out | std::ios::binary | std::ios::trunc);
typename Matrix::Index rows=matrix.rows(), cols=matrix.cols();
out.write((char*) (&rows), sizeof(typename Matrix::Index));
out.write((char*) (&cols), sizeof(typename Matrix::Index));
out.write((char*) matrix.data(), rowscolssizeof(typename Matrix::Scalar) );
out.close();
}
template
void read_binary(const char* filename, Matrix& matrix){
std::ifstream in(filename, std::ios::in | std::ios::binary);
typename Matrix::Index rows=0, cols=0;
in.read((char*) (&rows),sizeof(typename Matrix::Index));
in.read((char*) (&cols),sizeof(typename Matrix::Index));
matrix.resize(rows, cols);
in.read( (char ) matrix.data() , rowscols*sizeof(typename Matrix::Scalar) );
in.close();
}
} // Eigen::
eigen matrix 读写到文件
最新推荐文章于 2023-09-11 10:09:53 发布