I'm looking for a C++ "equivalent" of Java ByteBuffer.
I'm probably missing the obvious or just need an isolated usage example to clarify. I've looked through the iostream family & it looks like it may provide a basis. Specifically, I want to be able to:
- build a buffer from a byte array/point and get primitives from the buffer, e.g. getByte, getInt
- build a buffer using primitives e.g. putByte, putInt and then get the byte array/pointer.
I wrote this awhile back to do exactly what you're asking for. Give it a shot:
https://code.google.com/p/bytebuffer-cpp/
stringstream
provides basic unformattedget
andwrite
operations to write blocks of chars. To specialise onT
either subclass or wrap it, or provide free standing template functions to use the get/write appropriately sized memory.You could write such templates for whatever other stream or vector you want - in the vector's case, it would need to use insert rather than write.
Thanks for all the input, it has lead to this pretty simple solution:
To use eg:
for std::vector more efficient is method
You can find more here:
http://www.cppreference.com/wiki/stl/vector/start
and general about cpp stl libs
http://www.cppreference.com/wiki/stl/start
There are many containers, depends what do You need it for,
take a look at std::list, std::vector.
You have
stringbuf
,filebuf
or you could usevector<char>
.This is a simple example using
stringbuf
:Thanks @Pete Kirkham for the idea of generic functions.