专用于shared_ptr的集合 [英] Collection specialized for shared_ptr

查看:139
本文介绍了专用于shared_ptr的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个集合,意识到shared_ptr内部,并避免常规复制存储的shared_ptr元素,有利于只复制其内部弱指针?

Does there exist a collection, that is aware of shared_ptr internals, and avoids regular copying of stored shared_ptr elements in favor of just copying their internal weak pointer?

这隐含意味着不会构造函数/析构函数调用,也不会处理shared_ptrs的引用计数器。

This implicitly means, that no constructor/destructor calls will be done and that there will be no manipulation of shared_ptrs' reference counters.

推荐答案


意识到shared_ptr内部,

that is aware of shared_ptr internals,

这应该回答你的问题。要意识到内部,这样的集合几乎肯定必须是boost的智能指针库的一部分。不幸的是,没有这样的事情。

That should answer your question right there. To be aware of the internals, such a collection would almost certainly have to be part of boost's smart pointer libraries. Unfortunately, there is no such thing.

这确实是一个缺点,智能指针。我建议使用限制内部完成的副本数量的数据结构。矢量的重新分配将是痛苦的。也许一个deque,它有一个基于分块的分配,将是有用的。记住,向量实现往往会以指数级增长的块来获得新的内存。所以他们不重新分配,说,每10个元素。相反,你可以从128个元素开始,然后向量保留自己256,然后移动到512,1024等。每次加倍需要什么。

This is indeed a downside to smart pointers. I would recommend using data structures that limit the number of copies done internally. Vector's reallocations will be painful. Perhaps a deque, which has a chunked based allocation, would be useful. Keep in mind too that vector implementations tend to get new memory in exponentially increasing chunks. So they don't reallocate, say, every 10 elements. Instead you might start out with 128 elements, then the vector reserves itself 256, then moves up to 512, 1024, etc. Each time doubling what is needed.

这里有boost的ptr_vector或预分配你的数据结构足够的空间,以防止内部复制。

Short of this there's boost's ptr_vector or preallocating your data structures with enough space to prevent internal copying.

这篇关于专用于shared_ptr的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆