RAW pointer container wrapper
I have a raw pointer which points to an array of data. I would like to wrap this pointer into a container with STL container semantics (e.g. std::vector). Does the STL have any feature which allows this?
e.g.
class my_class
{
public:
std::some_container<char> get_data() { return std::some_container(my_data, my_data_size);}
private:
char* my_data;
size_t my_data_size;
};
EDIT:
I cannot use std:开发者_运维百科:vector directly since the memory is allocated by an external api.
STL doesn't, boost does:
boost::iterator_range<char*> get_data() {
return boost::iterator_range<char*>(my_data, my_data+my_data_size);
}
Possibly this is doable if you use std::vector
with a custom memory "allocator", but it doesn't sound like a good idea to me.
Since there is no way I know of that would let you get away with this without writing code, I suggest taking the time to write your own STL-like container for this scenario (or better yet, find an open source one!).
精彩评论