I'm about to write something myself since I didn't find what I was looking for, but figured I should ask the crowd before diving in.
The imaginary(?) container type would operate something like this:
const int blobSize = unpackBlobSize( msg );
int * blob = unpackBlobData( msg );
SpecialVector<int> mySpecialIntVector( blob, blobSize );
Basically I'm interfacing an old library with c-style raw pointers-to-buffers, but would like to use C++ style container semantics without requiring a copy step. What I would hope to have is std::vector plus preallocated & prefilled buffer constructor and, minus resize.
Libeigen has this sort of functionality with their Eigen::Map which allows things like the following:
int array[9];
for(int i = 0; i < 9; ++i)
array[i] = i;
stl::cout << Eigen::Map<Eigen::Matrix3i>(array) << stl::endl;
Anyone know of a boost or stl template that covers these constraints?
An iterator that reads, writes, and allows random access to a container.
All STL containers do not support all 5 types of iterators. For instance, random-access iterators are supported by the container “vector”, whereas bidirectional iterators are supported by the container “list”.
The RandomAccessIterator concept adds support for constant-time advancement with +=, +, -=, and -, as well as the computation of distance in constant time with -. Random access iterators also support array notation via subscripting.
So, as we can see here we can both access as well as assign value to the iterator, therefore the iterator is a random Access iterator.
Going from Dennis Zickenfoose's comment, I looked up Boost.range seems to offer the perfect solution:
#include <boost/range.hpp>
#include <boost/foreach.hpp>
#include <iostream>
int main()
{
const int blobSize = 100;
int blob[blobSize];
for( int i = 0; i < blobSize; ++i )
blob[i] = i;
boost::iterator_range<int*> blobPsudoContainer( blob, blob + blobSize );
BOOST_FOREACH( int & i, blobPsudoContainer )
i = i + 1;
std::cout << "Size is:" << blobPsudoContainer.size() << "\n";
std::cout << "value at 0 is:" << blobPsudoContainer[0] << "\n";
return 0;
}
Thanks Dennis! : )
There's nothing like you want, this is just a shortcoming of the language. If you were satisfied with having the allocator statically guess a good expected maximum element count, you could just embed a normal C array inside an allocator and defer to it as long as possible; this is usually called an auto buffer.
Old Answer:
All of the standard library containers allow you to specify an allocator. You can make a buffer allocator.
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