QVarLengthArray of Qt6 - variable length array

QVarLengthArray is a Qt container that acts like a vector. Its elements are stored contiguously in memory and have a dynamic size, elements can be added or removed from it at any time.

Unlike std::vector or QVector, QVarLengthArray pre-allocates a small buffer within itself. The idea is simple: avoid paying for heap allocation if it only needs to store a relatively small number of elements in the "common case". (Arguably: it's a "vector with small object optimization".)

1. This is achieved by storing the elements directly in the internal buffer of the QVarLengthArray.

2. If QVarLengthArray grows too much and the number of elements exceeds the size of the internal buffer, heap allocation is used and QVarLengthArray becomes very much like a "normal" vector;

 3. Warning: The resize of QVarLengthArray is different from others; 

QVarLengthArray<T>::resize(N) does not necessarily initialize its elements

QVector<int> vec;
vec.resize(10);   //全是0
 
QVarLengthArray<int> qvla;
qvla.resize(10);  // 你确定的值

Because the value is uncertain, it may trigger an exception when reading or operating, as follows, when I randomly find 3 values ​​and print them, the values ​​are obviously random;

Guess you like

Origin blog.csdn.net/yanchenyu365/article/details/130363841