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

Public Functions

QVarLengthArray(int size = 0)
QVarLengthArray(const QVarLengthArray<T, Prealloc> &other = Prealloc)
QVarLengthArray(std::initializer_list<T> args)
~QVarLengthArray()
void append(const T &t)
void append(T &&t)
void append(const T *buf, int size)
const T &at(int idx) const
T &back()
const T &back() const
QVarLengthArray::iterator begin()
QVarLengthArray::const_iterator begin() const
int capacity() const
QVarLengthArray::const_iterator cbegin() const
QVarLengthArray::const_iterator cend() const
void clear()
QVarLengthArray::const_iterator constBegin() const
const T *constData() const
QVarLengthArray::const_iterator constEnd() const
bool contains(const T &t) const
int count() const
QVarLengthArray::const_reverse_iterator crbegin() const
QVarLengthArray::const_reverse_iterator crend() const
T *data()
const T *data() const
bool empty() const
QVarLengthArray::iterator end()
QVarLengthArray::const_iterator end() const
QVarLengthArray::iterator erase(QVarLengthArray::const_iterator begin, QVarLengthArray::const_iterator end)
QVarLengthArray::iterator erase(QVarLengthArray::const_iterator pos)
T &first()
const T &first() const
T &front()
const T &front() const
int indexOf(const T &t, int from = 0) const
void insert(int i, T &&t)
void insert(int i, const T &t)
void insert(int i, int n, const T &t)
QVarLengthArray::iterator insert(QVarLengthArray::const_iterator before, int n, const T &x)
QVarLengthArray::iterator insert(QVarLengthArray::const_iterator before, T &&x)
QVarLengthArray::iterator insert(QVarLengthArray::const_iterator before, const T &x)
bool isEmpty() const
T &last()
const T &last() const
int lastIndexOf(const T &t, int from = -1) const
int length() const
void pop_back()
void prepend(T &&t)
void prepend(const T &t)
void push_back(const T &t)
void push_back(T &&t)
QVarLengthArray::reverse_iterator rbegin()
QVarLengthArray::const_reverse_iterator rbegin() const
void remove(int i)
void remove(int i, int n)
void removeLast()
QVarLengthArray::reverse_iterator rend()
QVarLengthArray::const_reverse_iterator rend() const
void replace(int i, const T &t)
void reserve(int size)
void resize(int size)
void shrink_to_fit()
int size() const
void squeeze()
T value(int i) const
T value(int i, const T &defaultValue) const
QVarLengthArray<T, Prealloc> &operator+=(const T &t)
QVarLengthArray<T, Prealloc> &operator+=(T &&t)
QVarLengthArray<T, Prealloc> &operator<<(const T &t)
QVarLengthArray<T, Prealloc> &operator<<(T &&t)
QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other = Prealloc)
QVarLengthArray<T, Prealloc> &operator=(std::initializer_list<T> list)
T &operator[](int idx)
const T &operator[](int idx) const

Detailed Description

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

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:

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.

Member Function Documentation

QVarLengthArray::QVarLengthArray(int size = 0)

Default constructs an instance of QVarLengthArray.

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

Default constructs an instance of QVarLengthArray.

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

Default constructs an instance of QVarLengthArray.

QVarLengthArray::~QVarLengthArray()

Destroys the instance of QVarLengthArray.

void QVarLengthArray::append(const T &t)

void QVarLengthArray::append(T &&t)

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

const T &QVarLengthArray::at(int idx) const

T &QVarLengthArray::back()

const T &QVarLengthArray::back() const

QVarLengthArray::iterator QVarLengthArray::begin()

QVarLengthArray::const_iterator QVarLengthArray::begin() const

int QVarLengthArray::capacity() const

QVarLengthArray::const_iterator QVarLengthArray::cbegin() const

QVarLengthArray::const_iterator QVarLengthArray::cend() const

void QVarLengthArray::clear()

QVarLengthArray::const_iterator QVarLengthArray::constBegin() const

const T *QVarLengthArray::constData() const

QVarLengthArray::const_iterator QVarLengthArray::constEnd() const

bool QVarLengthArray::contains(const T &t) const

int QVarLengthArray::count() const

QVarLengthArray::const_reverse_iterator QVarLengthArray::crbegin() const

QVarLengthArray::const_reverse_iterator QVarLengthArray::crend() const

T *QVarLengthArray::data()

const T *QVarLengthArray::data() const

bool QVarLengthArray::empty() const

QVarLengthArray::iterator QVarLengthArray::end()

QVarLengthArray::const_iterator QVarLengthArray::end() const

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

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

T &QVarLengthArray::first()

const T &QVarLengthArray::first() const

T &QVarLengthArray::front()

const T &QVarLengthArray::front() const

int QVarLengthArray::indexOf(const T &t, int from = 0) const

void QVarLengthArray::insert(int i, T &&t)

void QVarLengthArray::insert(int i, const T &t)

void QVarLengthArray::insert(int i, int n, const T &t)

QVarLengthArray::iterator QVarLengthArray::insert(QVarLengthArray::const_iterator before, int n, const T &x)

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

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

bool QVarLengthArray::isEmpty() const

T &QVarLengthArray::last()

const T &QVarLengthArray::last() const

int QVarLengthArray::lastIndexOf(const T &t, int from = -1) const

int QVarLengthArray::length() const

void QVarLengthArray::pop_back()

void QVarLengthArray::prepend(T &&t)

void QVarLengthArray::prepend(const T &t)

void QVarLengthArray::push_back(const T &t)

void QVarLengthArray::push_back(T &&t)

QVarLengthArray::reverse_iterator QVarLengthArray::rbegin()

QVarLengthArray::const_reverse_iterator QVarLengthArray::rbegin() const

void QVarLengthArray::remove(int i)

void QVarLengthArray::remove(int i, int n)

void QVarLengthArray::removeLast()

QVarLengthArray::reverse_iterator QVarLengthArray::rend()

QVarLengthArray::const_reverse_iterator QVarLengthArray::rend() const

void QVarLengthArray::replace(int i, const T &t)

void QVarLengthArray::reserve(int size)

void QVarLengthArray::resize(int size)

void QVarLengthArray::shrink_to_fit()

int QVarLengthArray::size() const

void QVarLengthArray::squeeze()

T QVarLengthArray::value(int i) const

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

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

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

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

QVarLengthArray<T, Prealloc> &QVarLengthArray::operator<<(T &&t)

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

Copy-assignment operator.

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

T &QVarLengthArray::operator[](int idx)

const T &QVarLengthArray::operator[](int idx) const