将对象保存到二进制文件中的有效方法
efficent way to save objects into binary files
我有一个类,它基本上由一个向量矩阵组成:vector< MyFeatVector<T> > m_vCells
,其中外部向量表示矩阵。然后,此矩阵中的每个元素都是一个vector
(我扩展了stl vector
类并将其命名为MyFeatVector<T>
(。
我正在尝试编写一种有效的方法来将此类的对象存储在二进制文件中。到目前为止,我需要三个嵌套循环:
foutput.write( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) );
其中this->at(dy,dx,dz)
检索位置 [dy,dx]
处向量的dz
元素。
是否可以在不使用循环的情况下存储m_vCells
私有成员?我尝试了类似的东西:foutput.write(reinterpret_cast<char*>(&(this->m_vCells[0])), (this->m_vCells.size())*sizeof(CFeatureVector<T>));
似乎无法正常工作。我们可以假设这个矩阵中的所有向量都具有相同的大小,尽管也欢迎更通用的解决方案:-(
此外,在我的嵌套循环实现之后,将此类的对象存储在二进制文件中似乎比在纯文本文件中存储相同的对象需要更多的物理空间。这有点奇怪。
我试图遵循 http://forum.allaboutcircuits.com/showthread.php?t=16465 下的建议,但无法找到适当的解决方案。
谢谢!
下面是我的serialization
和unserialization
方法的简化示例。
template < typename T >
bool MyFeatMatrix<T>::writeBinary( const string & ofile ){
ofstream foutput(ofile.c_str(), ios::out|ios::binary);
foutput.write(reinterpret_cast<char*>(&this->m_nHeight), sizeof(int));
foutput.write(reinterpret_cast<char*>(&this->m_nWidth), sizeof(int));
foutput.write(reinterpret_cast<char*>(&this->m_nDepth), sizeof(int));
//foutput.write(reinterpret_cast<char*>(&(this->m_vCells[0])), nSze*sizeof(CFeatureVector<T>));
for(register int dy=0; dy < this->m_nHeight; dy++){
for(register int dx=0; dx < this->m_nWidth; dx++){
for(register int dz=0; dz < this->m_nDepth; dz++){
foutput.write( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) );
}
}
}
foutput.close();
return true;
}
template < typename T >
bool MyFeatMatrix<T>::readBinary( const string & ifile ){
ifstream finput(ifile.c_str(), ios::in|ios::binary);
int nHeight, nWidth, nDepth;
finput.read(reinterpret_cast<char*>(&nHeight), sizeof(int));
finput.read(reinterpret_cast<char*>(&nWidth), sizeof(int));
finput.read(reinterpret_cast<char*>(&nDepth), sizeof(int));
this->resize(nHeight, nWidth, nDepth);
for(register int dy=0; dy < this->m_nHeight; dy++){
for(register int dx=0; dx < this->m_nWidth; dx++){
for(register int dz=0; dz < this->m_nDepth; dz++){
finput.read( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) );
}
}
}
finput.close();
return true;
}
最有效的方法是将对象存储到数组(或连续空间(中,然后将缓冲区爆炸到文件中。 一个优点是磁盘盘片不会浪费时间,而且写入可以连续执行,而不是在随机位置执行。
如果这是性能瓶颈,您可能需要考虑使用多个线程,一个额外的线程来处理输出。 将对象转储到缓冲区中,设置一个标志,然后写入线程将处理输出,重新离开主任务以执行更重要的任务。
编辑 1:序列化示例
以下代码尚未编译,仅用于说明目的。
#include <fstream>
#include <algorithm>
using std::ofstream;
using std::fill;
class binary_stream_interface
{
virtual void load_from_buffer(const unsigned char *& buf_ptr) = 0;
virtual size_t size_on_stream(void) const = 0;
virtual void store_to_buffer(unsigned char *& buf_ptr) const = 0;
};
struct Pet
: public binary_stream_interface,
max_name_length(32)
{
std::string name;
unsigned int age;
const unsigned int max_name_length;
void load_from_buffer(const unsigned char *& buf_ptr)
{
age = *((unsigned int *) buf_ptr);
buf_ptr += sizeof(unsigned int);
name = std::string((char *) buf_ptr);
buf_ptr += max_name_length;
return;
}
size_t size_on_stream(void) const
{
return sizeof(unsigned int) + max_name_length;
}
void store_to_buffer(unsigned char *& buf_ptr) const
{
*((unsigned int *) buf_ptr) = age;
buf_ptr += sizeof(unsigned int);
std::fill(buf_ptr, 0, max_name_length);
strncpy((char *) buf_ptr, name.c_str(), max_name_length);
buf_ptr += max_name_length;
return;
}
};
int main(void)
{
Pet dog;
dog.name = "Fido";
dog.age = 5;
ofstream data_file("pet_data.bin", std::ios::binary);
// Determine size of buffer
size_t buffer_size = dog.size_on_stream();
// Allocate the buffer
unsigned char * buffer = new unsigned char [buffer_size];
unsigned char * buf_ptr = buffer;
// Write / store the object into the buffer.
dog.store_to_buffer(buf_ptr);
// Write the buffer to the file / stream.
data_file.write((char *) buffer, buffer_size);
data_file.close();
delete [] buffer;
return 0;
}
编辑 2:具有字符串向量的类
class Many_Strings
: public binary_stream_interface
{
enum {MAX_STRING_SIZE = 32};
size_t size_on_stream(void) const
{
return m_string_container.size() * MAX_STRING_SIZE // Total size of strings.
+ sizeof(size_t); // with room for the quantity variable.
}
void store_to_buffer(unsigned char *& buf_ptr) const
{
// Treat the vector<string> as a variable length field.
// Store the quantity of strings into the buffer,
// followed by the content.
size_t string_quantity = m_string_container.size();
*((size_t *) buf_ptr) = string_quantity;
buf_ptr += sizeof(size_t);
for (size_t i = 0; i < string_quantity; ++i)
{
// Each string is a fixed length field.
// Pad with ' ' first, then copy the data.
std::fill((char *)buf_ptr, 0, MAX_STRING_SIZE);
strncpy(buf_ptr, m_string_container[i].c_str(), MAX_STRING_SIZE);
buf_ptr += MAX_STRING_SIZE;
}
}
void load_from_buffer(const unsigned char *& buf_ptr)
{
// The actual coding is left as an exercise for the reader.
// Psuedo code:
// Clear / empty the string container.
// load the quantity variable.
// increment the buffer variable by the size of the quantity variable.
// for each new string (up to the quantity just read)
// load a temporary string from the buffer via buffer pointer.
// push the temporary string into the vector
// increment the buffer pointer by the MAX_STRING_SIZE.
// end-for
}
std::vector<std::string> m_string_container;
};
我建议您阅读有关序列化C++常见问题解答,您可以选择最适合您的
当你使用结构和类时,你必须处理两件事
- 类内的指针
- 填充字节
这两者都可能会在您的输出中产生一些臭名昭著的结果。IMO,对象必须实现以序列化和反序列化对象。对象可以很好地了解结构,指针数据等。因此,它可以决定哪种格式可以有效实施。
无论如何,您都必须迭代或必须将其包装在某个地方。完成序列化和反序列化函数的实现后(可以使用运算符或函数编写(。尤其是在处理流对象时,重载<<运算符和>>运算符很容易传递对象。
关于您关于使用矢量底层指针的问题,如果它是单个向量,它可能会起作用。但反过来这不是一个好主意。
根据问题更新进行更新。
在覆盖 STL 成员之前,您应该注意一些事项。它们并不是继承的好候选者,因为它没有任何虚拟析构函数。如果您使用基本数据类型和类似 POD 的结构,则不会产生太大问题。但是如果你使用它真正的面向对象的方式,你可能会面临一些不愉快的行为。
关于您的代码
- 为什么要将其类型转换为字符*?
- 序列化对象的方式由您选择。IMO 您所做的是以序列化的名义执行的基本文件写入操作。
- 序列化取决于对象,即模板类中的参数"T"。如果您使用的是 POD,或者基本类型不需要特殊同步。否则,您必须仔细选择编写对象的方式。 选择
- 文本格式或二进制格式是您的选择。文本格式总是有成本的,同时它很容易操作它而不是二进制格式。
例如,以下代码用于简单的读写操作(文本格式(。
fstream fr("test.txt", ios_base::out | ios_base::binary );
for( int i =0;i <_countof(arr);i++)
fr << arr[i] << ' ';
fr.close();
fstream fw("test.txt", ios_base::in| ios_base::binary);
int j = 0;
while( fw.eof() || j < _countof(arrout))
{
fw >> arrout[j++];
}
在我看来,生成包含向量的二进制文件最直接的根是内存映射文件并将其放置在映射区域中。 正如 sarat 所指出的,您需要担心指针在类中的使用方式。但是,boost-interprocess 库有一个教程,介绍如何使用其共享内存区域(包括内存映射文件(来执行此操作。
首先,你看过Boost.multi_array吗?拿现成的东西总是好的,而不是重新发明轮子。
也就是说,我不确定这是否有帮助,但这是我实现基本数据结构的方法,并且序列化相当容易:
#include <array>
template <typename T, size_t DIM1, size_t DIM2, size_t DIM3>
class ThreeDArray
{
typedef std::array<T, DIM1 * DIM2 * DIM3> array_t;
array_t m_data;
public:
inline size_t size() const { return data.size(); }
inline size_t byte_size() const { return sizeof(T) * data.size(); }
inline T & operator()(size_t i, size_t j, size_t k)
{
return m_data[i + j * DIM1 + k * DIM1 * DIM2];
}
inline const T & operator()(size_t i, size_t j, size_t k) const
{
return m_data[i + j * DIM1 + k * DIM1 * DIM2];
}
inline const T * data() const { return m_data.data(); }
};
可以直接序列化数据缓冲区:
ThreeDArray<int, 4, 6 11> arr;
/* ... */
std::ofstream outfile("file.bin");
outfile.write(reinterpret_cast<char*>(arr.data()), arr.byte_size());
- 在C++中初始化向量映射的最有效方法
- 将此布尔值传递给此函数的最有效方法是什么?
- 比较C++变量的最有效方法
- 在 c++ 中解决段树以外的范围查询的有效方法是什么?
- 存储变量的更有效方法是什么?
- 确保套装新鲜度的有效方法
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 映射唯一值和重复值的有效方法.可以访问键或值的位置
- 在C++事务之间存储大量字符数据的有效方法
- 在unordered_multimap中精确迭代一次每个键的有效方法
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- 检查两个向量是否并行的最有效方法
- 从浮点数中删除小数部分但保留类型的有效方法
- 传递非泛型函数的最有效方法是什么?
- 按升序打印矢量的所有元素直到它为空而没有重复项的最有效方法是什么?
- 创建字符串数组的有效方法
- 返回一个引用C++中另一个类对象的对象的有效方法
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 从std::map值中获取密钥的有效方法