Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QVarLengthArray Class

TheQVarLengthArray class provides a low-level variable-length array.More...

Header:#include <QVarLengthArray>

Note: All functions in this class arereentrant.

Public Types

typedefconst_iterator
typedefconst_pointer
typedefconst_reference
typedefdifference_type
typedefiterator
typedefpointer
typedefreference
typedefsize_type
typedefvalue_type

Public Functions

QVarLengthArray(int size = 0)
QVarLengthArray(const QVarLengthArray<T, Prealloc> & other)
~QVarLengthArray()
voidappend(const T & t)
voidappend(const T * buf, int size)
const T &at(int i) const
iteratorbegin()
const_iteratorbegin() const
intcapacity() const
voidclear()
const_iteratorconstBegin() const
const T *constData() const
const_iteratorconstEnd() const
intcount() const
T *data()
const T *data() const
iteratorend()
const_iteratorend() const
iteratorerase(iterator pos)
iteratorerase(iterator begin, iterator end)
voidinsert(int i, const T & value)
iteratorinsert(iterator before, int count, const T & value)
voidinsert(int i, int count, const T & value)
iteratorinsert(iterator before, const T & value)
boolisEmpty() const
voidprepend(const T & value)
voidremove(int i)
voidremove(int i, int count)
voidremoveLast()
voidreplace(int i, const T & value)
voidreserve(int size)
voidresize(int size)
intsize() const
Tvalue(int i) const
Tvalue(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)
T &operator[](int i)
const T &operator[](int i) const

Related Non-Members

booloperator!=(const QVarLengthArray<T, Prealloc1> & left, const QVarLengthArray<T, Prealloc2> & right)
booloperator==(const QVarLengthArray<T, Prealloc1> & left, const QVarLengthArray<T, Prealloc2> & right)

Detailed Description

TheQVarLengthArray 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 (withnew):

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

However, if myfunc() is called very frequently from the application's inner loop, heap allocation can be a major source of slowdown.

QVarLengthArray is an attempt to work around this gap in the C++ language. It allocates a certain number of elements on the stack, and if you resize the array to a larger size, it automatically uses the heap instead. Stack allocation has the advantage that it is much faster than heap allocation.

Example:

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

In the example above,QVarLengthArray will preallocate 1024 elements on the stack and use them unlessn + 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 anassignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store aQWidget as a value; instead, store aQWidget *.

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

  • QVarLengthArray's API is much more low-level. It provides no iterators and lacks much ofQVector's functionality.
  • QVarLengthArray doesn't initialize the memory if the value is a basic type. (QVector always does.)
  • QVector usesimplicit sharing 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.

See alsoQVector,QList, andQLinkedList.

Member Type Documentation

typedef QVarLengthArray::const_iterator

Typedef for const T *. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::const_pointer

Typedef for const T *. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::const_reference

Typedef for const T &. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::iterator

Typedef for T *. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::pointer

Typedef for T *. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::reference

Typedef for T &. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::size_type

Typedef for int. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

typedef QVarLengthArray::value_type

Typedef for T. Provided for STL compatibility.

This typedef was introduced in Qt 4.7.

Member Function Documentation

QVarLengthArray::QVarLengthArray(int size = 0)

Constructs an array with an initial size ofsize elements.

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 default-constructed value.

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

Constructs a copy ofother.

QVarLengthArray::~QVarLengthArray()

Destroys the array.

void QVarLengthArray::append(constT & t)

Appends itemt to the array, extending the array if necessary.

See alsoremoveLast().

void QVarLengthArray::append(constT * buf,int size)

Appendssize amount of items referenced bybuf to this array.

constT & QVarLengthArray::at(int i) const

Returns a reference to the item at index positioni.

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

See alsovalue() andoperator[]().

iterator QVarLengthArray::begin()

Returns an STL-style iterator pointing to the first item in the array.

This function was introduced in Qt 4.8.

See alsoconstBegin() andend().

const_iterator QVarLengthArray::begin() const

This is an overloaded function.

This function was introduced in 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 tuningQVarLengthArray'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, callsize().

See alsoreserve().

void QVarLengthArray::clear()

Removes all the elements from the array.

Same as resize(0).

const_iterator QVarLengthArray::constBegin() const

Returns a const STL-style iterator pointing to the first item in the array.

This function was introduced in Qt 4.8.

See alsobegin() andconstEnd().

constT * 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.

See alsodata() andoperator[]().

const_iterator QVarLengthArray::constEnd() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the array.

This function was introduced in Qt 4.8.

See alsoconstBegin() andend().

int QVarLengthArray::count() const

Same assize().

See alsoisEmpty() andresize().

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.

Example:

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.

See alsoconstData() andoperator[]().

constT * QVarLengthArray::data() const

This is an overloaded function.

iterator QVarLengthArray::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the array.

This function was introduced in Qt 4.8.

See alsobegin() andconstEnd().

const_iterator QVarLengthArray::end() const

This is an overloaded function.

This function was introduced in Qt 4.8.

iterator QVarLengthArray::erase(iterator pos)

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

This function was introduced in Qt 4.8.

See alsoinsert() andremove().

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

This is an overloaded function.

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

This function was introduced in Qt 4.8.

void QVarLengthArray::insert(int i, constT & value)

Insertsvalue at index positioni in the array. Ifi is 0, the value is prepended to the vector. Ifi issize(), the value is appended to the vector.

For large arrays, this operation can be slow (linear time), because it requires moving all the items at indexesi and above by one position further in memory. If you want a container class that provides a fast insert() function, useQLinkedList instead.

This function was introduced in Qt 4.8.

See alsoremove().

iterator QVarLengthArray::insert(iterator before,int count, constT & value)

Insertscount copies ofvalue in front of the item pointed to by the iteratorbefore. Returns an iterator pointing at the first of the inserted items.

This function was introduced in Qt 4.8.

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

This is an overloaded function.

Insertscount copies ofvalue at index positioni in the vector.

This function was introduced in Qt 4.8.

iterator QVarLengthArray::insert(iterator before, constT & value)

This is an overloaded function.

Insertsvalue in front of the item pointed to by the iteratorbefore. Returns an iterator pointing at the inserted item.

This function was introduced in Qt 4.8.

bool QVarLengthArray::isEmpty() const

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

See alsosize() andresize().

void QVarLengthArray::prepend(constT & value)

Insertsvalue at the beginning of the array.

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

For large arrays, this operation can be slow (linear time), 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, useQList orQLinkedList instead.

This function was introduced in Qt 4.8.

See alsoappend() andinsert().

void QVarLengthArray::remove(int i)

This is an overloaded function.

Removes the element at index positioni.

This function was introduced in Qt 4.8.

See alsoinsert() andreplace().

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

This is an overloaded function.

Removescount elements from the middle of the array, starting at index positioni.

This function was introduced in Qt 4.8.

See alsoinsert() andreplace().

void QVarLengthArray::removeLast()

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

This function was introduced in Qt 4.5.

See alsoappend().

void QVarLengthArray::replace(int i, constT & value)

Replaces the item at index positioni withvalue.

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

This function was introduced in Qt 4.8.

See alsooperator[]() andremove().

void QVarLengthArray::reserve(int size)

Attempts to allocate memory for at leastsize elements. If you know in advance how large the array can get, you can call this function and if you callresize() often, you are likely to get better performance. Ifsize is an underestimate, the worst that will happen is that theQVarLengthArray will be a bit slower.

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

See alsocapacity().

void QVarLengthArray::resize(int size)

Sets the size of the array tosize. Ifsize is greater than the current size, elements are added to the end. Ifsize 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 default-constructed value.

See alsosize().

int QVarLengthArray::size() const

Returns the number of elements in the array.

See alsoisEmpty() andresize().

T QVarLengthArray::value(int i) const

Returns the value at index positioni.

If the indexi is out of bounds, the function returns a default-constructed value. If you are certain thati is within bounds, you can useat() instead, which is slightly faster.

See alsoat() andoperator[]().

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

This is an overloaded function.

If the indexi is out of bounds, the function returnsdefaultValue.

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

Appendsvalue to the array and returns a reference to this vector.

This function was introduced in Qt 4.8.

See alsoappend() andoperator<<().

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

Appendsvalue to the array and returns a reference to this vector.

This function was introduced in Qt 4.8.

See alsoappend() andoperator+=().

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

Assignsother to this array and returns a reference to this array.

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

Returns a reference to the item at index positioni.

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

See alsodata() andat().

constT & QVarLengthArray::operator[](int i) const

This is an overloaded function.

Related Non-Members

booloperator!=(constQVarLengthArray<T,Prealloc1> & left, constQVarLengthArray<T,Prealloc2> & right)

Returns true if the two arrays, specified byleft andright, arenot 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 ofoperator==().

This function was introduced in Qt 4.8.

See alsooperator==().

booloperator==(constQVarLengthArray<T,Prealloc1> & left, constQVarLengthArray<T,Prealloc2> & right)

Returns true if the two arrays, specified byleft andright, 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 ofoperator==().

This function was introduced in Qt 4.8.

See alsooperator!=().

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp