QList和QVector等容器的区别
- 大多数情况下可以用QList。像prepend()和insert()这种操作,通常QList比QVector快的多。这是因为QList是基于index标签存储它的元素项在内存中,比那种依赖iterator迭代的更快捷。而且你的代码也更少。
- 如果你需要一个真正的连接着的list,且需要保证一个固定插入耗时。那就用迭代器,而不是标签。使用QLinkedList();
- 如果你需要开辟连续的内存空间存储,或者你的元素远比一个指针大,这时你需要避免个别插入操作,出现堆栈溢出,这时候用QVector
- 如果你需要一个低层的可变数量大小的数组,用QVarLengthArray就够了。他可以预先在栈中分配已知长度大小的数组,如果超过这个长度,会在堆中继续存储。默认大小256。
QVarLengthArray<int, 1024> array(n + 1);
For most purposes, QList is the right class to use. Operations like prepend() and insert() are usually faster than with QVector because of the way QList stores its items in memory (see Algorithmic Complexity for details), and its index-based API is more convenient than QLinkedList's iterator-based API. It also expands to less code in your executable.
If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
If you want the items to occupy adjacent memory positions, or if your items are larger than a pointer and you want to avoid the overhead of allocating them on the heap individually at insertion time, then use QVector.
If you want a low-level variable-size array, QVarLengthArray may be sufficient.