向量的向量的序列化结构 [英] Serializing Struct of vectors of vector
问题描述
我在这里有点复杂.我想将向量的结构保存到文件中,并在一段时间后读取.但是问题出在阅读上.我不知道如何从保存的文件中将矢量填充到结构中.
I`m in a bit complicated situation here. I want to save a struct of vectors to a file and read it after some time. But the problem is with the reading. I dont know how i can fill al vectors into structure from the saved file.
struct TDNATable{
std::vector<String>GenomOne;
std::vector<String>GenomeL;
std::vector<String>GenomeER;
std::vector<String>GenomeRET;
std::vector<String>GenomeSEL;
};
std::vector<TDNATable> DnaTbl;
//PSEUDO CODE:
//For example simple writing could be
ofstream file("C:\\Users\\User11\\Desktop\\SNF_TBL.INI", ios::binary);
file.write((char*)&DnaTbl, sizeof(DnaTbl));
//Problem comes with reading
// impl
ifstream file("C:\\Users\\User11\\Desktop\\SNF_TBL.INI",
std::ifstream::binary);
// get pointer to associated buffer object
std::filebuf* pbuf = file.rdbuf();
// get file size using buffer's members
std::size_t size = pbuf->pubseekoff(0, file.end, file.in);
pbuf->pubseekpos(0, file.in);
// allocate memory to contain file data
char* buffer = new char[size];
// get file data
pbuf->sgetn(buffer, size);
file.close();
for (int i = 0; i < SnfTbl.size(); i++) {
//Back inserter can be used only with 1D vector
std::copy(buffer, buffer +sizeof(buffer),
std::back_inserter(SnfTbl[i].GenomeL);
std::copy(buffer, buffer +sizeof(buffer),
std::back_inserter(SnfTbl[i].GenomeER));
}
RefreshDFSGrid();
delete[]buffer;
file.close();
我尝试使用boost/serialize但没有成功.
I tried with boost/serialize but without sucsess.
您是否知道如何以一种优雅的方式保存/加载此ds?谢谢!
Do you have any idea how can i save/load this ds in a elegant way? Thanks!
推荐答案
对于轻松完成任务,助推器可能会显得过分杀伤力.在我自己的代码中,我通过各种流类解决了该问题.我是这样做的:
Boost could be an overkill for easy tasks. In my own code, I solved that problem with sorts of stream class. I did it that way:
-
使用
虚拟Read(buffer,byteCount)= 0
和virtual Write(buffer,byteCount)= 0
声明抽象基类.在下面的说明中,IArchiveI
和IArchiveO
是此类基类.
Declare abstract base class with
virtual Read(buffer, byteCount) = 0
andvirtual Write(buffer, byteCount) = 0
. In illustration below,IArchiveI
andIArchiveO
are such base classes.
对于内置类型,提供 operator<<
和 operator>>
,它们仅在适当的时候简单地调用Read()和Write().
For builtin types, provide operator <<
and operator >>
that simply calls Read() and Write() as appropriate.
对于诸如vector/string/...之类的库类型,请提供基于基本类型运算符构建的非成员模板运算符(例如,您不再调用原始的Read/Write).
For library types such as vector / string / ..., provide non-member template operators built on base type operators (eg you no longer call raw Read / Write).
例如,有一种处理向量的方法:
For instance, there's how I handle a vector:
template <class T>
IArchiveO& operator << (IArchiveO& a_Stream, const std::vector<T>& a_Vector)
{
a_Stream << a_Vector.size();
for (size_t i = 0; i < a_Vector.size(); i++)
{
a_Stream << a_Vector[i];
}
return a_Stream;
}
template <class T>
IArchiveI& operator >> (IArchiveI& a_Stream, std::vector<T>& a_Vector)
{
a_Vector.clear();
size_t contSize = 0;
a_Stream >> contSize;
a_Vector.resize(contSize);
for (size_t i = 0; i < contSize; i++)
{
a_Stream >> a_Vector[i];
}
return a_Stream;
}
对于您自己的非库类型,请以相同的方式向运营商提供.例如,您的代码如下所示:
For non-library types of your own, provide operators the same way. For instance, here's how your code would look like:
IArchiveI& operator >> (IArchiveI& a_Stream, TDNATable& a_Value)
{
a_Stream >> a_Value.GenomOne;
a_Stream >> a_Value.GenomeL;
a_Stream >> a_Value.GenomeER;
a_Stream >> a_Value.GenomeRET;
a_Stream >> a_Value.GenomeSEL;
return a_Stream;
}
-
从基类继承,并创建可提供存储(例如,读取/写入文件)的类.您只需要重载
虚拟读(缓冲区,byteCount)
和虚拟写(缓冲区,byteCount)
.
最后,您构造存储类的实例并一次性序列化整个数组(在此代码中,CFileArchiveO继承自IArchiveO,重载了Write()): CFileArchiveO ar(...);ar<<DnaTbl;
Finally, you construct an instance of storage class and serialize your entire array in one go (in this code, CFileArchiveO is inherited from IArchiveO, overloading Write()):
CFileArchiveO ar(...);
ar << DnaTbl;
诀窍是,当编译器为每种类型都有运算符时,即使它是 vector< vector< vector<<<<>>>;
The trick is, when compiler has operators for each type, it automatically builds code for whatever nesting you have, even if it's a vector<vector<vector<string>>>
这篇关于向量的向量的序列化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!