[Development] OK to add zero/bulk copy to QVector?

Marc Mutz marc.mutz at kdab.com
Mon Jan 6 11:19:41 CET 2014


On Monday 30 December 2013 00:24:58 Jiergir Ogoerg wrote:
> And because of that I have to (can) resize() it,

A better way is to reserve() + push_back(), but IIRC QVector never contains 
uninitialised memory. std::vector, however, does, and expands to less code, 
too, with private element types such as yours.

> and resizing sucks
> because it not just creates
> more space for the internal buffer but also initializes each item to
> the default value.

...so does new[]...

> QComposeTableElement *elem;
>     vec->reserve(kElemCount);
> 
>     for(int i=0; i<kElemCount; i++) {
>         elem = (QComposeTableElement*) (buf + (i * kElemSize));
>         vec->push_back(*elem);
>     }

you should use push_back(qMove(*elem)) and implement the move operation on 
QComposableTableElement.

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions



More information about the Development mailing list