本页内容

QVarLengthArray Class

template <typename T, qsizetype Prealloc> class QVarLengthArray

QVarLengthArray 类提供了一个低级可变长度数组。更多内容...

头文件: #include <QVarLengthArray>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QVLABase 和 QVLAStorage

注意:该类中的所有函数均为可重入的。

公共类型

公共函数

QVarLengthArray()
QVarLengthArray(qsizetype size)
QVarLengthArray(std::initializer_list<T> args)
QVarLengthArray(InputIterator first, InputIterator last)
(since 6.4) QVarLengthArray(qsizetype size, const T &v)
QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
(since 6.0) QVarLengthArray(QVarLengthArray<T, Prealloc> &&other)
~QVarLengthArray()
void append(const T &t)
void append(const T *buf, qsizetype size)
void append(T &&t)
(since 6.6) QVarLengthArray<T, Prealloc> &assign(std::initializer_list<T> list)
(since 6.6) QVarLengthArray<T, Prealloc> &assign(InputIterator first, InputIterator last)
(since 6.6) QVarLengthArray<T, Prealloc> &assign(qsizetype n, const T &t)
const T &at(qsizetype i) const
T &back()
const T &back() const
QVarLengthArray<T, Prealloc>::iterator begin()
QVarLengthArray<T, Prealloc>::const_iterator begin() const
qsizetype capacity() const
QVarLengthArray<T, Prealloc>::const_iterator cbegin() const
QVarLengthArray<T, Prealloc>::const_iterator cend() const
void clear()
QVarLengthArray<T, Prealloc>::const_iterator constBegin() const
const T *constData() const
QVarLengthArray<T, Prealloc>::const_iterator constEnd() const
bool contains(const AT &value) const
qsizetype count() const
QVarLengthArray<T, Prealloc>::const_reverse_iterator crbegin() const
QVarLengthArray<T, Prealloc>::const_reverse_iterator crend() const
T *data()
const T *data() const
(since 6.3) QVarLengthArray<T, Prealloc>::iterator emplace(QVarLengthArray<T, Prealloc>::const_iterator pos, Args &&... args)
(since 6.3) T &emplace_back(Args &&... args)
bool empty() const
QVarLengthArray<T, Prealloc>::iterator end()
QVarLengthArray<T, Prealloc>::const_iterator end() const
QVarLengthArray<T, Prealloc>::iterator erase(QVarLengthArray<T, Prealloc>::const_iterator pos)
QVarLengthArray<T, Prealloc>::iterator erase(QVarLengthArray<T, Prealloc>::const_iterator begin, QVarLengthArray<T, Prealloc>::const_iterator end)
T &first()
const T &first() const
T &front()
const T &front() const
qsizetype indexOf(const AT &value, qsizetype from = 0) const
void insert(qsizetype i, T &&value)
void insert(qsizetype i, const T &value)
QVarLengthArray<T, Prealloc>::iterator insert(QVarLengthArray<T, Prealloc>::const_iterator before, qsizetype count, const T &value)
QVarLengthArray<T, Prealloc>::iterator insert(QVarLengthArray<T, Prealloc>::const_iterator before, T &&value)
QVarLengthArray<T, Prealloc>::iterator insert(QVarLengthArray<T, Prealloc>::const_iterator before, const T &value)
void insert(qsizetype i, qsizetype count, const T &value)
bool isEmpty() const
T &last()
const T &last() const
qsizetype lastIndexOf(const AT &value, qsizetype from = -1) const
qsizetype length() const
(since 6.8) qsizetype max_size() const
void pop_back()
void push_back(const T &t)
void push_back(T &&t)
QVarLengthArray<T, Prealloc>::reverse_iterator rbegin()
QVarLengthArray<T, Prealloc>::const_reverse_iterator rbegin() const
void remove(qsizetype i, qsizetype count = 1)
(since 6.1) qsizetype removeAll(const AT &t)
(since 6.12) void removeAt(qsizetype i)
(since 6.1) qsizetype removeIf(Predicate pred)
void removeLast()
(since 6.1) bool removeOne(const AT &t)
QVarLengthArray<T, Prealloc>::reverse_iterator rend()
QVarLengthArray<T, Prealloc>::const_reverse_iterator rend() const
void replace(qsizetype i, const T &value)
void reserve(qsizetype size)
void resize(qsizetype size)
(since 6.4) void resize(qsizetype size, const T &v)
void shrink_to_fit()
qsizetype size() const
void squeeze()
T value(qsizetype i) const
T value(qsizetype i, const T &defaultValue) const
QVarLengthArray<T, Prealloc> &operator+=(const T &value)
QVarLengthArray<T, Prealloc> &operator+=(T &&value)
QVarLengthArray<T, Prealloc> &operator<<(const T &value)
QVarLengthArray<T, Prealloc> &operator<<(T &&value)
(since 6.0) QVarLengthArray<T, Prealloc> &operator=(QVarLengthArray<T, Prealloc> &&other)
QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
QVarLengthArray<T, Prealloc> &operator=(std::initializer_list<T> list)
T &operator[](qsizetype i)
const T &operator[](qsizetype i) const

静态公共成员

(since 6.8) qsizetype maxSize()
(since 6.1) qsizetype erase(QVarLengthArray<T, Prealloc> &array, const AT &t)
(since 6.1) qsizetype erase_if(QVarLengthArray<T, Prealloc> &array, Predicate pred)
size_t qHash(const QVarLengthArray<T, Prealloc> &key, size_t seed = 0)
bool operator!=(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
bool operator<(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
bool operator<=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
bool operator==(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
bool operator>(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
bool operator>=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)

详细说明

C++ 语言不支持在栈上使用可变长度数组。例如,以下代码无法编译:

int myfunc_wrong(int n)
{
    int table[n + 1];  // WRONG
    //...
    return table[n];
}

另一种方法是在堆上分配数组(使用new ):

int myfunc_correct(int n)
{
    int *table = new int[n + 1];
    //...
    int ret = table[n];
    delete[] table;
    return ret;
}

然而,如果应用程序的内部循环中频繁调用 myfunc(),堆分配可能会成为导致性能下降的主要原因。

QVarLengthArray 旨在弥补 C++ 语言中的这一缺陷。它会在栈上分配一定数量的元素,当您将数组调整为更大尺寸时,它会自动改用堆进行分配。栈分配的优势在于其速度远快于堆分配。

示例:

int myfunc_q(int n)
{
    QVarLengthArray<int, 1024> array(n + 1);
    //...
    return array[n];
}

在上例中,QVarLengthArray 将在栈上预分配 1024 个元素,并使用这些元素,除非 `n + 1 ` 大于 1024。模板参数 `T ` 指定元素类型,而 `Prealloc ` 指定要在栈上预分配的元素数量;如果省略,`Prealloc ` 默认值为 256。

QVarLengthArray 的值类型必须是可赋值的数据类型。这涵盖了大多数常用数据类型,但编译器不允许您将QWidget 作为值存储;相反,应存储QWidget *。

QVarLengthArray 与QList 一样,提供了一种可调整大小的数组数据结构。这两个类的主要区别在于:

  • QVarLengthArray 的 API 更偏向底层,且缺少QList 的一些功能。
  • 如果值是基本类型,QVarLengthArray 不会初始化内存。(QList 总是会初始化。)
  • QList xml-ph-0000@deepl.internal 使用隐式共享作为内存优化手段。QVarLengthArray 未提供此功能;但通常由于开销减少,其性能会略微更优,特别是在紧凑的循环中。

总而言之,QVarLengthArray 是一个低级优化类,仅在非常特定的情况下才有意义。它在 Qt 内部的少数地方被使用,并被添加到 Qt 的公共 API 中,以方便高级用户使用。

另请参阅 QList 。

成员类型文档

[alias] QVarLengthArray::const_iterator

const T * 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::const_pointer

const T * 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::const_reference

const T & 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::const_reverse_iterator

std::reverse_iterator<const T*> 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::difference_type

ptrdiff_t 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::iterator

T * 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::pointer

T * 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::reference

T 的 typedef。为兼容 STL 而提供。

[alias] QVarLengthArray::reverse_iterator

std::reverse_iterator<T*> 的 typedef。为确保与 STL 兼容而提供。

[alias] QVarLengthArray::size_type

int 的 typedef 定义。为兼容 STL 而提供。

[alias] QVarLengthArray::value_type

T 的 typedef 定义。为确保与 STL 兼容而提供。

成员函数文档

[noexcept] QVarLengthArray::QVarLengthArray()

构建一个初始大小为零的数组。

[explicit] QVarLengthArray::QVarLengthArray(qsizetype size)

构建一个初始大小为size 个元素的数组。

如果值类型是基本类型(例如 char、int、float)或指针类型(例如QWidget *),则元素不会被初始化。对于其他类型,元素将使用默认构造函数生成的值进行初始化。

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

根据args 中给定的std::initializer_list构建一个数组。

template <typename InputIterator, QVarLengthArray<T, Prealloc>::if_input_iterator<InputIterator> = true> QVarLengthArray::QVarLengthArray(InputIterator first, InputIterator last)

构建一个数组,其内容来自迭代器范围 [first,last)。

InputIterator 的值类型必须可转换为T 。

约束

仅当InputIterator 满足LegacyInputIterator 的要求时,才会参与重载解析。

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

构建一个初始大小为size 个元素的数组,并用v 的副本填充该数组。

注意:此 构造函数仅在T 支持复制构造时可用。

该函数在 Qt 6.4 中引入。

另请参阅 size() 和squeeze()。

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

创建other 的副本。

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

从other 中通过移动构造器构建此可变长度数组。移动操作完成后,other 为空。

该函数在 Qt 6.0 中引入。

注意: 当std::is_nothrow_move_constructible_v<T> 为true 时,此 函数为 noexcept。

QVarLengthArray::~QVarLengthArray()

销毁该数组。

void QVarLengthArray::append(const T &t)

将元素t 追加到数组中,必要时会扩展数组。

另请参阅 removeLast()。

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

将buf 所引用的size 个项目追加到此数组中。

void QVarLengthArray::append(T &&t)

注意:与 append() 的 lvalue 重载不同,若将 一个已经是*this 元素的对象的引用作为参数传递,会导致未定义行为:

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

此函数重载了 `QVarLengthArray::append`。

[since 6.6] QVarLengthArray<T, Prealloc> &QVarLengthArray::assign(std::initializer_list<T> list)

将此容器的内容替换为list 中元素的副本。

该容器的大小将等于list 中的元素个数。

只有当list 中的元素数量超过容器的容量时,此函数才会分配内存。

该函数在 Qt 6.6 中引入。

[since 6.6] template <typename InputIterator, QVarLengthArray<T, Prealloc>::if_input_iterator<InputIterator> = true> QVarLengthArray<T, Prealloc> &QVarLengthArray::assign(InputIterator first, InputIterator last)

将该容器的内容替换为迭代器范围 [first,last) 中元素的副本。

该容器的大小将等于范围 [first,last) 中的元素个数。只有当范围中的元素个数超过容器的容量时,此函数才会分配内存。

如果任一参数是指向 *this 的迭代器,则行为未定义。

约束

仅当InputIterator 满足LegacyInputIterator 的要求时,才参与重载解析。

该函数在 Qt 6.6 中引入。

[since 6.6] QVarLengthArray<T, Prealloc> &QVarLengthArray::assign(qsizetype n, const T &t)

将该容器的内容替换为n 个t 的副本。

该容器的大小将等于n 。仅当n 超过容器容量时,本函数才会分配内存。

该函数在 Qt 6.6 中引入。

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

返回索引位置为i 的项的引用。

i 该索引位置必须是数组中的有效索引(即 0 <=i <size())。

另请参见 value() 和operator[]()。

T &QVarLengthArray::back()

与last() 相同。为兼容 STL 而提供。

const T &QVarLengthArray::back() const

这是一个重载函数。

QVarLengthArray<T, Prealloc>::iterator QVarLengthArray::begin()

返回一个指向数组中第一个元素的STL 风格迭代器。

另请参阅 constBegin() 和end()。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::begin() const

这是一个重载函数。

qsizetype QVarLengthArray::capacity() const

返回数组在不强制重新分配内存的情况下所能存储的元素的最大数量。

该函数的唯一目的是为精细调整QVarLengthArray 的内存使用提供一种手段。通常情况下,您几乎不需要调用此函数。若要了解数组中包含多少个元素,请调用size()。

另请参阅 reserve() 和squeeze()。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::cbegin() const

返回一个指向数组中第一个元素的常量STL 风格迭代器。

另请参阅 begin() 和cend()。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::cend() const

返回一个指向数组中最后一个元素之后那个“虚构”元素的、STL 风格的const迭代器。

另请参见 cbegin() 和end()。

void QVarLengthArray::clear()

从数组中移除所有元素。

与 resize(0) 相同。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::constBegin() const

返回一个指向数组中第一个元素的 constSTL 风格迭代器。

另请参阅 begin() 和constEnd()。

const T *QVarLengthArray::constData() const

返回指向数组中存储数据的常量指针。该指针可用于访问数组中的元素。只要数组未被重新分配,该指针就一直有效。

该函数主要用于将数组传递给接受普通 C++ 数组的函数。

另请参阅 data() 和operator[]()。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::constEnd() const

返回一个指向数组中最后一个元素之后虚构元素的常量STL 风格迭代器。

另请参阅 constBegin() 和end()。

template <typename AT = T> bool QVarLengthArray::contains(const AT &value) const

如果数组中包含value ,则返回true ;否则返回false 。

此函数要求值类型实现了operator==() 接口。

另请参见 indexOf() 和lastIndexOf()。

qsizetype QVarLengthArray::count() const

与size() 相同。

另请参见 isEmpty() 和resize()。

QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray::crbegin() const

返回一个指向可变长度数组中第一个元素的常量STL 风格反向迭代器,且顺序为反向。

另请参阅 begin()、rbegin(),以及rend()。

QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray::crend() const

返回一个常量STL 风格的反向迭代器,该迭代器指向可变长度数组中最后一个元素之后的下一个位置,且按反向顺序排列。

另请参阅 end()、rend() 和rbegin()。

T *QVarLengthArray::data()

返回指向数组中存储的数据的指针。该指针可用于访问和修改数组中的元素。

示例:

QVarLengthArray<int> array(10);
int *data = array.data();
for (int i = 0; i < 10; ++i)
    data[i] = 2 * i;

只要数组未被重新分配,该指针就一直有效。

该函数主要用于将数组传递给接受普通 C++ 数组的函数。

另请参阅 constData() 和operator[]()。

const T *QVarLengthArray::data() const

这是一个重载函数。

[since 6.3] template <typename... Args> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray::emplace(QVarLengthArray<T, Prealloc>::const_iterator pos, Args &&... args)

在迭代器pos 所指向的项目之前插入一个项目,并将args 传递给该项目的构造函数。

返回一个指向已插入项的迭代器。

该函数于 Qt 6.3 中引入。

[since 6.3] template <typename... Args> T &QVarLengthArray::emplace_back(Args &&... args)

将一个项目插入到此QVarLengthArray 的末尾,并将args 作为参数传递给其构造函数。

返回被插入项的引用。

该函数在 Qt 6.3 中引入。

bool QVarLengthArray::empty() const

如果数组的大小为 0,则返回 `true `;否则返回 `false`。

与isEmpty() 相同。为兼容 STL 而提供。

QVarLengthArray<T, Prealloc>::iterator QVarLengthArray::end()

返回一个STL 风格的迭代器,该迭代器指向数组中最后一个元素之后的虚拟元素。

另请参阅 begin() 和constEnd()。

QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray::end() const

这是一个重载函数。

QVarLengthArray<T, Prealloc>::iterator QVarLengthArray::erase(QVarLengthArray<T, Prealloc>::const_iterator pos)

从向量中移除迭代器pos 所指向的元素,并返回指向向量中下一个元素的迭代器(该迭代器可能是end())。

另请参阅 insert() 和remove()。

QVarLengthArray<T, Prealloc>::iterator QVarLengthArray::erase(QVarLengthArray<T, Prealloc>::const_iterator begin, QVarLengthArray<T, Prealloc>::const_iterator end)

从begin 中移除所有元素,直至end (不包括 本身)。返回一个迭代器,该迭代器指向调用前end 所引用的同一项。

这是一个重载函数。

T &QVarLengthArray::first()

返回数组中第一个元素的引用。该数组不能为空。如果数组可能为空,请在调用此函数之前先检查isEmpty()。

另请参阅 last() 和isEmpty()。

const T &QVarLengthArray::first() const

这是一个重载函数。

T &QVarLengthArray::front()

与first() 相同。为兼容 STL 而提供。

const T &QVarLengthArray::front() const

这是一个重载函数。

template <typename AT = T> qsizetype QVarLengthArray::indexOf(const AT &value, qsizetype from = 0) const

返回数组中value 首次出现的位置索引,从索引位置from 开始向前搜索。如果没有匹配的项,则返回-1。

此函数要求该值类型已实现 `operator==()` 方法。

另请参阅 lastIndexOf() 和contains()。

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

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

将value 插入数组的索引位置i 处。如果i 为0,则该值被插入到向量的开头;如果i 为size(),则该值被插入到向量的末尾。

对于大型数组,此操作可能较慢(线性时间),因为它需要将索引为i 及以上的所有元素在内存中向后移动一位。若需一个提供快速insert() 函数的容器类,请改用 std::list。

另请参阅 remove()。

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

在迭代器before 所指向的项目之前插入count 个value 的副本。返回一个指向已插入项目中第一个项目的迭代器。

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

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

在迭代器before 所指向的元素之前插入value 。返回一个指向已插入元素的迭代器。

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

在向量中索引位置i 处插入count 个value 的副本。

这是一个重载函数。

bool QVarLengthArray::isEmpty() const

如果数组的大小为 0,则返回true ;否则返回false 。

另请参阅 size() 和resize()。

T &QVarLengthArray::last()

返回数组中最后一个元素的引用。该数组不能为空。如果数组可能为空,请在调用此函数之前先检查isEmpty()。

另请参阅 first() 和isEmpty()。

const T &QVarLengthArray::last() const

这是一个重载函数。

template <typename AT = T> qsizetype QVarLengthArray::lastIndexOf(const AT &value, qsizetype from = -1) const

返回数组中值value 最后一次出现的索引位置,从索引位置from 开始向后搜索。如果from 为-1(默认值),则从最后一个元素开始搜索。如果没有匹配的元素,则返回-1。

此函数要求该值类型实现了operator==() 方法。

另请参阅 indexOf() 和contains()。

qsizetype QVarLengthArray::length() const

与size() 相同。

另请参阅 isEmpty() 和resize()。

[static constexpr noexcept, since 6.8] qsizetype QVarLengthArray::maxSize()

[constexpr noexcept, since 6.8] qsizetype QVarLengthArray::max_size() const

该函数返回数组理论上可容纳的元素最大数量。实际上,该数量可能要小得多,具体取决于系统可用的内存大小。

这些函数是在 Qt 6.8 中引入的。

void QVarLengthArray::pop_back()

与removeLast() 相同。为兼容 STL 而提供。

void QVarLengthArray::push_back(const T &t)

将元素t 追加到数组中,必要时会扩展数组。此功能是为了兼容 STL 而提供的。

void QVarLengthArray::push_back(T &&t)

注意:与 push_back() 的左值重载不同, 将一个已经是 `*this ` 中元素的对象的引用作为参数传递会导致未定义行为:

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

该函数重载了 `QVarLengthArray::push_back`。

QVarLengthArray<T, Prealloc>::reverse_iterator QVarLengthArray::rbegin()

返回一个STL 风格的反向迭代器,该迭代器指向可变长度数组中第一个元素,且顺序为反向。

另请参阅 begin()、crbegin() 和rend()。

QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray::rbegin() const

这是一个重载函数。

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

从数组中移除索引位置i 开始的count 个元素。

i size 必须是数组中的有效索引位置(即,0 <= < ())。 必须小于等于 () - 。当 == 0时,允许 == ()。i count size i count i size

另请参阅 insert() 和replace()。

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

从数组中移除所有与t 相等的元素。如果移除了元素,则返回移除的元素个数。

该函数在 Qt 6.1 中引入。

另请参阅 removeOne()。

[since 6.12] void QVarLengthArray::removeAt(qsizetype i)

从数组中移除位于位置i 的元素。

i i 必须是数组中的有效索引位置(即 0 <= < ())。size

提供此函数是为了与QList 保持兼容。

该函数在 Qt 6.12 中引入。

另请参阅 insert() 和replace()。

[since 6.1] template <typename Predicate> qsizetype QVarLengthArray::removeIf(Predicate pred)

从数组中移除所有满足谓词pred 的元素。如果移除了元素,则返回移除的元素个数。

该函数在 Qt 6.1 中引入。

另请参阅 removeAll()。

void QVarLengthArray::removeLast()

将数组的大小减少一个。已分配的大小保持不变。

另请参阅 append()。

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

从数组中移除第一个与t 相等的元素。返回是否确实移除了该元素。

该函数于 Qt 6.1 中引入。

另请参阅 removeAll()。

QVarLengthArray<T, Prealloc>::reverse_iterator QVarLengthArray::rend()

返回一个STL 风格的反向迭代器,该迭代器指向可变长度数组中最后一个元素之后的一个位置,且按反向顺序排列。

另请参阅 end()、crend() 以及rbegin()。

QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray::rend() const

这是一个重载函数。

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

将索引位置为i 的元素替换为value 。

i i 必须是数组中的有效索引位置(即,0 <= < ())。size

另请参阅 operator[]() 和remove()。

void QVarLengthArray::reserve(qsizetype size)

尝试为至少size 个元素分配内存。如果您事先知道数组可能达到的大小,可以调用此函数;如果您频繁调用resize(),性能可能会得到提升。如果size 的估计值过低,最坏的情况是QVarLengthArray 的运行速度会稍微变慢一些。

该函数的唯一目的是提供一种精细调整QVarLengthArray 内存使用的方法。通常情况下,您几乎不需要调用此函数。如果您想更改数组的大小,请调用resize()。

另请参阅 capacity() 和squeeze()。

void QVarLengthArray::resize(qsizetype size)

将数组的大小设置为 `size`。如果 `size ` 大于当前大小,则将元素追加到数组末尾;如果 `size ` 小于当前大小,则从数组末尾移除元素。

如果值类型是基本类型(例如 char、int、float)或指针类型(例如QWidget *),则新元素不会被初始化。对于其他类型,元素将使用默认构造函数生成的值进行初始化。

另请参阅 size() 和squeeze()。

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

将数组的大小设置为size 。如果size 大于当前大小,则在数组末尾添加v 个元素的副本;如果size 小于当前大小,则从数组末尾移除相应数量的元素。

注意:此 函数仅在T 可被复制构造时可用。

该函数在 Qt 6.4 中引入。

另请参阅 size() 和squeeze()。

void QVarLengthArray::shrink_to_fit()

与squeeze() 相同。为兼容 STL 而提供。

qsizetype QVarLengthArray::size() const

返回数组中元素的个数。

另请参阅 isEmpty() 和resize()。

void QVarLengthArray::squeeze()

释放存储这些项所不需要的内存。如果容器能够将其存储空间全部放在栈上,它将释放堆上的内存,并将元素复制回栈上。

此函数的唯一目的是提供一种方法来微调QVarLengthArray 的内存使用情况。通常情况下,您几乎不需要调用此函数。

另请参阅 reserve()、capacity() 和resize()。

T QVarLengthArray::value(qsizetype i) const

返回索引位置i 处的值。

如果索引i 超出范围,该函数将返回一个通过默认构造生成的值。如果您确定i 在范围内,可以使用at() 代替,其执行速度会稍快一些。

另请参阅 at() 和operator[]()。

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

如果索引i 超出范围,该函数将返回defaultValue 。

这是一个重载函数。

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

将value 追加到数组中,并返回该向量的引用。

另请参阅 append() 和operator<<()。

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

这是一个重载函数。

另请参阅 append() 和operator<<()。

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

将value 追加到数组中,并返回该向量的引用。

另请参阅 append() 和operator+=()。

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

这是一个重载函数。

另请参阅 append() 和operator+=()。

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

该函数将other 移动赋值给该数组,并返回该数组的引用。移动操作完成后,other 为空。

该函数在 Qt 6.0 中引入。

注意: 当std::is_nothrow_move_constructible_v<T> 为true 时,此 函数为 noexcept。

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

将other 赋值给该数组,并返回该数组的引用。

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

将list 的值赋给此数组,并返回对此数组的引用。

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

返回索引位置为i 的项的引用。

i 该索引位置必须是数组中的有效索引(即 0 <=i <size())。

另请参阅 data() 和at()。

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

这是一个重载函数。

相关的非成员

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

从数组array 中移除所有与t 相等的元素。若移除了元素,则返回移除的元素个数。

注意: t 不能是array 中某个元素的引用。如果您无法确定是否属于这种情况,请先复制t ,然后对该副本调用此函数。

该函数自 Qt 6.1 起引入。

另请参阅 erase_if()。

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

从列表array 中移除所有满足谓词pred 的元素。返回移除的元素个数(如有)。预分配大小Prealloc 不影响此操作。

该函数在 Qt 6.1 中引入。

另请参阅 erase()。

[noexcept(...)] template <typename T, qsizetype Prealloc> size_t qHash(const QVarLengthArray<T, Prealloc> &key, size_t seed = 0)

返回key 的哈希值,并使用seed 作为计算的种子。

qHash() 必须支持T 类型。

返回 `key` 的哈希值,并使用 `seed ` 作为计算的种子。预分配大小 `Prealloc ` 不影响哈希值。

注意: 当QtPrivate::QNothrowHashable_v<T> 为true 时,此 函数为 noexcept。

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

如果由 `left ` 和 `right` 指定的两个数组不相同,则返回 `true `。预分配大小 `Prealloc1 ` 和 `Prealloc2 ` 可能不同。

若两个数组包含相同的值且顺序相同,则视为相等。

该函数要求值类型实现了operator==() 。

另请参阅 operator==()。

template < typename T, qsizetype Prealloc1, qsizetype Prealloc2 > bool operator<(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)

如果可变长度数组lhs 在字典序上小于 rhs ,则返回true ;否则返回false 。预分配大小Prealloc1 和Prealloc2 可能不同。

该函数要求值类型实现了operator<() 。

template < typename T, qsizetype Prealloc1, qsizetype Prealloc2 > bool operator<=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)

如果可变长度数组lhs 在字典序上小于或等于 rhs ,则返回true ;否则返回false 。预分配大小Prealloc1 和Prealloc2 可能不同。

此函数要求值类型实现了operator<() 接口。

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

如果由 `left ` 和 `right` 指定的两个数组相等,则返回 `true `。预分配大小 `Prealloc1 ` 和 `Prealloc2 ` 可能不同。

若两个数组包含相同的值且顺序相同,则视为相等。

该函数要求值类型实现了operator==() 。

另请参阅 operator!=()。

template < typename T, qsizetype Prealloc1, qsizetype Prealloc2 > bool operator>(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)

如果可变长度数组lhs 在字典序上大于 rhs ,则返回true ;否则返回false 。预分配大小Prealloc1 和Prealloc2 可能不同。

该函数要求值类型实现了operator<() 接口。

template < typename T, qsizetype Prealloc1, qsizetype Prealloc2 > bool operator>=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)

如果可变长度数组lhs 在字典序上大于或等于 rhs ,则返回true ;否则返回false 。预分配大小Prealloc1 和Prealloc2 可能不同。

此函数要求值类型实现了operator<() 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.