You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

cereal库序列化/反序列化后对象不一致问题求助

cereal序列化/反序列化后非时间戳向量不一致问题

(此问题表述可能不够清晰妥当,我斟酌许久,仍认为请教比不问更好。)

我使用cereal库对自定义struct进行序列化/反序列化操作时,发现反序列化后的对象与原对象存在部分不一致。

操作流程

(Windows系统,MSVC编译器,Release配置(开启优化))

  • 读取文本文件并构建一个包含不同类型std::vector<T>的std::unordered_map对象,同时记录当前时间戳。
  • 使用cereal库将该对象序列化为二进制文件保存 - [file1]
  • 重复步骤1(读取文本构建对象,记录当前时间戳,因此两次时间戳不同) - [object1]
  • 反序列化[file1]得到对象,通过obj1.vector1 == obj2.vector1与[object1]对比。

我原本以为仅含时间戳的向量会不一致,其余向量应完全相同,但实际有部分非时间戳向量也不一致。

已尝试的解决方法

  • 将std::unordered_map替换为std::map -> 结果无变化。
  • 切换至Debug模式运行程序 -> 结果有变化,但对象仍不一致。
  • 固定时间戳的值 -> 两个对象完全一致。

只有方法3得到了预期结果,我无法理解此现象,恳请各位提供建议。


相关代码示例

自定义struct包含如下向量:

// not exactly the same struct that I used, but you get the idea..
struct MyStruct{
    std::vector<uint64_t> timestamps;
    std::vector<int> scores;
    std::vector<std::string> names;
    //...
}

(我向timestamps向量中push_back了微秒级的当前时间戳。)

使用cereal库序列化对象至二进制文件的标准代码:

{
    std::ofstream ofsb{ path, std::ios_base::binary };
    cereal::BinaryOutputArchive oab(ofsb); 
    oab(myObject_to_binary);
}

使用cereal库反序列化的代码:

{
    std::ifstream ifsb{ path, std::ios_base::binary };
    cereal::BinaryInputArchive iab(ifsb);
    iab(myObject_from_binary);
}

内容的提问来源于stack exchange,提问作者starriet 차주녕

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:30:49