QVarLengthArray 類

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

QVarLengthArray 類 供低級可變長度數組。 更多...

頭: #include <QVarLengthArray>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
繼承: QVLABase and QVLAStorage

注意: 此類的所有函數 可重入 .

公共類型

const_iterator
const_pointer
const_reference
const_reverse_iterator
difference_type
iterator
pointer
reference
reverse_iterator
size_type
value_type

公共函數

QVarLengthArray ()
QVarLengthArray (qsizetype size )
QVarLengthArray (std::initializer_list<T> args )
QVarLengthArray (InputIterator first , InputIterator last )
(從 6.4 起) QVarLengthArray (qsizetype size , const T & v )
QVarLengthArray (const QVarLengthArray<T, Prealloc> & other )
(從 6.0 起) QVarLengthArray (QVarLengthArray<T, Prealloc> && other )
~QVarLengthArray ()
void append (const T & t )
void append (const T * buf , qsizetype size )
void append (T && t )
(從 6.6 起) QVarLengthArray<T, Prealloc> & assign (std::initializer_list<T> list )
(從 6.6 起) QVarLengthArray<T, Prealloc> & assign (InputIterator first , InputIterator last )
(從 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
(從 6.3 起) QVarLengthArray<T, Prealloc>::iterator emplace (QVarLengthArray<T, Prealloc>::const_iterator pos , Args &&... args )
(從 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
(從 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)
(從 6.1 起) qsizetype removeAll (const AT & t )
(從 6.1 起) qsizetype removeIf (Predicate pred )
void removeLast ()
(從 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 )
(從 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 )
(從 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

靜態公共成員

(從 6.8 起) qsizetype maxSize ()
(從 6.1 起) qsizetype erase (QVarLengthArray<T, Prealloc> & array , const AT & t )
(從 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 )

詳細描述

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.

範例:

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.

QVarLengthArry 的值類型必須是 可賦值數據類型 . This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget 作為值;取而代之,存儲 QWidget *.

QVarLengthArray,像 QList , provides a resizable array data structure. The main differences between the two classes are:

  • QVarLengthArray's API is much more low-level and it lacks some of QList 's functionality.
  • QVarLengthArray doesn't initialize the memory if the value is a basic type. ( QList always does.)
  • QList 使用 隱式共享 as a memory optimization. QVarLengthArray doesn't provide that feature; however, it usually produces slightly better performance due to reduced overhead, especially in tight loops.

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.

另請參閱 QList .

成員類型文檔編製

[alias] QVarLengthArray:: const_iterator

Typedef for const T *. Provided for STL compatibility.

[alias] QVarLengthArray:: const_pointer

Typedef for const T *. Provided for STL compatibility.

[alias] QVarLengthArray:: const_reference

Typedef for const T &. Provided for STL compatibility.

[alias] QVarLengthArray:: const_reverse_iterator

typedef 對於 std::reverse_iterator<const T*> . Provided for STL compatibility.

[alias] QVarLengthArray:: difference_type

typedef 對於 ptrdiff_t。為兼容 STL 提供。

[alias] QVarLengthArray:: iterator

Typedef for T *. Provided for STL compatibility.

[alias] QVarLengthArray:: pointer

Typedef for T *. Provided for STL compatibility.

[alias] QVarLengthArray:: reference

Typedef for T &. Provided for STL compatibility.

[alias] QVarLengthArray:: reverse_iterator

typedef 對於 std::reverse_iterator<T*> . Provided for STL compatibility.

[alias] QVarLengthArray:: size_type

typedef 對於 int。為兼容 STL 提供。

[alias] QVarLengthArray:: value_type

typedef 對於 T。為兼容 STL 提供。

成員函數文檔編製

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

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

It returns the maximum number of elements that the array can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system.

這些函數在 Qt 6.8 引入。

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

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

插入 value at index position i in the array. If i is 0, the value is prepended to the vector. If i is size (), the value is appended to the vector.

For large arrays, this operation can be slow ( 綫性時間 ), because it requires moving all the items at indexes i and above by one position further in memory. If you want a container class that provides a fast insert () function, use std::list instead.

另請參閱 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 )

插入 value in front of the item pointed to by the iterator before . Returns an iterator pointing at the inserted item.

[noexcept] QVarLengthArray:: QVarLengthArray ()

Constructs an array with an initial size of zero.

[explicit] QVarLengthArray:: QVarLengthArray ( qsizetype size )

Constructs an array with an initial size of size 元素。

If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), the elements are not initialized. For other types, the elements are initialized with a 默認構造值 .

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

Constructs an array from the std::initializer_list given by args .

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

Constructs an array with the contents in the iterator range [ first , last ).

The value type of InputIterator must be convertible to T .

約束

Participates in overload resolution only if InputIterator meets the requirements of an LegacyInputIterator .

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

Constructs an array with an initial size of size elements filled with copies of v .

注意: This constructor is only available when T is copy-constructible.

該函數在 Qt 6.4 引入。

另請參閱 size () 和 squeeze ().

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

構造副本為 other .

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

Move-constructs this variable-length array from other . After the move, other is empty.

該函數在 Qt 6.0 引入。

注意: This function is noexcept when std::is_nothrow_move_constructible_v<T> is true .

QVarLengthArray:: ~QVarLengthArray ()

Destroys the array.

void QVarLengthArray:: append (const T & t )

Appends item t to the array, extending the array if necessary.

另請參閱 removeLast ().

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

追加 size amount of items referenced by buf 到此數組。

void QVarLengthArray:: append ( T && t )

注意: Unlike the lvalue overload of append(), passing a reference to an object that is already an element of *this leads to undefined behavior:

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 )

Replaces the contents of this container with a copy of the elements of list .

The size of this container will be equal to the number of elements in list .

This function only allocates memory if the number of elements in list exceeds the capacity of the container.

該函數在 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 )

Replaces the contents of this container with a copy of the elements in the iterator range [ first , last ).

The size of this container will be equal to the number of elements in the range [ first , last ). This function will only allocate memory if the number of elements in the range exceeds the capacity of the container.

The behavior is undefined if either argument is an iterator into *this.

約束

Participates in overload resolution only if InputIterator meets the requirements of an LegacyInputIterator .

該函數在 Qt 6.6 引入。

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

Replaces the contents of this container with n 個副本對於 t .

The size of this container will be equal to n . This function will only allocate memory if n exceeds the capacity of the container.

該函數在 Qt 6.6 引入。

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

Returns a reference to the item at index position i .

i must be a valid index position in the array (i.e., 0 <= i < size ()).

另請參閱 value () 和 operator[] ().

T &QVarLengthArray:: back ()

如同 last (). Provided for STL-compatibility.

const T &QVarLengthArray:: back () const

這是重載函數。

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

返迴 STL 樣式迭代器 pointing to the first item in the array.

另請參閱 constBegin () 和 end ().

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

這是重載函數。

qsizetype QVarLengthArray:: capacity () const

Returns the maximum number of elements that can be stored in the array without forcing a reallocation.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray 's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the array, call size ().

另請參閱 reserve () 和 squeeze ().

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

返迴常量 STL 樣式迭代器 pointing to the first item in the array.

另請參閱 begin () 和 cend ().

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

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the array.

另請參閱 cbegin () 和 end ().

void QVarLengthArray:: clear ()

Removes all the elements from the array.

Same as resize(0).

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

返迴常量 STL 樣式迭代器 pointing to the first item in the array.

另請參閱 begin () 和 constEnd ().

const T *QVarLengthArray:: constData () const

Returns a const pointer to the data stored in the array. The pointer can be used to access the items in the array. The pointer remains valid as long as the array isn't reallocated.

This function is mostly useful to pass an array to a function that accepts a plain C++ array.

另請參閱 data () 和 operator[] ().

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

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the array.

另請參閱 constBegin () 和 end ().

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

返迴 true if the array contains an occurrence of value ;否則返迴 false .

This function requires the value type to have an implementation of operator==() .

另請參閱 indexOf () 和 lastIndexOf ().

qsizetype QVarLengthArray:: count () const

如同 size ().

另請參閱 isEmpty () 和 resize ().

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

返迴常量 STL-style reverse iterator pointing to the first item in the variable length array, in reverse order.

另請參閱 begin (), rbegin (),和 rend ().

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

返迴常量 STL-style reverse iterator pointing to one past the last item in the variable length array, in reverse order.

另請參閱 end (), rend (),和 rbegin ().

T *QVarLengthArray:: data ()

Returns a pointer to the data stored in the array. The pointer can be used to access and modify the items in the array.

範例:

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

The pointer remains valid as long as the array isn't reallocated.

This function is mostly useful to pass an array to a function that accepts a plain C++ array.

另請參閱 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 )

Inserts an item in front of the item pointed to by the iterator pos , passing args to its constructor.

Returns an iterator pointing at the emplaced item.

該函數在 Qt 6.3 引入。

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

Inserts an item at the back of this QVarLengthArray , passing args to its constructor.

Returns a reference to the emplaced item.

該函數在 Qt 6.3 引入。

bool QVarLengthArray:: empty () const

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

如同 isEmpty (). Provided for STL-compatibility.

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

返迴 STL 樣式迭代器 pointing to the imaginary item after the last item in the array.

另請參閱 begin () 和 constEnd ().

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

這是重載函數。

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

Removes the item pointed to by the iterator pos from the vector, and returns an iterator to the next item in the vector (which may be end ()).

另請參閱 insert () 和 remove ().

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

Removes all the items from begin up to (but not including) end . Returns an iterator to the same item that end referred to before the call.

這是重載函數。

T &QVarLengthArray:: 第一 ()

Returns a reference to the first item in the array. The array must not be empty. If the array can be empty, check isEmpty () before calling this function.

另請參閱 last () 和 isEmpty ().

const T &QVarLengthArray:: 第一 () const

這是重載函數。

T &QVarLengthArray:: front ()

如同 first (). Provided for STL-compatibility.

const T &QVarLengthArray:: front () const

這是重載函數。

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

Returns the index position of the first occurrence of value in the array, searching forward from index position from . Returns -1 if no item matched.

This function requires the value type to have an implementation of operator==() .

另請參閱 lastIndexOf () 和 contains ().

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

插入 count 個副本對於 value in front of the item pointed to by the iterator before . Returns an iterator pointing at the first of the inserted items.

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

插入 count 個副本對於 value at index position i in the vector.

這是重載函數。

bool QVarLengthArray:: isEmpty () const

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

另請參閱 size () 和 resize ().

T &QVarLengthArray:: last ()

Returns a reference to the last item in the array. The array must not be empty. If the array can be empty, check isEmpty () before calling this function.

另請參閱 first () 和 isEmpty ().

const T &QVarLengthArray:: last () const

這是重載函數。

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

Returns the index position of the last occurrence of the value value in the array, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

This function requires the value type to have an implementation of operator==() .

另請參閱 indexOf () 和 contains ().

qsizetype QVarLengthArray:: length () const

如同 size ().

另請參閱 isEmpty () 和 resize ().

void QVarLengthArray:: pop_back ()

如同 removeLast (). Provided for STL-compatibility.

void QVarLengthArray:: push_back (const T & t )

Appends item t to the array, extending the array if necessary. Provided for STL-compatibility.

void QVarLengthArray:: push_back ( T && t )

注意: Unlike the lvalue overload of push_back(), passing a reference to an object that is already an element of *this leads to undefined behavior:

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-style reverse iterator pointing to the first item in the variable length array, in reverse order.

另請參閱 begin (), crbegin (),和 rend ().

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

這是重載函數。

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

移除 count elements from the middle of the array, starting at index position i .

另請參閱 insert () 和 replace ().

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

Removes all elements that compare equal to t from the array. Returns the number of elements removed, if any.

該函數在 Qt 6.1 引入。

另請參閱 removeOne ().

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

Removes all elements for which the predicate pred returns true from the array. Returns the number of elements removed, if any.

該函數在 Qt 6.1 引入。

另請參閱 removeAll ().

void QVarLengthArray:: removeLast ()

Decreases the size of the array by one. The allocated size is not changed.

另請參閱 append ().

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

Removes the first element that compares equal to t from the array. Returns whether an element was, in fact, removed.

該函數在 Qt 6.1 引入。

另請參閱 removeAll ().

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

返迴 STL-style reverse iterator pointing to one past the last item in the variable length array, in reverse order.

另請參閱 end (), crend (),和 rbegin ().

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

這是重載函數。

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

替換項在索引位置 i with value .

i must be a valid index position in the array (i.e., 0 <= i < size ()).

另請參閱 operator[] () 和 remove ().

void QVarLengthArray:: reserve ( qsizetype size )

試圖分配內存為至少 size elements. If you know in advance how large the array can get, you can call this function and if you call resize () often, you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QVarLengthArray will be a bit slower.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray 's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the array, call resize ().

另請參閱 capacity () 和 squeeze ().

void QVarLengthArray:: resize ( qsizetype size )

Sets the size of the array to size 。若 size is greater than the current size, elements are added to the end. If size is less than the current size, elements are removed from the end.

If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), new elements are not initialized. For other types, the elements are initialized with a 默認構造值 .

另請參閱 size () 和 squeeze ().

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

Sets the size of the array to size 。若 size is greater than the current size, copies of v are added to the end. If size is less than the current size, elements are removed from the end.

注意: This function is only available when T is copy-constructible.

該函數在 Qt 6.4 引入。

另請參閱 size () 和 squeeze ().

void QVarLengthArray:: shrink_to_fit ()

如同 squeeze (). Provided for STL-compatibility.

qsizetype QVarLengthArray:: size () const

Returns the number of elements in the array.

另請參閱 isEmpty () 和 resize ().

void QVarLengthArray:: squeeze ()

Releases any memory not required to store the items. If the container can fit its storage on the stack allocation, it will free the heap allocation and copy the elements back to the stack.

The sole purpose of this function is to provide a means of fine tuning QVarLengthArray 's memory usage. In general, you will rarely ever need to call this function.

另請參閱 reserve (), capacity (),和 resize ().

T QVarLengthArray:: value ( qsizetype i ) const

Returns the value at index position i .

If the index i is out of bounds, the function returns a 默認構造值 . If you are certain that i is within bounds, you can use at () instead, which is slightly faster.

另請參閱 at () 和 operator[] ().

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

If the index i is out of bounds, the function returns defaultValue .

這是重載函數。

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

追加 value to the array and returns a reference to this vector.

另請參閱 append () 和 operator<< ().

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

這是重載函數。

另請參閱 append () 和 operator<< ().

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

追加 value to the array and returns a reference to this vector.

另請參閱 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 to this array and returns a reference to this array. After the move, other is empty.

該函數在 Qt 6.0 引入。

注意: This function is noexcept when std::is_nothrow_move_constructible_v<T> is true .

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

賦值 other to this array and returns a reference to this array.

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

Assigns the values of list to this array, and returns a reference to this array.

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

Returns a reference to the item at index position i .

i must be a valid index position in the array (i.e., 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 )

Removes all elements that compare equal to t from the array array . Returns the number of elements removed, if any.

注意: t is not allowed to be a reference to an element inside array . If you cannot be sure that this is not the case, take a copy of t and call this function with the copy.

該函數在 Qt 6.1 引入。

另請參閱 erase_if ().

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

Removes all elements for which the predicate pred returns true from the list array . Returns the number of elements removed, if any.

該函數在 Qt 6.1 引入。

另請參閱 erase ().

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

返迴哈希值為 key ,使用 seed 做計算種子。

類型 T must be supported by qHash().

注意: This function is noexcept when QtPrivate::QNothrowHashable_v<T> is true .

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

返迴 true if the two arrays, specified by left and right , are not equal.

Two arrays are considered equal if they contain the same values in the same order.

This function requires the value type to have an implementation of operator==() .

另請參閱 operator== ().

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

返迴 true if variable length array lhs is lexicographically less than rhs ;否則返迴 false .

This function requires the value type to have an implementation of operator<() .

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

返迴 true if variable length array lhs is lexicographically less than or equal to rhs ;否則返迴 false .

This function requires the value type to have an implementation of operator<() .

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

返迴 true if the two arrays, specified by left and right , are equal.

Two arrays are considered equal if they contain the same values in the same order.

This function requires the value type to have an implementation of operator==() .

另請參閱 operator!= ().

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

返迴 true if variable length array lhs is lexicographically greater than rhs ;否則返迴 false .

This function requires the value type to have an implementation of operator<() .

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

返迴 true if variable length array lhs is lexicographically greater than or equal to rhs ;否則返迴 false .

This function requires the value type to have an implementation of operator<() .