std::string vs std::vector<char>

Is it bad practice to use std::string instead of std::vector<char>, for reading non-text (binary) data from file streams? And if yes, why?
std::char_traits<char> doesn't make any sense for binary (non- character) data.

I would go for: std::vector< unsigned char > if the bytes are to be treated as unsigned,
std::vector< signed char > if not.
Last edited on
std::char_traits<char> doesn't make any sense for binary (non- character) data.

Are there any repercussions for this, in the form of the data not being read/stored/written correctly?
Thank you.
http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment

If it's a string, I should be able to treat it like a string. Does cout << string_that_is_not_a_string make sense? Does it make sense to be able to use substr on a container of binary data?

I just think it doesn't properly convey intent.
using StreamableRawData = std::basic_string<unit8_t>;

Does this convey intent?
Last edited on
YeenFei at StackOverflow wrote:
I dont think one should use std::string for byte-data-storage. The method provide aren't design to deal with byte-data and you will risk yourself since any changes (or "optimization") on std::string will break your code.

Relevant. Because compiler optimization does seem to break my program.
Topic archived. No new replies allowed.