“Proper” way to store binary data with C++/STL

后端 未结 4 952
陌清茗
陌清茗 2020-12-02 08:25

In general, what is the best way of storing binary data in C++? The options, as far as I can tell, pretty much boil down to using strings or vectors. (I\'ll omit

4条回答
  •  伪装坚强ぢ
    2020-12-02 09:12

    I use std::string for this too, and have never had a problem with it.

    One "pointer," which I just received a sharp reminder of in a piece of code yesterday: when creating a string from a block of binary data, use the std::string(startIter, endIter) constructor form, not the std::string(ptr, offset, length) form -- the latter makes the assumption that the pointer points to a C-style string, and ignores anything after the first zero character (it copies "up to" the specified length, not length characters).

提交回复
热议问题