如何读写一个项目序列,其中前面的项目可能指的是后面的项目
How to read and write a sequence of items where preciding items may refer to postceding items?
树状结构中有许多项。我可以在树的迭代中一个一个地读取和写入它们。然而,在Item类中有一个引用指针,就像下面的
class Item
{
Item* m_refItem;
...
};
如果被引用的项在树的引用项之前,我们可以在读和写时使用被引用项的索引,如下所示
void read()
{
Index index;
in >> index;
m_refItem = getItem(index);
}
但是,如果被引用的项在引用项之后,则在读取引用项时,不会构造被引用的项,并且getItem(index)方法会给出错误的结果。在这种情况下如何进行阅读和写作?
您可以分两个阶段完成:
- 读取所有节点并存储索引
- 用指针替换所有索引
class Item {
union {
Item* m_refItem;
int _index; }
void read_phase_one(stream in) {
in >> _index; }
void read_phase_two(vector vect) {
m_refItem = vect[index]; }
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 我是否正确将项目添加到链接列表前面
- 如何读写一个项目序列,其中前面的项目可能指的是后面的项目