Serialise and deserialise vector in binary

前端 未结 4 1220
时光说笑
时光说笑 2020-12-11 21:42

I am having problems trying to serialise a vector (std::vector) into a binary format and then correctly deserialise it and be able to read the data. This is my first time us

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 22:06

    You're using the address of the vector. What you need/want is the address of the data being held by the vector. Writing, for example, would be something like:

    size = example.size();
    file.write((char *)&size, sizeof(size));
    file.write((char *)&example[0], sizeof(example[0] * size));
    

提交回复
热议问题