STL或Boost中有QList类型的数据结构吗?
简单介绍一下QList,QList与QVector和STL向量类似,但在开头也预留了一些空间; (最后也是) 我正在 STL 中寻找类似的东西,如果不是至少 Boost
在开始时保留空间可以改进前置或删除第一个项目(恒定时间),因为缓冲区可以向后增长。根据插入位置改善插入。 那么有人知道STL/C++中有类似的数据结构吗? 谢谢。
just a brief QList intro, QList is similar to QVector and STL vector but in addition it also reserves some space at the beginning; (and in the end too)
I am looking for something similar in STL , if not atleast Boost
Reserving space at the beginning improves prepending or removing the first item (constant time), because the buffer can grow backwards. Insertion is improved depending on the position of insertion.
So does anyone know of a similar data structure in STL/C++??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
std::deque
提供恒定时间的前向和后向插入删除(如果您正在寻找的话)。std::deque
provides front and back insertion removal at constant time, if it is what you are looking for.如果您希望在序列的开头和结尾处进行高效的插入和删除,请使用 std::deque。
If you want efficient insertion and removal at the beginning and end of a sequence, use a
std::deque
.如果您知道容器大小的上限并且不打算在容器中间插入,则可以使用 boost::circular_buffer。
如果您要进行大量的中间容器插入,
deque
将是比vector
更好的选择,因为它(通常)将成员分组为固定大小的块,而不是一个连续的内存块。注意 - QList 实际上声明它使用指向对象的指针数组,如果它们都是不平凡的。要在 C++ 中模拟此操作,您可以使用
deque
或(更好)MyClass
上的一些智能指针包装器,例如unique_ptr
或shared_ptr
,以防止在管理中过度复制MyClass
。If you know an upper limit on container size and don't plan on mid-container inserts you could use boost::circular_buffer.
If you are doing a lot of mid-container inserts
deque
would be the a better choice thanvector
since it (typically) groups members into fixed-size blocks, rather than one contiguous block of memory.Note - QList actually states that it uses an array of pointers to objects, if they are non-trivial. To simulate this in C++, you would use
deque<MyClass*>
or (better) some smart pointer wrapper onMyClass
such asunique_ptr
orshared_ptr
, to prevent excessive copying ofMyClass
in housekeeping.