简体   繁体   中英

Using QVector and implement a subset of QVector in good practice

Hello and good morning together, I have two questions concerning QVector and its usage. I have an own custom class. It is necessary to use QVector<QSharedPointer<Class*>> or does it suffice to directly add instances to QVector like QVector<Class*>> . I read that QVector already uses a shared pointer internally contrary std vector. It is bad practice to append instance pointer directly?

Next, I want to have a subset of QVector with selected elements. What is good practice to do that using QVector>?

 __________
|QVector   |
|   _______|
|  |QVector|
|__|_______|

Mayble it's not gut idea to use QVector to directly add instances. Better solution is using QList container.

It does not store objects directly, but instead stores pointers to them. You gain all the benefits of quick insertions at both ends, and reallocations involve shuffling pointers instead of copy constructors, but lose the spacial locality of an actual or , and gain a lot of heap allocations.

QList<QList<Class>> testlist , and don't forget to override operator =

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM