14

While string should be used for working with strings, I would like to know what structure you should use in C++ when working with blocks of data.

I'm asking this because it would be nicer to use one parameter instead of passing a char* data and size_t size (or a custom structure).

0

3 Answers 3

14
std::vector<unsigned char>

or, preferably,

std::vector<std::uint8_t>

(In C++11, uint8_t can be found in <cinttypes>. Older compilers, but not MSVC, may have the C99 header <inttypes.h>. If your data is a sequence of 16-bit units, use uint16_t etc.)

If the size of the data blocks is known at compile time, std::array is appropriate; it wastes less space than vector.

Sign up to request clarification or add additional context in comments.

Comments

4

There are a number of containers in the STL, not only vector. Look and choose what fits your situation.

Comments

2

The above solutions is good, but consider this solution may be good :

  1. bitset in STL
  2. bit_vector in SGI
  3. qbitarray in QTL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.