vector vs string for binary data

后端 未结 9 2163
日久生厌
日久生厌 2020-12-05 07:25

Which is a better c++ container for holding and accessing binary data?

std::vector

or

std::string
         


        
9条回答
  •  醉梦人生
    2020-12-05 07:39

    For the longest time I agreed with most answers here. However, just today it hit me why it might be more wise to actually use std::string over std::vector.

    As most agree, using either one will work just fine. But often times, file data can actually be in text format (more common now with XML having become mainstream). This makes it easy to view in the debugger when it becomes pertinent (and these debuggers will often let you navigate the bytes of the string anyway). But more importantly, many existing functions that can be used on a string, could easily be used on file/binary data. I've found myself writing multiple functions to handle both strings and byte arrays, and realized how pointless it all was.

提交回复
热议问题