I hope this question is appropriate for stackoverflow… What is the difference between storing raw data bytes (8 bits) in a std::string rather than storing them in std::vector<char>. I’m reading binary data from a file and storing those raw bytes in a std::string. This works well, there are no problems or issues with doing this. My program works as expected. However, other programmers prefer the std::vector<char> approach and suggest I stop using std::string as it’s unsafe for raw bytes. So I’m wondering why might it be unsafe to use std::string to hold raw data bytes? I know std::string is most often used to store ASCII text, but a byte is a byte, so I don’t understand the preference of the std::vector<char>.
Thanks for any advice!
The problem is not really whether it works or it doesn’t. The problem is that it is utterly confusing for the next guy reading your code.
std::stringis meant for displaying text. Anybody reading your code will expect that. You’ll declare your intent much better with astd::vector<char>.It increases your WTF/min in code reviews.