CopperSpice API
1.9.2
|
The QVarLengthArray class provides a low level variable length array. More...
Public Typedefs | |
using | const_iterator = const T * |
using | const_pointer = const value_type * |
using | const_reference = const value_type & |
using | const_reverse_iterator = std::reverse_iterator< const_iterator > |
using | difference_type = qptrdiff |
using | iterator = T * |
using | pointer = value_type * |
using | reference = value_type & |
using | reverse_iterator = std::reverse_iterator< iterator > |
using | size_type = int |
using | value_type = T |
Public Methods | |
QVarLengthArray (const QVarLengthArray< T, Prealloc > &other) | |
QVarLengthArray (int size=0) | |
~QVarLengthArray () | |
void | append (const T &value) |
void | append (const T *buffer, int length) |
const T & | at (int index) 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 begin, const_iterator end) |
iterator | erase (const_iterator pos) |
T & | first () |
const T & | first () const |
T & | front () |
const T & | front () const |
int | indexOf (const T &value, int from=0) const |
iterator | insert (const_iterator before, const T &value) |
iterator | insert (const_iterator before, int count, const T &value) |
void | insert (int index, const T &value) |
void | insert (int index, int count, const T &value) |
bool | isEmpty () const |
T & | last () |
const T & | last () const |
int | lastIndexOf (const T &value, int from=-1) const |
int | length () 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 index) |
const T & | operator[] (int index) const |
void | pop_back () |
void | prepend (const T &value) |
void | push_back (const T &value) |
reverse_iterator | rbegin () |
const_reverse_iterator | rbegin () const |
void | remove (int index) |
void | remove (int index, int count) |
void | removeLast () |
reverse_iterator | rend () |
const_reverse_iterator | rend () const |
void | replace (int index, const T &value) |
void | reserve (int size) |
void | resize (int size) |
int | size () const |
void | squeeze () |
T | value (int index) const |
T | value (int index, const T &defaultValue) const |
Related Functions | |
These are not member functions | |
bool | operator!= (const QVarLengthArray< T, Prealloc1 > &left, const QVarLengthArray< T, Prealloc2 > &right) |
bool | operator== (const QVarLengthArray< T, Prealloc1 > &left, const QVarLengthArray< T, Prealloc2 > &right) |
The QVarLengthArray class provides a low level variable length array. C++ does not have a built in variable length array. This class only makes sense in very specific cases.
For example, the following code will not compile:
An alternative is to allocate the array on the heap using new
. This approach has a drawback if myfunc() is called frequently. Heap allocation can be expensive and slow.
The QVarLengthArray class allocates a specified number of elements on the stack. If the array needs to grow beyond the original stack allocation it will automatically use the heap. Stack allocation has the advantage that it is much faster than heap allocation.
In this example 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.
The QVarLengthArray value type T should be Default Constructible and Copy Constructible to work with all methods of this class. Data types like QObject are not copy constructible and therefore should never be stored in a container. You can however use a pointer to a QObject or any subclass as the type T.
QVarLengthArray is similar to QVector however it provides a resizable array data structure. The following are the key differences between the two classes,
QVarLengthArray< T, Prealloc >::const_iterator |
Typedef for const T *.
QVarLengthArray< T, Prealloc >::const_pointer |
Typedef for const T *.
QVarLengthArray< T, Prealloc >::const_reference |
Typedef for const T &.
QVarLengthArray< T, Prealloc >::const_reverse_iterator |
Typedef for const T *.
QVarLengthArray< T, Prealloc >::difference_type |
Typedef for ptrdiff_t.
QVarLengthArray< T, Prealloc >::iterator |
Typedef for T *.
QVarLengthArray< T, Prealloc >::pointer |
Typedef for T *.
QVarLengthArray< T, Prealloc >::reference |
Typedef for T &.
QVarLengthArray< T, Prealloc >::reverse_iterator |
Typedef for T *.
QVarLengthArray< T, Prealloc >::size_type |
Typedef for int.
QVarLengthArray< T, Prealloc >::value_type |
Typedef for T.
|
inlineexplicit |
Constructs an array with an initial size of size elements.
If the type has a user defined constructor the new elements are default initialized. Otherwise the elements are uninitialized. Refer to default constructed elements for additional information.
|
inline |
Constructs a copy of other.
|
inline |
Destroys the array.
|
inline |
Appends item value to the array, extending the array if necessary.
void QVarLengthArray< T, Prealloc >::append | ( | const T * | buffer, |
int | length | ||
) |
Appends length amount of items referenced by buffer to this array.
|
inline |
Returns a reference to the item at index position index. The given index must be a valid position in the array.
|
inline |
Equivalent to calling removeLast().
|
inline |
Equivalent to calling removeLast().
|
inline |
Returns an STL-style iterator pointing to the first item in the array.
|
inline |
Returns an STL-style iterator pointing to the first item in the array.
|
inline |
Returns the maximum number of elements that can be stored in the array without forcing a reallocation.
The purpose of this method is to provide a means of fine tuning QVarLengthArray's memory usage. This method is rarely used. If you want to know how many items are in the array call size().
|
inline |
Returns a const STL-style iterator pointing to the first item in the array.
|
inline |
Returns a const STL-style iterator pointing to the imaginary item after the last item in the array.
|
inline |
Removes all the elements from the array. Same as calling resize(0).
|
inline |
Returns a const STL-style iterator pointing to the first item in the array.
|
inline |
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 is not reallocated.
This method is mostly useful to pass an array to a function which accepts a raw C++ array.
|
inline |
Returns a const STL-style iterator pointing to the imaginary item after the last item in the array.
|
inline |
Returns true if the array contains an occurrence of value, otherwise returns false. This method requires the value type to have an implementation of operator==().
|
inline |
|
inline |
|
inline |
|
inline |
Returns a pointer to the data stored in the array. The pointer can be used to access and modify the items in the array.
The pointer remains valid as long as the array is not reallocated. This method is mostly useful to pass an array to a method which accepts a raw C++ array.
|
inline |
This is an overloaded method.
|
inline |
Returns true if the array has size 0, otherwise returns false.
Equivalent to calling isEmpty().
|
inline |
Returns an STL-style iterator pointing to the imaginary item after the last item in the array.
|
inline |
Returns an STL-style iterator pointing to the imaginary item after the last item in the array.
QVarLengthArray< T, Prealloc >::iterator QVarLengthArray< T, Prealloc >::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.
|
inline |
|
inline |
|
inline |
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 method.
|
inline |
Equivalent to calling first().
|
inline |
Equivalent to calling first().
int QVarLengthArray< T, Prealloc >::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 method requires the type T to support operator==()
.
|
inline |
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
QVarLengthArray< T, Prealloc >::iterator QVarLengthArray< T, Prealloc >::insert | ( | const_iterator | before, |
int | count, | ||
const T & | value | ||
) |
Inserts count copies of value in front of the item pointed to by the iterator before. Returns an iterator pointing at the first of the inserted items.
|
inline |
Inserts value at index position index in the array. If index is 0 the value is prepended to the vector. If index is equal to size() 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 indexes i and above by one position further in memory. If you want a container class that provides a fast insert() method use QLinkedList instead.
|
inline |
Inserts count copies of value at index position index in the array.
|
inline |
|
inline |
|
inline |
int QVarLengthArray< T, Prealloc >::lastIndexOf | ( | const T & | value, |
int | from = -1 |
||
) | const |
Returns the index position of the last occurrence of the value in the array, searching backward from index position from. If from is -1 the search starts at the last item. Returns -1 if no item matched.
This method requires the type T to support operator==()
.
|
inline |
|
inline |
Appends value to the array and returns a reference to this vector.
|
inline |
Appends value to the array and returns a reference to this vector.
|
inline |
Assigns other to this array and returns a reference to this array.
|
inline |
Assigns the values of list to this array, and returns a reference to this array.
|
inline |
|
inline |
Equivalent to calling at[].
|
inline |
Equivalent to calling removeLast().
|
inline |
Inserts value 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 array by one position further in memory. If you want a container class that provides a fast prepend() method use QList or QLinkedList instead.
|
inline |
Appends value to the array, extending the array if necessary.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Decreases the size of the array by one. The allocated size is not changed.
|
inline |
|
inline |
|
inline |
Replaces the item at index position index with value. The index must be a valid position in the array.
void QVarLengthArray< T, Prealloc >::reserve | ( | int | size | ) |
Attempts to allocate memory for at least size elements. If you know in advance how large the array can get, you can call this method 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 purpose of this method is to provide a means of fine tuning QVarLengthArray's memory usage. This method is rarely used. If you want to know how many items are in the array, call size().
void QVarLengthArray< T, Prealloc >::resize | ( | int | size | ) |
Sets the size of the array to size. If 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 type has a user defined constructor the new elements are default initialized. Otherwise the elements are uninitialized. Refer to default constructed elements for additional information.
|
inline |
void QVarLengthArray< T, Prealloc >::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 purpose of this method is to provide a means of fine tuning QVarLengthArray's memory usage. This method is rarely used.
T QVarLengthArray< T, Prealloc >::value | ( | int | index | ) | const |
Returns the value at index position index. If index is guaranteed to be within bounds use at() which is slightly faster. If index is out of bounds a value initialized object is returned. Refer to default constructed elements for additional information.
T QVarLengthArray< T, Prealloc >::value | ( | int | index, |
const T & | defaultValue | ||
) | const |
Returns the value at index position index. If the index is out of bounds the defaultValue is returned.
|
related |
Returns 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 method requires the value type to have an implementation of operator==()
.
|
related |
Returns 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 method requires the value type to have an implementation of operator==()
.