IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QVarLengthArray Class

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QVarLengthArray Class

  • Header: QVarLengthArray

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Inherits: QVLABase and QVLAStorage

  • Inherited By:

  • Group: QVarLengthArray is part of tools

Detailed Description

The C++ language doesn't support variable-length arrays on the stack. For example, the following code won't compile:

 
Sélectionnez
int myfunc(int n)
{
    int table[n + 1];  // WRONG
    ...
    return table[n];
}

The alternative is to allocate the array on the heap (with new):

 
Sélectionnez
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:

 
Sélectionnez
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 QList, 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 QList's functionality.

  • QVarLengthArray doesn't initialize the memory if the value is a basic type. (QList always does.)

  • QList 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

See also QList

Member Type Documentation

 

[alias] QVarLengthArray::const_iterator

Typedef for const T *. Provided for STL compatibility.

[alias] QVarLengthArray::const_pointer

Typedef for const T *. Provided for STL compatibility.

[alias] QVarLengthArray::const_reference

Typedef for const T &. Provided for STL compatibility.

[alias, since 5.6] QVarLengthArray::const_reverse_iterator

Typedef for std::reverse_iterator<const T*>. Provided for STL compatibility.

This typedef was introduced in Qt 5.6.

[alias] QVarLengthArray::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

[alias] QVarLengthArray::iterator

Typedef for T *. Provided for STL compatibility.

[alias] QVarLengthArray::pointer

Typedef for T *. Provided for STL compatibility.

[alias] QVarLengthArray::reference

Typedef for T &. Provided for STL compatibility.

[alias, since 5.6] QVarLengthArray::reverse_iterator

Typedef for std::reverse_iterator<T*>. Provided for STL compatibility.

This typedef was introduced in Qt 5.6.

[alias] QVarLengthArray::size_type

Typedef for int. Provided for STL compatibility.

[alias] QVarLengthArray::value_type

Typedef for T. Provided for STL compatibility.

Member Function Documentation

 

void QVarLengthArray::insert(qsizetype i, T &&value)

void QVarLengthArray::insert(qsizetype i, const T &value)

Inserts value at index position i in the array. If i is 0, the value is prepended to the vector. If i is size(), the value is appended to the vector.

For large arrays, this operation can be slow (linear time), because it requires moving all the items at indexes i and above by one position further in memory. If you want a container class that provides a fast insert() function, use std::list instead.

See Also

See also remove()

QVarLengthArray::iterator QVarLengthArray::insert(QVarLengthArray::const_iterator before, T &&value)

QVarLengthArray::iterator QVarLengthArray::insert(QVarLengthArray::const_iterator before, const T &value)

This is an overloaded function.

Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.

QVarLengthArray::QVarLengthArray()

Constructs an array with an initial size of zero.

[explicit] QVarLengthArray::QVarLengthArray(qsizetype size)

Constructs an array with an initial size of size elements.

If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), the elements are not initialized. For other types, the elements are initialized with a default-constructed value.

[explicit, since 6.4] QVarLengthArray::QVarLengthArray(qsizetype size, const T &v)

Constructs an array with an initial size of size elements filled with copies of v.

This constructor is only available when T is copy-constructible.

This function was introduced in Qt 6.4.

See Also

See also size(), squeeze()

QVarLengthArray::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other = Prealloc)

Constructs a copy of other.

[since 6.0] QVarLengthArray::QVarLengthArray(QVarLengthArray<T, Prealloc> &&other = Prealloc)

Move-constructs this variable-length array from other. After the move, other is empty.

This function was introduced in Qt 6.0.

[since 5.5] QVarLengthArray::QVarLengthArray(std::initializer_list<T> args)

Constructs an array from the std::initializer_list given by args.

This constructor is only enabled if the compiler supports C++11 initializer lists.

This function was introduced in Qt 5.5.

[since 5.14] QVarLengthArray::QVarLengthArray(InputIterator first, InputIterator last)

Constructs an array with the contents in the iterator range [first, last).

The value type of InputIterator must be convertible to T.

This function was introduced in Qt 5.14.

QVarLengthArray::~QVarLengthArray()

Destroys the array.

void QVarLengthArray::append(const T &t)

Appends item t to the array, extending the array if necessary.

See Also

See also removeLast()

[since 5.9] void QVarLengthArray::append(T &&t)

This function overloads append.

Unlike the lvalue overload of append(), passing a reference to an object that is already an element of *this leads to undefined behavior:

 
Sélectionnez
vla.append(std::move(vla[0])); // BUG: passing an object that is already in the container

This function was introduced in Qt 5.9.

void QVarLengthArray::append(const T *buf, qsizetype size)

Appends size amount of items referenced by buf to this array.

const T &QVarLengthArray::at(qsizetype i) const

Returns a reference to the item at index position i.

i must be a valid index position in the array (i.e., 0 <= i < size()).

See Also

See also value(), operator[]()

[since 5.0] T &QVarLengthArray::back()

Same as last(). Provided for STL-compatibility.

This function was introduced in Qt 5.0.

[since 5.0] const T &QVarLengthArray::back() const

This is an overloaded function.

This function was introduced in Qt 5.0.

QVarLengthArray::iterator QVarLengthArray::begin()

Returns an STL-style iterator pointing to the first item in the array.

See Also

See also constBegin(), end()

QVarLengthArray::const_iterator QVarLengthArray::begin() const

This is an overloaded function.

qsizetype QVarLengthArray::capacity() const

Returns the maximum number of elements that can be stored in the array without forcing a reallocation.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the array, call size().

See Also

See also reserve(), squeeze()

[since 5.0] QVarLengthArray::const_iterator QVarLengthArray::cbegin() const

Returns a const STL-style iterator pointing to the first item in the array.

This function was introduced in Qt 5.0.

See Also

See also begin(), cend()

[since 5.0] QVarLengthArray::const_iterator QVarLengthArray::cend() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the array.

This function was introduced in Qt 5.0.

See Also

See also cbegin(), end()

void QVarLengthArray::clear()

Removes all the elements from the array.

Same as resize(0).

QVarLengthArray::const_iterator QVarLengthArray::constBegin() const

Returns a const STL-style iterator pointing to the first item in the array.

See Also

See also begin(), constEnd()

const T *QVarLengthArray::constData() const

Returns a const pointer to the data stored in the array. The pointer can be used to access the items in the array. The pointer remains valid as long as the array isn't reallocated.

This function is mostly useful to pass an array to a function that accepts a plain C++ array.

See Also

See also data(), operator[]()

QVarLengthArray::const_iterator QVarLengthArray::constEnd() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the array.

See Also

See also constBegin(), end()

[since 5.3] bool QVarLengthArray::contains(const AT &value) const

Returns true if the array contains an occurrence of value; otherwise returns false.

This function requires the value type to have an implementation of operator==().

This function was introduced in Qt 5.3.

See Also

See also indexOf(), lastIndexOf()

qsizetype QVarLengthArray::count() const

Same as size().

See Also

See also isEmpty(), resize()

[since 5.6] QVarLengthArray::const_reverse_iterator QVarLengthArray::crbegin() const

Returns a const STL-style reverse iterator pointing to the first item in the variable length array, in reverse order.

This function was introduced in Qt 5.6.

See Also

See also begin(), rbegin(), rend()

[since 5.6] QVarLengthArray::const_reverse_iterator QVarLengthArray::crend() const

Returns a const STL-style reverse iterator pointing to one past the last item in the variable length array, in reverse order.

This function was introduced in Qt 5.6.

See Also

See also end(), rend(), rbegin()

T *QVarLengthArray::data()

Returns a pointer to the data stored in the array. The pointer can be used to access and modify the items in the array.

Example:

 
Sélectionnez
QVarLengthArray&lt;int&gt; array(10);
int *data = array.data();
for (int i = 0; i &lt; 10; ++i)
    data[i] = 2 * i;

The pointer remains valid as long as the array isn't reallocated.

This function is mostly useful to pass an array to a function that accepts a plain C++ array.

See Also

See also constData(), operator[]()

const T *QVarLengthArray::data() const

This is an overloaded function.

[since 6.3] QVarLengthArray::iterator QVarLengthArray::emplace(QVarLengthArray::const_iterator pos, Args &&... args)

Inserts an item in front of the item pointed to by the iterator pos, passing args to its constructor.

Returns an iterator pointing at the emplaced item.

This function was introduced in Qt 6.3.

[since 6.3] T &QVarLengthArray::emplace_back(Args &&... args)

Inserts an item at the back of this QVarLengthArray, passing args to its constructor.

Returns a reference to the emplaced item.

This function was introduced in Qt 6.3.

[since 5.0] bool QVarLengthArray::empty() const

Returns true if the array has size 0; otherwise returns false.

Same as isEmpty(). Provided for STL-compatibility.

This function was introduced in Qt 5.0.

QVarLengthArray::iterator QVarLengthArray::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the array.

See Also

See also begin(), constEnd()

QVarLengthArray::const_iterator QVarLengthArray::end() const

This is an overloaded function.

QVarLengthArray::iterator QVarLengthArray::erase(QVarLengthArray::const_iterator pos)

Removes the item pointed to by the iterator pos from the vector, and returns an iterator to the next item in the vector (which may be end()).

See Also

See also insert(), remove()

QVarLengthArray::iterator QVarLengthArray::erase(QVarLengthArray::const_iterator begin, QVarLengthArray::const_iterator end)

This is an overloaded function.

Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.

T &QVarLengthArray::first()

Returns a reference to the first item in the array. The array must not be empty. If the array can be empty, check isEmpty() before calling this function.

See Also

See also last(), isEmpty()

const T &QVarLengthArray::first() const

This is an overloaded function.

[since 5.0] T &QVarLengthArray::front()

Same as first(). Provided for STL-compatibility.

This function was introduced in Qt 5.0.

[since 5.0] const T &QVarLengthArray::front() const

This is an overloaded function.

This function was introduced in Qt 5.0.

[since 5.3] qsizetype QVarLengthArray::indexOf(const AT &value, qsizetype from = 0) const

Returns the index position of the first occurrence of value in the array, searching forward from index position from. Returns -1 if no item matched.

This function requires the value type to have an implementation of operator==().

This function was introduced in Qt 5.3.

See Also

See also lastIndexOf(), contains()

void QVarLengthArray::insert(qsizetype i, qsizetype count, const T &value)

This is an overloaded function.

Inserts count copies of value at index position i in the vector.

QVarLengthArray::iterator QVarLengthArray::insert(QVarLengthArray::const_iterator before, qsizetype count, const T &value)

Inserts count copies of value in front of the item pointed to by the iterator before. Returns an iterator pointing at the first of the inserted items.

bool QVarLengthArray::isEmpty() const

Returns true if the array has size 0; otherwise returns false.

See Also

See also size(), resize()

T &QVarLengthArray::last()

Returns a reference to the last item in the array. The array must not be empty. If the array can be empty, check isEmpty() before calling this function.

See Also

See also first(), isEmpty()

const T &QVarLengthArray::last() const

This is an overloaded function.

[since 5.3] qsizetype QVarLengthArray::lastIndexOf(const AT &value, qsizetype from = -1) const

Returns the index position of the last occurrence of the value value in the array, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

This function requires the value type to have an implementation of operator==().

This function was introduced in Qt 5.3.

See Also

See also indexOf(), contains()

[since 5.0] qsizetype QVarLengthArray::length() const

Same as size().

This function was introduced in Qt 5.0.

See Also

See also isEmpty(), resize()

[since 5.0] void QVarLengthArray::pop_back()

Same as removeLast(). Provided for STL-compatibility.

This function was introduced in Qt 5.0.

[since 5.0] void QVarLengthArray::push_back(const T &t)

Appends item t to the array, extending the array if necessary. Provided for STL-compatibility.

This function was introduced in Qt 5.0.

[since 5.9] void QVarLengthArray::push_back(T &&t)

This function overloads push_back.

Unlike the lvalue overload of push_back(), passing a reference to an object that is already an element of *this leads to undefined behavior:

 
Sélectionnez
vla.push_back(std::move(vla[0])); // BUG: passing an object that is already in the container

This function was introduced in Qt 5.9.

[since 5.6] QVarLengthArray::reverse_iterator QVarLengthArray::rbegin()

Returns a STL-style reverse iterator pointing to the first item in the variable length array, in reverse order.

This function was introduced in Qt 5.6.

See Also

See also begin(), crbegin(), rend()

[since 5.6] QVarLengthArray::const_reverse_iterator QVarLengthArray::rbegin() const

This is an overloaded function.

This function was introduced in Qt 5.6.

void QVarLengthArray::remove(qsizetype i, qsizetype count = 1)

This is an overloaded function.

Removes count elements from the middle of the array, starting at index position i.

See Also

See also insert(), replace()

[since 6.1] qsizetype QVarLengthArray::removeAll(const AT &t)

Removes all elements that compare equal to t from the array. Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

See Also

See also removeOne()

[since 6.1] qsizetype QVarLengthArray::removeIf(Predicate pred)

Removes all elements for which the predicate pred returns true from the array. Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

See Also

See also removeAll()

void QVarLengthArray::removeLast()

Decreases the size of the array by one. The allocated size is not changed.

See Also

See also append()

[since 6.1] bool QVarLengthArray::removeOne(const AT &t)

Removes the first element that compares equal to t from the array. Returns whether an element was, in fact, removed.

This function was introduced in Qt 6.1.

See Also

See also removeAll()

[since 5.6] QVarLengthArray::reverse_iterator QVarLengthArray::rend()

Returns a STL-style reverse iterator pointing to one past the last item in the variable length array, in reverse order.

This function was introduced in Qt 5.6.

See Also

See also end(), crend(), rbegin()

[since 5.6] QVarLengthArray::const_reverse_iterator QVarLengthArray::rend() const

This is an overloaded function.

This function was introduced in Qt 5.6.

void QVarLengthArray::replace(qsizetype i, const T &value)

Replaces the item at index position i with value.

i must be a valid index position in the array (i.e., 0 <= i < size()).

See Also

See also operator[](), remove()

void QVarLengthArray::reserve(qsizetype size)

Attempts to allocate memory for at least size elements. If you know in advance how large the array can get, you can call this function and if you call resize() often, you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QVarLengthArray will be a bit slower.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the array, call resize().

See Also

See also capacity(), squeeze()

void QVarLengthArray::resize(qsizetype size)

Sets the size of the array to size. If size is greater than the current size, elements are added to the end. If size is less than the current size, elements are removed from the end.

If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), new elements are not initialized. For other types, the elements are initialized with a default-constructed value.

See Also

See also size(), squeeze()

[since 6.4] void QVarLengthArray::resize(qsizetype size, const T &v)

Sets the size of the array to size. If size is greater than the current size, copies of v are added to the end. If size is less than the current size, elements are removed from the end.

This function is only available when T is copy-constructible.

This function was introduced in Qt 6.4.

See Also

See also size(), squeeze()

[since 5.10] void QVarLengthArray::shrink_to_fit()

Same as squeeze(). Provided for STL-compatibility.

This function was introduced in Qt 5.10.

qsizetype QVarLengthArray::size() const

Returns the number of elements in the array.

See Also

See also isEmpty(), resize()

[since 5.1] void QVarLengthArray::squeeze()

Releases any memory not required to store the items. If the container can fit its storage on the stack allocation, it will free the heap allocation and copy the elements back to the stack.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray's memory usage. In general, you will rarely ever need to call this function.

This function was introduced in Qt 5.1.

See Also

See also reserve(), capacity(), resize()

T QVarLengthArray::value(qsizetype i) const

Returns the value at index position i.

If the index i is out of bounds, the function returns a default-constructed value. If you are certain that i is within bounds, you can use at() instead, which is slightly faster.

See Also

See also at(), operator[]()

T QVarLengthArray::value(qsizetype i, const T &defaultValue) const

This is an overloaded function.

If the index i is out of bounds, the function returns defaultValue.

QVarLengthArray<T, Prealloc> &QVarLengthArray::operator+=(const T &value)

Appends value to the array and returns a reference to this vector.

See Also

See also append(), operator<<()

[since 5.11] QVarLengthArray<T, Prealloc> &QVarLengthArray::operator+=(T &&value)

This is an overloaded function.

This function was introduced in Qt 5.11.

See Also

See also append(), operator<<()

QVarLengthArray<T, Prealloc> &QVarLengthArray::operator<<(const T &value)

Appends value to the array and returns a reference to this vector.

See Also

See also append(), operator+=()

[since 5.11] QVarLengthArray<T, Prealloc> &QVarLengthArray::operator<<(T &&value)

This is an overloaded function.

This function was introduced in Qt 5.11.

See Also

See also append(), operator+=()

QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(const QVarLengthArray<T, Prealloc> &other = Prealloc)

Assigns other to this array and returns a reference to this array.

[since 6.0] QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(QVarLengthArray<T, Prealloc> &&other = Prealloc)

Move-assigns other to this array and returns a reference to this array. After the move, other is empty.

This function was introduced in Qt 6.0.

[since 5.5] QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(std::initializer_list<T> list)

Assigns the values of list to this array, and returns a reference to this array.

This constructor is only enabled if the compiler supports C++11 initializer lists.

This function was introduced in Qt 5.5.

T &QVarLengthArray::operator[](qsizetype i)

Returns a reference to the item at index position i.

i must be a valid index position in the array (i.e., 0 <= i < size()).

See Also

See also data(), at()

const T &QVarLengthArray::operator[](qsizetype i) const

This is an overloaded function.

Related Non-Members

 

[since 6.1] qsizetype erase(QVarLengthArray<T, Prealloc> &array = Prealloc, const AT &t)

Removes all elements that compare equal to t from the array array. Returns the number of elements removed, if any.

t is not allowed to be a reference to an element inside array. If you cannot be sure that this is not the case, take a copy of t and call this function with the copy.

This function was introduced in Qt 6.1.

See Also

See also erase_if()

[since 6.1] qsizetype erase_if(QVarLengthArray<T, Prealloc> &array = Prealloc, Predicate pred)

Removes all elements for which the predicate pred returns true from the list array. Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

See Also

See also erase()

[since 5.14] size_t qHash(const QVarLengthArray<T, Prealloc> &key = Prealloc, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 5.14.

bool operator!=(const QVarLengthArray<T, Prealloc1> &left = Prealloc1, const QVarLengthArray<T, Prealloc2> &right = Prealloc2)

Returns true if the two arrays, specified by left and right, are not equal.

Two arrays are considered equal if they contain the same values in the same order.

This function requires the value type to have an implementation of operator==().

See Also

See also operator==()

[since 5.6] bool operator<(const QVarLengthArray<T, Prealloc1> &lhs = Prealloc1, const QVarLengthArray<T, Prealloc2> &rhs = Prealloc2)

Returns true if variable length array lhs is lexicographically less than rhs; otherwise returns false.

This function requires the value type to have an implementation of operator<().

This function was introduced in Qt 5.6.

[since 5.6] bool operator<=(const QVarLengthArray<T, Prealloc1> &lhs = Prealloc1, const QVarLengthArray<T, Prealloc2> &rhs = Prealloc2)

Returns true if variable length array lhs is lexicographically less than or equal to rhs; otherwise returns false.

This function requires the value type to have an implementation of operator<().

This function was introduced in Qt 5.6.

bool operator==(const QVarLengthArray<T, Prealloc1> &left = Prealloc1, const QVarLengthArray<T, Prealloc2> &right = Prealloc2)

Returns true if the two arrays, specified by left and right, are equal.

Two arrays are considered equal if they contain the same values in the same order.

This function requires the value type to have an implementation of operator==().

See Also

See also operator!=()

[since 5.6] bool operator>(const QVarLengthArray<T, Prealloc1> &lhs = Prealloc1, const QVarLengthArray<T, Prealloc2> &rhs = Prealloc2)

Returns true if variable length array lhs is lexicographically greater than rhs; otherwise returns false.

This function requires the value type to have an implementation of operator<().

This function was introduced in Qt 5.6.

[since 5.6] bool operator>=(const QVarLengthArray<T, Prealloc1> &lhs = Prealloc1, const QVarLengthArray<T, Prealloc2> &rhs = Prealloc2)

Returns true if variable length array lhs is lexicographically greater than or equal to rhs; otherwise returns false.

This function requires the value type to have an implementation of operator<().

This function was introduced in Qt 5.6.

Obsolete Members for QVarLengthArray

The following members of class QVarLengthArray are deprecated. We strongly advise against using them in new code.

Obsolete Member Function Documentation

 
void QVarLengthArray::prepend(T &&value)

void QVarLengthArray::prepend(const T &value)

This function is deprecated since 6.3. We strongly advise against using it in new code.

This is slow. If you must, use insert(cbegin(), ~~~) instead.

Inserts value at the beginning of the array.

This is the same as vector.insert(0, value).

For large arrays, this operation can be slow (linear time), because it requires moving all the items in the vector by one position further in memory. If you want a container class that provides a fast prepend() function, use std::list instead.

See Also

See also append(), insert()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+