无法反序列化 std::list<std::string>
Cannot deserialize std::list<std::string>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
class SerializableSmth
{
friend class boost::serialization::access;
private:
std::list<std::string> data;
template<class Archive> void serialize(Archive & ar, unsigned int version)
{
ar & data;
}
};
BOOST_CLASS_VERSION(SerializableSmth, 1);
首先看一下这个问题:当data
包含超过~7个字符的字符串时,它不能被序列化回。序列化的text_oarchive看起来像这样:
22 serialiation::archive 10 0 1 0 0 2 0 5 test1 13 test2-914166-
(当test2-被缩短为5 test2-时,它工作正常)。
当使用text_oarchive
对std::stringstream
进行序列化并使用boost::iostreams::basic_array
进行反序列化时,会发生这种情况。
这个问题似乎正在通过设置在输出和输入存档上的boost::archive::no_header | boost::archive::no_codecvt
标志得到修复。
我无法复制它,但也许这个SSCCE可以帮助您发现问题:
查看Live On Coliru
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>
class SerializableSmth
{
public:
void init() {
for (int i=0; i<100; ++i) {
data.push_back("Some fairly long string " + std::to_string(i) + " with a number");
}
}
friend class boost::serialization::access;
std::list<std::string> data;
template<class Archive> void serialize(Archive & ar, unsigned int version)
{
ar & data;
}
};
BOOST_CLASS_VERSION(SerializableSmth, 1);
#include <iostream>
int main()
{
std::string serialized;
{
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
SerializableSmth original;
original.init();
oa << original;
serialized = ss.str();
}
{
boost::iostreams::basic_array_source<char> as(serialized.data(), serialized.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > is(as);
// now let's see it back:
boost::archive::text_iarchive ia(is);
SerializableSmth cloned;
ia >> cloned;
std::cout << "Cloned data has " << cloned.data.size() << " recordsn";
}
}
打印
Cloned data has 100 records
像预期的那样序列化的数据为一行,约4.5kB:
22 serialization::archive 10 0 1 0 0 100 0 39 Some fairly long string 0 with a number 39 Some fairly long string 1 with a number 39 Some fairly long string 2 with a number 39 Some fairly long string 3 with a number 39 Some fairly long string 4 with a number 39 Some fairly long string 5 with a number 39 Some fairly long string 6 with a number 39 Some fairly long string 7 with a number 39 Some fairly long string 8 with a number 39 Some fairly long string 9 with a number 40 Some fairly long string 10 with a number 40 Some fairly long string 11 with a number 40 Some fairly long string 12 with a number 40 Some fairly long string 13 with a number 40 Some fairly long string 14 with a number 40 Some fairly long string 15 with a number 40 Some fairly long string 16 with a number 40 Some fairly long string 17 with a number 40 Some fairly long string 18 with a number 40 Some fairly long string 19 with a number 40 Some fairly long string 20 with a number 40 Some fairly long string 21 with a number 40 Some fairly long string 22 with a number 40 Some fairly long string 23 with a number 40 Some fairly long string 24 with a number 40 Some fairly long string 25 with a number 40 Some fairly long string 26 with a number 40 Some fairly long string 27 with a number 40 Some fairly long string 28 with a number 40 Some fairly long string 29 with a number 40 Some fairly long string 30 with a number 40 Some fairly long string 31 with a number 40 Some fairly long string 32 with a number 40 Some fairly long string 33 with a number 40 Some fairly long string 34 with a number 40 Some fairly long string 35 with a number 40 Some fairly long string 36 with a number 40 Some fairly long string 37 with a number 40 Some fairly long string 38 with a number 40 Some fairly long string 39 with a number 40 Some fairly long string 40 with a number 40 Some fairly long string 41 with a number 40 Some fairly long string 42 with a number 40 Some fairly long string 43 with a number 40 Some fairly long string 44 with a number 40 Some fairly long string 45 with a number 40 Some fairly long string 46 with a number 40 Some fairly long string 47 with a number 40 Some fairly long string 48 with a number 40 Some fairly long string 49 with a number 40 Some fairly long string 50 with a number 40 Some fairly long string 51 with a number 40 Some fairly long string 52 with a number 40 Some fairly long string 53 with a number 40 Some fairly long string 54 with a number 40 Some fairly long string 55 with a number 40 Some fairly long string 56 with a number 40 Some fairly long string 57 with a number 40 Some fairly long string 58 with a number 40 Some fairly long string 59 with a number 40 Some fairly long string 60 with a number 40 Some fairly long string 61 with a number 40 Some fairly long string 62 with a number 40 Some fairly long string 63 with a number 40 Some fairly long string 64 with a number 40 Some fairly long string 65 with a number 40 Some fairly long string 66 with a number 40 Some fairly long string 67 with a number 40 Some fairly long string 68 with a number 40 Some fairly long string 69 with a number 40 Some fairly long string 70 with a number 40 Some fairly long string 71 with a number 40 Some fairly long string 72 with a number 40 Some fairly long string 73 with a number 40 Some fairly long string 74 with a number 40 Some fairly long string 75 with a number 40 Some fairly long string 76 with a number 40 Some fairly long string 77 with a number 40 Some fairly long string 78 with a number 40 Some fairly long string 79 with a number 40 Some fairly long string 80 with a number 40 Some fairly long string 81 with a number 40 Some fairly long string 82 with a number 40 Some fairly long string 83 with a number 40 Some fairly long string 84 with a number 40 Some fairly long string 85 with a number 40 Some fairly long string 86 with a number 40 Some fairly long string 87 with a number 40 Some fairly long string 88 with a number 40 Some fairly long string 89 with a number 40 Some fairly long string 90 with a number 40 Some fairly long string 91 with a number 40 Some fairly long string 92 with a number 40 Some fairly long string 93 with a number 40 Some fairly long string 94 with a number 40 Some fairly long string 95 with a number 40 Some fairly long string 96 with a number 40 Some fairly long string 97 with a number 40 Some fairly long string 98 with a number 40 Some fairly long string 99 with a number
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>
- 如何获取std::vector<DMatch>