Google's Protocol buffer uses the C++ standard string class std::string
as variable size byte array (see here) similar to Python where the string class is also used as byte array (at least until Python 3.0).
This approach seems to be good:
assign
and fast direct access via data
that is not allowed with vector<byte>
byte*
.But I am curious: Is that the preferred way for a byte arrays in C++? What are the drawbacks of this approach (more than a few static_cast
s)
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
While std::string has the size of 24 bytes, it allows strings up to 22 bytes(!!) with no allocation.
Remember that a string is basically just a byte array For example, the following code iterates over every byte in a string and prints it out as both a string and as a byte.
The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.
Personally, I prefer std::vector
, since std::string
is not guaranteed to be stored contiguously and std::string::data()
need not be O(1). std::vector
will have data
member function in C++0x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With