QVarLengthArray 类

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

头: #include <QVarLengthArray>
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共类型

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

公共函数

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

范例:

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

在以上范例中, 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 可赋值数据类型 . This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget 作为值;取而代之,存储 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.

另请参阅 QVector , QList ,和 QLinkedList .

成员类型文档编制

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 对于 std::reverse_iterator<const T*> . Provided for STL compatibility.

该 typedef 在 Qt 5.6 引入。

typedef QVarLengthArray:: difference_type

typedef 对于 ptrdiff_t。为兼容 STL 提供。

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 对于 std::reverse_iterator<T*> . Provided for STL compatibility.

该 typedef 在 Qt 5.6 引入。

typedef QVarLengthArray:: size_type

typedef 对于 int。为兼容 STL 提供。

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray:: value_type

typedef 对于 T。为兼容 STL 提供。

This typedef was introduced in Qt 4.7.

成员函数文档编制

QVarLengthArray:: QVarLengthArray ( int size = 0)

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 (const QVarLengthArray < T , Prealloc > & other )

构造副本为 other .

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

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

This constructor is only enabled if the compiler supports C++11 initializer lists.

该函数在 Qt 5.5 引入。

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 ( T && t )

此函数重载 append .

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

该函数在 Qt 5.9 引入。

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

追加 size amount of items referenced by buf 到此数组。

const T &QVarLengthArray:: at ( int 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.

该函数在 Qt 5.0 引入。

const T &QVarLengthArray:: back () const

这是重载函数。

该函数在 Qt 5.0 引入。

iterator QVarLengthArray:: begin ()

返回 STL 样式迭代器 pointing to the first item in the array.

该函数在 Qt 4.8 引入。

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

const_iterator QVarLengthArray:: begin () const

这是重载函数。

该函数在 Qt 4.8 引入。

int 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 ().

const_iterator QVarLengthArray:: cbegin () const

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

该函数在 Qt 5.0 引入。

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

const_iterator QVarLengthArray:: cend () const

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

该函数在 Qt 5.0 引入。

另请参阅 cbegin () 和 end ().

void QVarLengthArray:: clear ()

Removes all the elements from the array.

Same as resize(0).

const_iterator QVarLengthArray:: constBegin () const

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

该函数在 Qt 4.8 引入。

另请参阅 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[] ().

const_iterator QVarLengthArray:: constEnd () const

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

该函数在 Qt 4.8 引入。

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

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

返回 true if the array contains an occurrence of value ;否则返回 false .

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

该函数在 Qt 5.3 引入。

另请参阅 indexOf () 和 lastIndexOf ().

int QVarLengthArray:: count () const

如同 size ().

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

const_reverse_iterator QVarLengthArray:: crbegin () const

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

该函数在 Qt 5.6 引入。

另请参阅 begin (), rbegin (),和 rend ().

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.

该函数在 Qt 5.6 引入。

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

这是重载函数。

bool QVarLengthArray:: empty () const

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

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

该函数在 Qt 5.0 引入。

iterator QVarLengthArray:: end ()

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

该函数在 Qt 4.8 引入。

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

const_iterator QVarLengthArray:: end () const

这是重载函数。

该函数在 Qt 4.8 引入。

iterator QVarLengthArray:: erase ( 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 ()).

该函数在 Qt 4.8 引入。

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

iterator QVarLengthArray:: erase ( const_iterator begin , 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.

该函数在 Qt 4.8 引入。

T &QVarLengthArray:: first ()

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:: first () const

这是重载函数。

T &QVarLengthArray:: front ()

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

该函数在 Qt 5.0 引入。

const T &QVarLengthArray:: front () const

这是重载函数。

该函数在 Qt 5.0 引入。

int QVarLengthArray:: indexOf (const T & value , int 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==() .

该函数在 Qt 5.3 引入。

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

void QVarLengthArray:: insert ( int 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 QLinkedList 代替。

该函数在 Qt 4.8 引入。

另请参阅 remove ().

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

这是重载函数。

插入 count 个副本对于 value at index position i in the vector.

该函数在 Qt 4.8 引入。

iterator QVarLengthArray:: insert ( const_iterator before , int 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.

该函数在 Qt 4.8 引入。

iterator QVarLengthArray:: insert ( 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.

该函数在 Qt 4.8 引入。

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

这是重载函数。

int QVarLengthArray:: lastIndexOf (const T & value , int 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==() .

该函数在 Qt 5.3 引入。

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

int QVarLengthArray:: length () const

如同 size ().

该函数在 Qt 5.0 引入。

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

void QVarLengthArray:: pop_back ()

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

该函数在 Qt 5.0 引入。

void QVarLengthArray:: prepend (const T & value )

插入 value at the beginning of the array.

This is the same as vector.insert(0, value ).

For large arrays, this operation can be slow ( 线性时间 ), because it requires moving all the items in the vector by one position further in memory. If you want a container class that provides a fast prepend() function, use QList or QLinkedList 代替。

该函数在 Qt 4.8 引入。

另请参阅 append () 和 insert ().

void QVarLengthArray:: push_back (const T & t )

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

该函数在 Qt 5.0 引入。

void QVarLengthArray:: push_back ( T && t )

此函数重载 push_back .

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

该函数在 Qt 5.9 引入。

reverse_iterator QVarLengthArray:: rbegin ()

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

该函数在 Qt 5.6 引入。

另请参阅 begin (), crbegin (),和 rend ().

const_reverse_iterator QVarLengthArray:: rbegin () const

这是重载函数。

该函数在 Qt 5.6 引入。

void QVarLengthArray:: remove ( int i )

这是重载函数。

Removes the element at index position i .

该函数在 Qt 4.8 引入。

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

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

这是重载函数。

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

该函数在 Qt 4.8 引入。

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

void QVarLengthArray:: removeLast ()

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

该函数在 Qt 4.5 引入。

另请参阅 append ().

reverse_iterator QVarLengthArray:: rend ()

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

该函数在 Qt 5.6 引入。

另请参阅 end (), crend (),和 rbegin ().

const_reverse_iterator QVarLengthArray:: rend () const

这是重载函数。

该函数在 Qt 5.6 引入。

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

替换项在索引位置 i with value .

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

该函数在 Qt 4.8 引入。

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

void QVarLengthArray:: reserve ( int 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 ( int 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 ().

int 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.

该函数在 Qt 5.1 引入。

另请参阅 reserve (), capacity (),和 resize ().

T QVarLengthArray:: value ( int 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 ( int 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.

该函数在 Qt 4.8 引入。

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

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

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

该函数在 Qt 4.8 引入。

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

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.

This constructor is only enabled if the compiler supports C++11 initializer lists.

该函数在 Qt 5.5 引入。

T &QVarLengthArray:: operator[] ( int 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[] ( int i ) const

这是重载函数。

相关非成员

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==() .

该函数在 Qt 4.8 引入。

另请参阅 operator== ().

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

该函数在 Qt 5.6 引入。

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

该函数在 Qt 5.6 引入。

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==() .

该函数在 Qt 4.8 引入。

另请参阅 operator!= ().

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

该函数在 Qt 5.6 引入。

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

该函数在 Qt 5.6 引入。