STL或Boost中有QList类型的数据结构吗?

发布于 2024-10-01 01:28:44 字数 181 浏览 1 评论 0原文

简单介绍一下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-10-08 01:28:44

std::deque 提供恒定时间的前向和后向插入删除(如果您正在寻找的话)。

std::deque provides front and back insertion removal at constant time, if it is what you are looking for.

九厘米的零° 2024-10-08 01:28:44

如果您希望在序列的开头和结尾处进行高效的插入和删除,请使用 std::deque。

If you want efficient insertion and removal at the beginning and end of a sequence, use a std::deque.

风筝有风,海豚有海 2024-10-08 01:28:44

如果您知道容器大小的上限并且不打算在容器中间插入,则可以使用 boost::circular_buffer

如果您要进行大量的中间容器插入,deque 将是比 vector 更好的选择,因为它(通常)将成员分组为固定大小的块,而不是一个连续的内存块。

注意 - QList 实际上声明它使用指向对象的指针数组,如果它们都是不平凡的。要在 C++ 中模拟此操作,您可以使用 deque 或(更好)MyClass 上的一些智能指针包装器,例如 unique_ptrshared_ptr,以防止在管理中过度复制MyClass

在内部,QList 表示为
指向类型项的指针数组
T. 如果 T 本身是指针类型或
不大于 a 的基本类型
指针,或者如果 T 是 Qt 的共享指针之一
类,然后 QList 存储
直接在指针数组中的项目。
对于一千个项目以下的列表,此
数组表示允许非常
中间快速插入,而且它
允许基于索引的访问。

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 than vector 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 on MyClass such as unique_ptr or shared_ptr, to prevent excessive copying of MyClass in housekeeping.

Internally, QList is represented as
an array of pointers to items of type
T. If T is itself a pointer type or a
basic type that is no larger than a
pointer, or if T is one of Qt's shared
classes, then QList stores the
items directly in the pointer array.
For lists under a thousand items, this
array representation allows for very
fast insertions in the middle, and it
allows index-based access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文