vector <unsigned char> vs string for binary data

kalaxy picture kalaxy · Oct 12, 2009 · Viewed 9.5k times · Source

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

std::vector<unsigned char>

or

std::string

Is one more efficient than the other?
Is one a more 'correct' usage?

Answer

David Rodr&#237;guez - dribeas picture David Rodríguez - dribeas · Oct 12, 2009

You should prefer std::vector over std::string. In common cases both solutions can be almost equivalent, but std::strings are designed specifically for strings and string manipulation and that is not your intended use.