QVarLengthArray Class
template <typename T, qsizetype Prealloc = 256> class QVarLengthArray
The QVarLengthArray class provides a low-level variable-length array. More…
| Header | #include <QVarLengthArray> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| typedef | value_type |
| typedef | iterator |
| typedef | const_iterator |
| typedef | reverse_iterator |
| typedef | const_reverse_iterator |
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) |
| qsizetype | size() const |
| qsizetype | count() const |
| qsizetype | length() const |
| qsizetype | capacity() const |
| bool | isEmpty() const |
| bool | empty() const |
| T * | data() |
| const T * | data() const |
| const T * | constData() const |
| void | reserve(qsizetype n) |
| void | squeeze() |
| void | clear() |
| void | resize(qsizetype n) |
| void | resize(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 |
| T | value(qsizetype i) const |
| T | value(qsizetype i, const T &def) const |
| void | append(const T &v) |
| void | append(T &&v) |
| void | append(const T *buf, qsizetype n) |
| void | push_back(const T &v) |
| void | push_back(T &&v) |
| void | prepend(const T &v) |
| void | prepend(T &&v) |
| T & | emplace_back(Args &&...args) |
| void | pop_back() |
| void | removeLast() |
| iterator | insert(It before, const T &v) |
| void | insert(qsizetype i, const T &v) |
| iterator | insert(It before, T &&v) |
| void | insert(qsizetype i, T &&v) |
| iterator | insert(It before, qsizetype n, const T &v) |
| void | insert(qsizetype i, qsizetype n, const T &v) |
| void | replace(qsizetype i, const T &v) |
| void | fill(const T &v, qsizetype newSize = -1) |
| void | removeAt(qsizetype i) |
| void | removeFirst() |
| qsizetype | removeAll(const AT &t) |
| bool | removeOne(const AT &t) |
| qsizetype | removeIf(Predicate pred) |
| QVarLengthArray & | assign(qsizetype n, const T &t) |
| QVarLengthArray & | assign(It first, It last) |
| QVarLengthArray & | assign(std::initializer_list<T> list) |
| void | shrink_to_fit() |
| iterator | emplace(const_iterator before, Args &&...args) |
| QVarLengthArray & | operator+=(const T &v) |
| QVarLengthArray & | operator+=(T &&v) |
| QVarLengthArray & | operator<<(const T &v) |
| QVarLengthArray & | operator<<(T &&v) |
| void | append(std::initializer_list<T> list) |
| iterator | erase(const_iterator first, const_iterator last) |
| iterator | erase(const_iterator pos) |
| void | remove(qsizetype i, qsizetype n = 1) |
| qsizetype | indexOf(const U &v, qsizetype from = 0) const |
| qsizetype | lastIndexOf(const U &v, qsizetype from = -1) const |
| bool | contains(const U &v) const |
| iterator | begin() |
| iterator | end() |
| const_iterator | begin() const |
| const_iterator | end() const |
| const_iterator | cbegin() const |
| const_iterator | cend() const |
| const_iterator | constBegin() const |
| const_iterator | constEnd() const |
| reverse_iterator | rbegin() |
| reverse_iterator | rend() |
| const_reverse_iterator | rbegin() const |
| const_reverse_iterator | rend() const |
| const_reverse_iterator | crbegin() const |
| const_reverse_iterator | crend() const |
Static Public Members
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.
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.
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().
qsizetype QVarLengthArray::max_size()
Returns the largest number of items the array can theoretically hold. This function is provided for STL compatibility.
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.
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.