我试图理解序列化/反序列化如何在不使用库的情况下在C++中工作.我从简单的对象开始,但是当反序列化向量时,我发现,如果没有先写入它的大小,我就无法获得向量.而且,我不知道应该选择哪种文件格式,因为如果在向量大小之前存在数字,我就无法正确读取它.此外,我想用类和映射容器来做到这一点.我的任务是序列化/反序列化这样的对象:
PersonInfo
{
unsigned int age_;
string name_;
enum { undef, man, woman } sex_;
}
Person : PersonInfo
{
vector<Person> children_;
map<string, PersonInfo> addrBook_;
}
Run Code Online (Sandbox Code Playgroud)
目前我知道如何序列化这样的简单对象:
vector<PersonInfo> vecPersonInfo;
vecPersonInfo.push_back(*personInfo);
vecPersonInfo.push_back(*oneMorePersonInfo);
ofstream file("file", ios::out | ios::binary);
if (!file) {
cout<<"can not open file";
} else {
vector<PersonInfo>::const_iterator iterator = vecPersonInfo.begin();
for (; iterator != vecPersonInfo.end(); iterator++) {
file<<*iterator;
}
Run Code Online (Sandbox Code Playgroud)
能否请您建议,我如何为这个复杂的对象或一个能够清楚解释它的好教程做到这一点?
我在 Lisp 中有这个函数:
(defun AddtoQueue (queue method)
(cond
( (eq method 'DFS) (append (growPath (car queue) (findCh (caar queue))) (cdr queue) ) )
( (eq method 'BFS) (append (cdr queue) (growPath (car queue)(findCh (caar queue))) ) )
( (eq method 'A) (SORT (append (cdr queue) (growPath (car queue) (findCh (caar queue)) ) ) #'> :key #'pathLength ) )
(T "not implemented")
)
)
Run Code Online (Sandbox Code Playgroud)
我必须使用自定义函数(此处名为pathLength)对列表进行排序。我阅读了 lisp 的文档,sort但我什么都不懂。我的问题是我到底在为我的比较函数提供什么?
比较功能:
(defun pathLength(point)
;;distance from origin point
(setq x (- …Run Code Online (Sandbox Code Playgroud)