6.0.0-rc4Source

QVarLengthArray Class

template <typename T, qsizetype Prealloc = 256> class QVarLengthArray

The QVarLengthArray class provides a low-level variable-length array. More…

Header#include <QVarLengthArray>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QVarLengthArray()
QVarLengthArray(qsizetype size)
QVarLengthArray(qsizetype size, const T &v)
QVarLengthArray(std::initializer_list<T> list)
QVarLengthArray(It first, It last)
QVarLengthArray(const QVarLengthArray &o)
QVarLengthArray(QVarLengthArray &&o)
~QVarLengthArray()
QVarLengthArray &operator=(const QVarLengthArray &o)
QVarLengthArray &operator=(QVarLengthArray &&o)
QVarLengthArray &operator=(std::initializer_list<T> list)
qsizetypesize() const
qsizetypecount() const
qsizetypelength() const
qsizetypecapacity() const
boolisEmpty() const
boolempty() const
T *data()
const T *data() const
const T *constData() const
voidreserve(qsizetype n)
voidsqueeze()
voidclear()
voidresize(qsizetype n)
voidresize(qsizetype n, const T &v)
T &operator[](qsizetype i)
const T &operator[](qsizetype i) const
const T &at(qsizetype i) const
T &front()
const T &front() const
T &back()
const T &back() const
T &first()
const T &first() const
T &last()
const T &last() const
Tvalue(qsizetype i) const
Tvalue(qsizetype i, const T &def) const
voidappend(const T &v)
voidappend(T &&v)
voidappend(const T *buf, qsizetype n)
voidpush_back(const T &v)
voidpush_back(T &&v)
voidprepend(const T &v)
voidprepend(T &&v)
T &emplace_back(Args &&...args)
voidpop_back()
voidremoveLast()
iteratorinsert(It before, const T &v)
voidinsert(qsizetype i, const T &v)
iteratorinsert(It before, T &&v)
voidinsert(qsizetype i, T &&v)
iteratorinsert(It before, qsizetype n, const T &v)
voidinsert(qsizetype i, qsizetype n, const T &v)
voidreplace(qsizetype i, const T &v)
voidfill(const T &v, qsizetype newSize = -1)
voidremoveAt(qsizetype i)
voidremoveFirst()
qsizetyperemoveAll(const AT &t)
boolremoveOne(const AT &t)
qsizetyperemoveIf(Predicate pred)
QVarLengthArray &assign(qsizetype n, const T &t)
QVarLengthArray &assign(It first, It last)
QVarLengthArray &assign(std::initializer_list<T> list)
voidshrink_to_fit()
iteratoremplace(const_iterator before, Args &&...args)
QVarLengthArray &operator+=(const T &v)
QVarLengthArray &operator+=(T &&v)
QVarLengthArray &operator<<(const T &v)
QVarLengthArray &operator<<(T &&v)
voidappend(std::initializer_list<T> list)
iteratorerase(const_iterator first, const_iterator last)
iteratorerase(const_iterator pos)
voidremove(qsizetype i, qsizetype n = 1)
qsizetypeindexOf(const U &v, qsizetype from = 0) const
qsizetypelastIndexOf(const U &v, qsizetype from = -1) const
boolcontains(const U &v) const
iteratorbegin()
iteratorend()
const_iteratorbegin() const
const_iteratorend() const
const_iteratorcbegin() const
const_iteratorcend() const
const_iteratorconstBegin() const
const_iteratorconstEnd() const
reverse_iteratorrbegin()
reverse_iteratorrend()
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const

Static Public Members

qsizetypemax_size()
qsizetypemaxSize()

Detailed Description

QVarLengthArray<T, Prealloc> is a resizable array that stores its first Prealloc items inside the object itself and switches to heap memory only when it grows beyond that. For short-lived, small arrays, such as temporary buffers in a function, this avoids a heap allocation.

QVarLengthArray<int, 16> values;
for (int i = 0; i < 10; ++i)
    values.append(i * i);

See also QList.

Differences from Qt

Indexes are not range-checked except by value(). The relational operators compare items with operator<() only.

Member Type Documentation

typedef QVarLengthArray::const_iterator

The const iterator type, a pointer to const T.

typedef QVarLengthArray::const_reverse_iterator

The const reverse iterator type.

typedef QVarLengthArray::iterator

The non-const iterator type, a pointer to T.

typedef QVarLengthArray::reverse_iterator

The non-const reverse iterator type.

typedef QVarLengthArray::value_type

The type of the items stored in the array, T. Provided for STL compatibility.

Member Function Documentation

QVarLengthArray::QVarLengthArray()

Constructs an empty array.

[explicit] QVarLengthArray::QVarLengthArray(qsizetype size)

Constructs an array with size value-initialized items.

QVarLengthArray::QVarLengthArray(qsizetype size, const T &v)

This is an overloaded function.

Constructs an array with size copies of v.

This function was introduced in 6.4.

QVarLengthArray::QVarLengthArray(std::initializer_list<T> list)

Constructs an array from the items in the initializer list list.

template <typename It, typename = typename std::iterator_traits<It>::iterator_category>QVarLengthArray::QVarLengthArray(It first, It last)

Constructs an array with a copy of the items in the range [first, last).

QVarLengthArray::QVarLengthArray(const QVarLengthArray &o)

Constructs a copy of the array o.

QVarLengthArray::QVarLengthArray(QVarLengthArray &&o)

Constructs an array by moving the contents of o, which is left empty.

QVarLengthArray::~QVarLengthArray()

Destroys the array and all its items.

void QVarLengthArray::append(const T &v)

Appends v to the end of the array.

See also prepend(), insert() and push_back().

void QVarLengthArray::append(T &&v)

This is an overloaded function.

Moves v to the end of the array.

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

This is an overloaded function.

Appends n items copied from the buffer buf to the end of the array.

void QVarLengthArray::append(std::initializer_list<T> list)

This is an overloaded function.

Appends the items in the initializer list list to the array.

QVarLengthArray & QVarLengthArray::assign(qsizetype n, const T &t)

Replaces the contents of the array with n copies of t and returns a reference to the array.

This function was introduced in 6.6.

See also fill().

template <typename It, typename = typename std::iterator_traits<It>::iterator_category>QVarLengthArray & QVarLengthArray::assign(It first, It last)

This is an overloaded function.

Replaces the contents of the array with a copy of the items in the range [first, last) and returns a reference to the array. The range must not refer to this array.

This function was introduced in 6.6.

QVarLengthArray & QVarLengthArray::assign(std::initializer_list<T> list)

This is an overloaded function.

Replaces the contents of the array with the items in list and returns a reference to the array.

This function was introduced in 6.6.

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

Returns the item at index position i. i must be a valid index.

See also value().

T & QVarLengthArray::back()

Returns a reference to the last item in the array. The array must not be empty.

See also front() and isEmpty().

const T & QVarLengthArray::back() const

This is an overloaded function.

Returns a const reference to the last item in the array.

iterator QVarLengthArray::begin()

Returns an iterator pointing to the first item in the array.

const_iterator QVarLengthArray::begin() const

This is an overloaded function.

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

qsizetype QVarLengthArray::capacity() const

Returns the maximum number of items the array can hold without reallocating.

See also reserve() and squeeze().

const_iterator QVarLengthArray::cbegin() const

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

const_iterator QVarLengthArray::cend() const

Returns a const iterator pointing just after the last item in the array.

void QVarLengthArray::clear()

Removes all the items from the array. The capacity is unchanged.

See also resize().

const_iterator QVarLengthArray::constBegin() const

Same as cbegin().

const T * QVarLengthArray::constData() const

Returns a const pointer to the data stored in the array.

See also data().

const_iterator QVarLengthArray::constEnd() const

Same as cend().

template <typename U>bool QVarLengthArray::contains(const U &v) const

Returns true if the array contains an item equal to v; otherwise returns false.

See also indexOf().

qsizetype QVarLengthArray::count() const

Same as size().

const_reverse_iterator QVarLengthArray::crbegin() const

Same as rbegin() const.

const_reverse_iterator QVarLengthArray::crend() const

Same as rend() const.

T * QVarLengthArray::data()

Returns a pointer to the data stored in the array. The pointer stays valid until the array is reallocated or destroyed.

See also constData().

const T * QVarLengthArray::data() const

This is an overloaded function.

Returns a const pointer to the data stored in the array.

template <typename... Args>iterator QVarLengthArray::emplace(const_iterator before, Args &&...args)

Constructs a new item from args and inserts it before the item pointed to by before. Returns an iterator to the new item.

See also insert() and emplace_back().

template <typename... Args>T & QVarLengthArray::emplace_back(Args &&...args)

Constructs a new item at the end of the array from args and returns a reference to it.

See also append().

bool QVarLengthArray::empty() const

Same as isEmpty(). This function is provided for STL compatibility.

iterator QVarLengthArray::end()

Returns an iterator pointing just after the last item in the array.

const_iterator QVarLengthArray::end() const

This is an overloaded function.

Returns a const iterator pointing just after the last item in the array.

iterator QVarLengthArray::erase(const_iterator first, const_iterator last)

Removes the items in the range [first, last) and returns an iterator to the item that followed the range.

See also remove() and insert().

iterator QVarLengthArray::erase(const_iterator pos)

This is an overloaded function.

Removes the item pointed to by pos and returns an iterator to the item that followed it.

void QVarLengthArray::fill(const T &v, qsizetype newSize = -1)

Assigns v to every item in the array. If newSize is not negative, the array is first resized to newSize items.

See also resize().

T & QVarLengthArray::first()

Same as front().

const T & QVarLengthArray::first() const

This is an overloaded function.

Same as front() const.

T & QVarLengthArray::front()

Returns a reference to the first item in the array. The array must not be empty.

See also back() and isEmpty().

const T & QVarLengthArray::front() const

This is an overloaded function.

Returns a const reference to the first item in the array.

template <typename U>qsizetype QVarLengthArray::indexOf(const U &v, qsizetype from = 0) const

Returns the index position of the first item equal to v, searching forward from index position from. Returns -1 if no item matches. The comparison uses operator==() between the item type and U.

See also lastIndexOf() and contains().

template <typename It, typename = std::enable_if_t<std::is_same_v<It, iterator> || std::is_same_v<It, const_iterator>>>iterator QVarLengthArray::insert(It before, const T &v)

Inserts v before the item pointed to by the iterator before and returns an iterator to the inserted item. The iterator type may be iterator or const_iterator.

See also append(), prepend() and erase().

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

This is an overloaded function.

Inserts v at index position i. i must be between 0 and size().

template <typename It, typename = std::enable_if_t<std::is_same_v<It, iterator> || std::is_same_v<It, const_iterator>>>iterator QVarLengthArray::insert(It before, T &&v)

This is an overloaded function.

Moves v before the item pointed to by before and returns an iterator to the inserted item.

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

This is an overloaded function.

Moves v to index position i. i must be between 0 and size().

template <typename It, typename = std::enable_if_t<std::is_same_v<It, iterator> || std::is_same_v<It, const_iterator>>>iterator QVarLengthArray::insert(It before, qsizetype n, const T &v)

This is an overloaded function.

Inserts n copies of v before the item pointed to by before and returns an iterator to the first inserted item.

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

This is an overloaded function.

Inserts n copies of v at index position i. i must be between 0 and size().

bool QVarLengthArray::isEmpty() const

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

See also size() and clear().

T & QVarLengthArray::last()

Same as back().

const T & QVarLengthArray::last() const

This is an overloaded function.

Same as back() const.

template <typename U>qsizetype QVarLengthArray::lastIndexOf(const U &v, qsizetype from = -1) const

Returns the index position of the last item equal to v, searching backward from index position from. A negative from starts at the last item. Returns -1 if no item matches.

See also indexOf() and contains().

qsizetype QVarLengthArray::length() const

Same as size().

[static, constexpr] qsizetype QVarLengthArray::max_size()

Returns the largest number of items the array can theoretically hold. This function is provided for STL compatibility.

[static, constexpr] qsizetype QVarLengthArray::maxSize()

Returns the largest number of items the array can theoretically hold. This is the same as max_size().

This function was introduced in 6.8.

QVarLengthArray & QVarLengthArray::operator+=(const T &v)

Appends v to the array and returns a reference to the array.

QVarLengthArray & QVarLengthArray::operator+=(T &&v)

This is an overloaded function.

Moves v to the end of the array and returns a reference to the array.

QVarLengthArray & QVarLengthArray::operator<<(const T &v)

Appends v to the array and returns a reference to the array, so that calls can be chained.

QVarLengthArray & QVarLengthArray::operator<<(T &&v)

This is an overloaded function.

Moves v to the end of the array and returns a reference to the array, so that calls can be chained.

QVarLengthArray & QVarLengthArray::operator=(const QVarLengthArray &o)

Assigns a copy of the array o to this array and returns a reference to this array.

QVarLengthArray & QVarLengthArray::operator=(QVarLengthArray &&o)

This is an overloaded function.

Moves the contents of o into this array, leaving o empty, and returns a reference to this array.

QVarLengthArray & QVarLengthArray::operator=(std::initializer_list<T> list)

This is an overloaded function.

Replaces the contents of this array with the items in list and returns a reference to this array.

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

Returns a reference to the item at index position i, which can be assigned to. i must be a valid index.

See also at() and value().

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

This is an overloaded function.

Same as at(i).

void QVarLengthArray::pop_back()

Removes the last item in the array. The array must not be empty.

See also removeLast() and removeFirst().

void QVarLengthArray::prepend(const T &v)

Inserts v at the beginning of the array.

See also append() and insert().

void QVarLengthArray::prepend(T &&v)

This is an overloaded function.

Moves v to the beginning of the array.

void QVarLengthArray::push_back(const T &v)

Same as append(v). This function is provided for STL compatibility.

void QVarLengthArray::push_back(T &&v)

This is an overloaded function.

Same as append(v) for an rvalue.

reverse_iterator QVarLengthArray::rbegin()

Returns a reverse iterator pointing to the last item in the array.

const_reverse_iterator QVarLengthArray::rbegin() const

This is an overloaded function.

Returns a const reverse iterator pointing to the last item in the array.

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

Removes n items starting at index position i. The range must be valid.

See also removeAt() and erase().

template <typename AT = T>qsizetype QVarLengthArray::removeAll(const AT &t)

Removes all items that compare equal to t and returns the number of items removed.

This function was introduced in 6.1.

See also removeOne() and removeIf().

void QVarLengthArray::removeAt(qsizetype i)

Removes the item at index position i. i must be a valid index.

See also remove(), removeFirst() and removeLast().

void QVarLengthArray::removeFirst()

Removes the first item in the array. The array must not be empty.

See also removeLast() and removeAt().

template <typename Predicate>qsizetype QVarLengthArray::removeIf(Predicate pred)

Removes all items for which the predicate pred returns true and returns the number of items removed.

This function was introduced in 6.1.

See also removeAll().

void QVarLengthArray::removeLast()

Same as pop_back().

template <typename AT = T>bool QVarLengthArray::removeOne(const AT &t)

Removes the first item that compares equal to t. Returns true if an item was removed; otherwise returns false.

This function was introduced in 6.1.

See also removeAll().

reverse_iterator QVarLengthArray::rend()

Returns a reverse iterator pointing just before the first item in the array.

const_reverse_iterator QVarLengthArray::rend() const

This is an overloaded function.

Returns a const reverse iterator pointing just before the first item in the array.

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

Replaces the item at index position i with v. i must be a valid index.

See also insert() and remove().

void QVarLengthArray::reserve(qsizetype n)

Attempts to allocate memory for at least n items. Nothing happens if the capacity is already large enough.

See also capacity() and squeeze().

void QVarLengthArray::resize(qsizetype n)

Sets the size of the array to n. If n is larger than the current size, the new items are value-initialized; if it is smaller, items are removed from the end.

See also size() and reserve().

void QVarLengthArray::resize(qsizetype n, const T &v)

This is an overloaded function.

Sets the size of the array to n. New items are copies of v.

void QVarLengthArray::shrink_to_fit()

Same as squeeze(). This function is provided for STL compatibility.

qsizetype QVarLengthArray::size() const

Returns the number of items in the array.

See also isEmpty() and resize().

void QVarLengthArray::squeeze()

Releases any memory not required to store the items. If the items fit in the preallocated storage, the array moves back into it.

See also reserve() and capacity().

T QVarLengthArray::value(qsizetype i) const

Returns the item at index position i, or a default-constructed value if i is out of range.

See also at().

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

This is an overloaded function.

Returns the item at index position i, or def if i is out of range.