QVarLengthArray Class
The QVarLengthArray class provides a low-level variable-length array. More...
Header: | #include <QVarLengthArray> |
qmake: | QT += core |
Note: All functions in this class are reentrant.
Public Types
typedef | const_iterator |
typedef | const_pointer |
typedef | const_reference |
typedef | const_reverse_iterator |
typedef | difference_type |
typedef | iterator |
typedef | pointer |
typedef | reference |
typedef | reverse_iterator |
typedef | size_type |
typedef | value_type |
Related Non-Members
bool | operator!=(const int &left, const int &right) |
bool | operator==(const int &left, const int &right) |
Detailed Description
The C++ language doesn't support variable-length arrays on the stack. For example, the following code won't compile:
int myfunc(int n) { int table[n + 1]; // WRONG ... return table[n]; }
The alternative is to allocate the array on the heap (with new
):
int myfunc(int n) { int *table = new int[n + 1]; ... int ret = table[n]; delete[] table; return ret; }
However, if myfunc() is called very frequently from the application's inner loop, heap allocation can be a major source of slowdown.
QVarLengthArray is an attempt to work around this gap in the C++ language. It allocates a certain number of elements on the stack, and if you resize the array to a larger size, it automatically uses the heap instead. Stack allocation has the advantage that it is much faster than heap allocation.
Example:
int myfunc(int n) { QVarLengthArray<int, 1024> array(n + 1); ... return array[n]; }
In the example above, QVarLengthArray will preallocate 1024 elements on the stack and use them unless n + 1
is greater than 1024. If you omit the second template argument, QVarLengthArray's default of 256 is used.
QVarLengthArray's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *.
QVarLengthArray, like QVector, provides a resizable array data structure. The main differences between the two classes are:
- QVarLengthArray's API is much more low-level and it lacks some of QVector's functionality.
- QVarLengthArray doesn't initialize the memory if the value is a basic type. (QVector always does.)
- QVector uses implicit sharing as a memory optimization. QVarLengthArray doesn't provide that feature; however, it usually produces slightly better performance due to reduced overhead, especially in tight loops.
In summary, QVarLengthArray is a low-level optimization class that only makes sense in very specific cases. It is used a few places inside Qt and was added to Qt's public API for the convenience of advanced users.
See also QVector, QList, and QLinkedList.
Member Type Documentation
typedef QVarLengthArray::const_iterator
Typedef for const T *. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::const_pointer
Typedef for const T *. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::const_reference
Typedef for const T &. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::const_reverse_iterator
Typedef for std::reverse_iterator<const T*>
. Provided for STL compatibility.
This typedef was introduced in Qt 5.6.
typedef QVarLengthArray::difference_type
Typedef for ptrdiff_t. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::iterator
Typedef for T *. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::pointer
Typedef for T *. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::reference
Typedef for T &. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::reverse_iterator
Typedef for std::reverse_iterator<T*>
. Provided for STL compatibility.
This typedef was introduced in Qt 5.6.
typedef QVarLengthArray::size_type
Typedef for int. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
typedef QVarLengthArray::value_type
Typedef for T. Provided for STL compatibility.
This typedef was introduced in Qt 4.7.
Related Non-Members
bool QVarLengthArray::operator!=(const int &left, const int &right)
Returns true
if string s1 is not equal to string s2; otherwise returns false
.
The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().
This function was introduced in Qt 5.0.
bool QVarLengthArray::operator==(const int &left, const int &right)
Returns true
if s1 and s2 are equal; otherwise returns false.
This function was introduced in Qt 5.0.