Which is a better c++ container for holding and accessing binary data?
std::vector
or
std::string
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.