我有一个类,它基本上由一个向量矩阵组成: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;
}发布于 2011-07-01 23:11:47
最有效的方法是将对象存储到一个数组(或连续的空间)中,然后将缓冲区爆炸到文件中。一个优点是,磁盘盘片不会浪费时间上升,而且写入可以连续执行,而不是在随机位置。
如果这是您的性能瓶颈,您可能需要考虑使用多个线程,一个额外的线程来处理输出。将对象转储到缓冲区中,设置一个标志,然后写入线程将处理输出,释放您的主任务来执行更重要的任务。
编辑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 '\0' 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;
};发布于 2011-07-01 23:15:48
我建议你阅读C++ FAQ on Serialization,你可以选择最适合你的
在处理结构和类时,必须注意两件事
这两种方法都可能在输出中产生一些臭名昭著的结果。IMO,对象必须实现才能序列化和反序列化对象。对象可以很好地了解结构、指针、数据等,因此它可以决定哪种格式可以有效地实现。
无论如何,你将不得不迭代,或者不得不将其包装在某个地方。一旦完成了序列化和反序列化函数的实现(您可以使用运算符或函数编写)。尤其是当您使用stream objects, overloading << and >> operators时,传递对象会很容易。
关于你关于使用向量的底层指针的问题,如果它是一个单独的向量,它可能会起作用。但从另一方面来说,这不是一个好主意。
根据问题更新进行更新。
在重写STL成员之前,有几件事你应该注意。它们实际上不是一个很好的继承候选者,因为它没有任何虚拟析构函数。如果你使用的是基本的数据类型和类似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++];
}发布于 2011-07-02 01:03:39
在我看来,生成包含向量的二进制文件的最直接根是内存映射文件,并将其放在映射区域中。正如sarat所指出的,您需要关心指针在类中的使用方式。但是,boost-interprocess library有一个关于如何使用包括memory mapped files的共享内存区域来做到这一点的tutorial。
https://stackoverflow.com/questions/6549731
复制相似问题