如何在序列化期间插入字符串流的中间

How to insert in middle of stringstream during serialisation?

本文关键字:字符串 中间 插入 序列化      更新时间:2023-10-16
std::ostringstream oss;
boost::archive::text_oarchive oa(oss); 

我向这个oa添加了可变数量的内容,就像这样

    oa & int1;
    oa &int2;
--------------------> insert number of matrices here
    oa & matrix1;
    ..//do some processing
    oa & matrix2; 
    ...//do some more
    ....
    oa & matrixn;

矩阵参考 - http://robot.kaist.ac.kr/haptics/chai3d-2.0.0_Doc/resources/html/structc_matrix3d.html

现在,当我完成时,我想在 udp 发送之前插入我添加到此存档的矩阵数量,然后再开始添加矩阵。但我也知道在将它们添加到流之后,我添加了多少矩阵

我应该怎么做?

你不能做

oa & matrix1.

为此,矩阵必须是一个简单的类型(它不是(或实现函数序列化。您可以重写矩阵实现序列化,然后使用它。

这是一个很好的参考: http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/tutorial.html

你实际上可以从OSS <<(int(0;然后在你写完所有东西之后,都找回开头,用你添加的项目数重写前 4 个字节。