Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何读取和写入一系列项目,其中沉淀项目可能指的是后编码项目?_C++_Io - Fatal编程技术网

C++ 如何读取和写入一系列项目,其中沉淀项目可能指的是后编码项目?

C++ 如何读取和写入一系列项目,其中沉淀项目可能指的是后编码项目?,c++,io,C++,Io,我在树结构中有许多项。我可以沿着树迭代一个接一个地读写它们。但是,类项中有引用指针,如下所示 class Item { Item* m_refItem; ... }; void read() { Index index; in >> index; m_refItem = getItem(index); } 如果被引用项在树中的引用项之前,我们可以在读写中使用被引用项的索引,如下所示 class Item { Item* m_refI

我在树结构中有许多项。我可以沿着树迭代一个接一个地读写它们。但是,类项中有引用指针,如下所示

class Item
{
    Item* m_refItem;
    ...
};
void read()
{
    Index index;
    in >> index;
    m_refItem = getItem(index);
}
如果被引用项在树中的引用项之前,我们可以在读写中使用被引用项的索引,如下所示

class Item
{
    Item* m_refItem;
    ...
};
void read()
{
    Index index;
    in >> index;
    m_refItem = getItem(index);
}

但是,如果引用项位于引用项之后,则在读取引用项时,不会构造引用项,并且getItem(index)方法会给出错误的结果。在这种情况下如何读写?

您可以分两个阶段进行:

  • 读取所有节点并存储索引
  • 用指针替换所有索引
  • 代码: