QVarLengthArray Class

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

QVarLengthArray 类提供了一个低级变长数组。更多

Header: #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.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(int n)
{
    int table[n + 1];  // WRONG
    ...
    return table[n];
}

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

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

但是,如果在应用程序的内循环中频繁调用 myfunc(),堆分配可能会成为导致速度减慢的主要原因。

QVarLengthArray 尝试解决 C++ 语言中的这一缺陷。它在堆栈中分配一定数量的元素,如果将数组的大小调整到更大,它会自动使用堆来代替。堆栈分配的优点是比堆分配快得多。

举例说明

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

在上面的示例中,QVarLengthArray 将在堆栈中预先分配 1024 个元素并使用它们,除非n + 1 大于 1024。如果省略第二个模板参数,将使用 QVarLengthArray 的默认值 256。

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

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

  • QVarLengthArray 的 API 更底层,缺少QList 的某些功能。
  • 如果值是基本类型,QVarLengthArray 不会初始化内存。(QList 总是这样)。
  • QList QVarLengthArray 使用隐式共享作为内存优化。QVarLengthArray 并不提供这种功能;不过,由于减少了开销,它的性能通常会稍好一些,尤其是在紧凑的循环中。

总之,QVarLengthArray 是一个低级优化类,只有在非常特殊的情况下才有意义。它在 Qt 中使用的地方不多,是为了方便高级用户而添加到 Qt 的公共 API 中的。

另请参阅 QList

成员类型文档

[alias] QVarLengthArray::const_iterator

const T * 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::const_pointer

const T * 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::const_reference

const T & 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::const_reverse_iterator

用于std::reverse_iterator<const T*> 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::difference_type

ptrdiff_t 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::iterator

T * 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::pointer

T * 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::reference

T & 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::reverse_iterator

用于std::reverse_iterator<T*> 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::size_type

int 的类型定义。为与 STL 兼容而提供。

[alias] QVarLengthArray::value_type

为 T 提供的类型定义,用于兼容 STL。

成员函数文档

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

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

它返回数组理论上可容纳的最大元素数。在实际应用中,受系统可用内存量的限制,这个数字可能会小得多。

此函数在 Qt 6.8 中引入。

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

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

value 插入数组中的索引位置i 。如果i 为 0,则将该值预输入到向量中。如果isize() ,则将该值添加到向量中。

对于大型数组,这一操作可能会很慢(线性时间),因为它需要将i 及以上索引位置的所有项在内存中移动一个位置。如果你想要一个提供快速insert() 函数的容器类,请使用 std::list 代替。

另请参见 remove()。

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 。返回指向插入项的迭代器。

[noexcept] QVarLengthArray::QVarLengthArray()

构造一个初始大小为 0 的数组。

[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)

Move- 从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。

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

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

[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)

nt 的副本替换此容器的内容。

此容器的大小将等于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

返回指向数组中第一个项目的 ConstSTL 样式迭代器

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

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

返回一个常STL 样式的迭代器,指向数组中最后一个项目之后的虚数项目。

另请参阅 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

返回一个 ConstSTL 样式的反向迭代器,按相反顺序指向变长数组中的第一个项目。

另请参见 begin()、rbegin() 和rend()。

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

返回一个 ConstSTL 样式的反向迭代器,以相反的顺序指向变长数组中最后一个项目的后面一个项目。

另请参见 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)

这是一个重载函数。

删除beginend (但不包括 )的所有项目。返回一个迭代器,指向调用前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()。

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

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

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

这是一个重载函数。

在向量的索引位置i 插入countvalue

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() 。

void QVarLengthArray::pop_back()

removeLast() 相同。提供 STL 兼容性。

void QVarLengthArray::push_back(const T &t)

将项目t 添加到数组中,必要时扩展数组。提供 STL 兼容性。

void QVarLengthArray::push_back(T &&t)

此函数重载 push_back。

注意: 与 push_back() 的 lvalue 重载不同,如果传递的引用对象已经是*this 的元素,则会导致未定义的行为:

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

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)

这是一个重载函数。

从数组中间删除count 元素,从索引位置i 开始。

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

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

删除数组中所有比较值等于t 的元素。如果有,则返回删除的元素数。

此函数在 Qt 6.1 中引入。

另请参阅 removeOne()。

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

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

此函数在 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)

value 替换索引位置i 上的项目。

i 必须是数组中有效的索引位置(即 0 <= < () )。i 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)

Move 将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 返回 true 的所有元素。如果有,则返回已删除元素的数量。

此函数在 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 类型。

注: 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)

如果leftright 指定的两个数组相等,则返回true

如果两个数组以相同顺序包含相同的值,则视为相等。

此函数要求值类型具有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

该函数要求值类型具有operator<() 的实现。

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

如果变长数组lhs词法上小于或等于 rhs ,则返回true ;否则返回false

该函数要求值类型具有operator<() 的实现。

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

如果leftright 指定的两个数组相等,则返回true

如果两个数组以相同顺序包含相同的值,则视为相等。

此函数要求值类型具有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

该函数要求值类型具有operator<() 的实现。

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

如果变长数组lhs词法上大于或等于 rhs ,则返回true ;否则返回false

该函数要求值类型具有operator<() 的实现。

© 2025 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.