简体   繁体   English

使用 QVector 并在良好实践中实现 QVector 的子集

[英]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.大家好,早上好,我有两个关于 QVector 及其用法的问题。 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*>> .有必要使用QVector<QSharedPointer<Class*>>或者直接将实例添加到QVector就足够了,比如QVector<Class*>> I read that QVector already uses a shared pointer internally contrary std vector.我读到QVector已经在内部使用了一个与 std 向量相反的共享指针。 It is bad practice to append instance pointer directly?直接附加实例指针是不好的做法吗?

Next, I want to have a subset of QVector with selected elements.接下来,我想要一个带有选定元素的QVector子集。 What is good practice to do that using QVector>?使用 QVector> 做到这一点的好做法是什么?

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

Mayble it's not gut idea to use QVector to directly add instances.也许使用QVector直接添加实例并不是一个好主意。 Better solution is using QList container.更好的解决方案是使用QList容器。

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.您可以获得两端快速插入的所有好处,重新分配涉及改组指针而不是复制构造函数,但失去了实际 or 的空间局部性,并获得了大量堆分配。

QList<QList<Class>> testlist , and don't forget to override operator = QList<QList<Class>> testlist ,不要忘记覆盖operator =

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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