UID:80073
UID:79855
UID:94770
UID:21207
引用第1楼hanfengjay于2010-04-11 20:33发表的 :貌似QList每次插入或追加元素时,都要从堆里面分配内存的。而QVector好像是一开始就申请了一块比较大的内存,这样不用每次都要申请内存。。。
UID:88244
UID:7394
引用第3楼yangfanxing于2010-04-12 09:49发表的 :顶起求证~
QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. 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, use QVector.Internally, QList<T> 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<T> 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. Furthermore, operations like prepend() and append() are very fast, because QList preallocates memory at both ends of its internal array. (See Algorithmic Complexity for details.) Note, however, that for unshared list items that are larger than a pointer, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation might make QVector a better choice in cases that do lots of appending or inserting, since QVector allocates memory for its items in a single heap allocation.Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.
引用第6楼dbzhang800于2010-04-12 11:11发表的 :