• 7668阅读
  • 7回复

QList v.s. QVector【Over】 [复制链接]

上一主题 下一主题
离线yangfanxing
 
只看楼主 倒序阅读 楼主  发表于: 2010-04-11
两者使用以及本质区别~大家讨论~~~
[ 此帖被yangfanxing在2010-04-12 13:27重新编辑 ]
PHPWind好恶心。。。不想看这种界面。。。
离线hanfengjay
只看该作者 1楼 发表于: 2010-04-11
貌似QList每次插入或追加元素时,都要从堆里面分配内存的。

而QVector好像是一开始就申请了一块比较大的内存,这样不用每次都要申请内存。。。
离线sak555555

只看该作者 2楼 发表于: 2010-04-12
顶顶顶顶
离线water_wf

只看该作者 3楼 发表于: 2010-04-12
引用第1楼hanfengjay于2010-04-11 20:33发表的  :
貌似QList每次插入或追加元素时,都要从堆里面分配内存的。
而QVector好像是一开始就申请了一块比较大的内存,这样不用每次都要申请内存。。。

Can you tell us,where you see that?
thx
离线benbenmajia

只看该作者 4楼 发表于: 2010-04-12
小羊羊,我记得有本书里面讲的挺清楚的...不过忘记了名字了~
我去google了!
安然.....
离线dbzhang800

只看该作者 5楼 发表于: 2010-04-12
引用第3楼yangfanxing于2010-04-12 09:49发表的  :
顶起求证~


from manual:
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.
离线benbenmajia

只看该作者 6楼 发表于: 2010-04-12
引用第6楼dbzhang800于2010-04-12 11:11发表的  :

张老大,你对manual的了解偶真是服啊~
安然.....
离线yangfanxing
只看该作者 7楼 发表于: 2010-04-12
嗯,manual是张老师编的,呵呵。。。谢谢~
PHPWind好恶心。。。不想看这种界面。。。
快速回复
限100 字节
 
上一个 下一个