开发者

std::string as C++ byte array

开发者 https://www.devze.com 2022-12-16 11:26 出处:网络
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 Pyth

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:

  • It allows fast assignment via assign and f开发者_开发问答ast direct access via data that is not allowed with vector<byte>
  • It allows easier memory management and const references, unlike using 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_casts)


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.


std::strings may have a reference counted implementation which may or may not be a advantage/disadvantage to what you're writing -- always be careful about that. std::string may not be thread safe. The potential advantage of std::string is easy concatenation, however, this can also be easily achieved using STL.

Also, all those problems in relation to protocols dissapear when using boost::asio and it's buffer objects.

As for drawbacks of std::vector:

  1. fast assign can be done by a trick with std::swap
  2. data can be accessed via &arr[0] -- vectors are guaranteed (?) to be continious (at least all implementations implement them so)

Personally I use std::vector for variable sized arrays, and boost::array for static sized ones.


I "think" that using std::vector is a better approach, because it was intended to be used as an array. It is true that all implementations(I know and heard of) store string "elements" in contiguous memory, but that doesn't make it standard. i.e. the code that uses std::string like a byte array, it assumes that the elements are contiguous where they don't have to be according to the standards.

0

精彩评论

暂无评论...
验证码 换一张
取 消