RAW指针容器包装器
我有一个指向数据数组的原始指针。我想将此指针包装到具有 STL 容器语义的容器中(例如 std::vector)。 STL 有什么功能可以实现这一点吗?
例如
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;
};
编辑:
我不能直接使用 std::vector 因为内存是由外部 api 分配的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
STL 没有,boost 可以:
STL doesn't, boost does:
如果您将 std::vector 与自定义内存“分配器”一起使用,这可能是可行的,但对我来说这听起来不是一个好主意。
由于据我所知,您不可能在不编写代码的情况下摆脱这种情况,因此我建议花时间为这种情况编写您自己的类似 STL 的容器(或者更好的是,找到一个开源容器!)。
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!).