Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QArray Class

The QArray class is a template class that provides a dynamic array of simple types. More...

 #include <QArray>

Inherited by: QVector2DArray, QVector3DArray, and QVector4DArray.

This class was introduced in Qt 4.8.

Public Types

typedef ConstIterator
typedef Iterator
typedef const_iterator
typedef const_pointer
typedef const_reference
typedef difference_type
typedef iterator
typedef pointer
typedef reference
typedef size_type
typedef value_type

Public Functions

QArray()
QArray(int size)
QArray(int size, const T & value)
QArray(const T * values, int size)
QArray(const QArray<T, PreallocSize> & other)
~QArray()
void append(const T & value)
void append(const T * values, int count)
void append(const QArray<T, PreallocSize> & other)
void append(const T & value1, const T & value2)
void append(const T & value1, const T & value2, const T & value3)
void append(const T & value1, const T & value2, const T & value3, const T & value4)
const T & at(int index) const
reference back()
const_reference back() const
iterator begin()
const_iterator begin() const
int capacity() const
void clear()
const_iterator constBegin() const
const T * constData() const
const_iterator constEnd() const
bool contains(const T & value) const
int count(const T & value) const
int count() const
T * data()
const T * data() const
bool empty() const
iterator end()
const_iterator end() const
bool endsWith(const T & value) const
iterator erase(iterator pos)
iterator erase(iterator begin, iterator end)
T * extend(int size)
QArray<T, PreallocSize> & fill(const T & value, int size = -1)
T & first()
const T & first() const
reference front()
const_reference front() const
int indexOf(const T & value, int from = 0) const
void insert(int index, const T & value)
iterator insert(iterator before, int count, const T & value)
void insert(int index, int count, const T & value)
iterator insert(iterator before, const T & value)
bool isEmpty() const
T & last()
const T & last() const
int lastIndexOf(const T & value, int from = -1) const
QArray<T, PreallocSize> left(int length) const
QArray<T, PreallocSize> mid(int index, int length = -1) const
void pop_back()
void pop_front()
void prepend(const T & value)
void push_back(const T & value)
void push_front(const T & value)
void remove(int index, int count)
void remove(int index)
void removeFirst()
void removeLast()
void replace(int index, const T & value)
void replace(int index, const T * values, int count)
void reserve(int size)
void resize(int size)
void reverse()
QArray<T, PreallocSize> reversed() const
QArray<T, PreallocSize> right(int length) const
int size() const
void squeeze()
bool startsWith(const T & value) const
T value(int index) const
T value(int index, const T & defaultValue) const
bool operator!=(const QArray<T, PreallocSize> & other) const
QArray<T, PreallocSize> & operator+=(const QArray<T, PreallocSize> & other)
QArray<T, PreallocSize> & operator+=(const T & value)
QArray<T, PreallocSize> & operator<<(const QArray<T, PreallocSize> & other)
QArray<T, PreallocSize> & operator<<(const T & value)
QArray<T, PreallocSize> & operator=(const QArray<T, PreallocSize> & other)
bool operator==(const QArray<T, PreallocSize> & other) const
T & operator[](int index)
const T & operator[](int index) const

Static Public Members

QArray<T, PreallocSize> fromRawData(const T * data, int size)
QArray<T, PreallocSize> fromWritableRawData(T * data, int size)

Related Non-Members

QDataStream & operator<<(QDataStream & stream, const QArray<T, PreallocSize> & array)
QDataStream & operator>>(QDataStream & stream, QArray<T, PreallocSize> & array)

Detailed Description

The QArray class is a template class that provides a dynamic array of simple types.

QArray is similar to QVector except that it has much less overhead when constructing large arrays by appending individual elements one by one.

QArray instances have a preallocated data area for quickly building small arrays on the stack without malloc overhead. Once the array grows beyond the preallocated size, it is copied to the heap. The size of the preallocated area, which defaults to 8, can be specified with the second template parameter:

 QArray<QVector3D, 32> array;

QArray uses implicit sharing and copy-on-write semantics to support passing large arrays around an application with little overhead.

QArray is heavily optimized for copy-on-write and the case of constructing an array by calling append(). It has a slight performance penalty for random access using the non-const version of operator[]().

Member Type Documentation

typedef QArray::ConstIterator

Qt-style synonym for QArray::const_iterator.

typedef QArray::Iterator

Qt-style synonym for QArray::iterator.

typedef QArray::const_iterator

The QArray::iterator typedef provides an STL-style const iterator for QArray. The iterator is simply a typedef for "const T *" (pointer to const T).

See also QArray::constBegin() and QArray::iterator.

typedef QArray::const_pointer

Typedef for const T *. Provided for STL compatibility.

typedef QArray::const_reference

Typedef for T &. Provided for STL compatibility.

typedef QArray::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

typedef QArray::iterator

The QArray::iterator typedef provides an STL-style non-const iterator for QArray. The iterator is simply a typedef for "T *" (pointer to T).

See also QArray::begin() and QArray::const_iterator.

typedef QArray::pointer

Typedef for T *. Provided for STL compatibility.

typedef QArray::reference

Typedef for T &. Provided for STL compatibility.

typedef QArray::size_type

Typedef for int. Provided for STL compatibility.

typedef QArray::value_type

Typedef for T. Provided for STL compatibility.

Member Function Documentation

QArray::QArray()

Constructs an empty array.

See also reserve().

QArray::QArray(int size)

Constructs an array of size elements, all initialized to their default-constructed values.

QArray::QArray(int size, const T & value)

Constructs an array of size elements, all initialized to value.

See also fill().

QArray::QArray(const T * values, int size)

Constructs an array of size elements, initialized from values.

QArray::QArray(const QArray<T, PreallocSize> & other)

Constructs a copy of other.

See also operator=().

QArray::~QArray()

Destroys the array.

void QArray::append(const T & value)

Appends value to this array.

See also prepend() and insert().

void QArray::append(const T * values, int count)

Appends the count elements of values to this array.

void QArray::append(const QArray<T, PreallocSize> & other)

Appends the elements of other to this array.

void QArray::append(const T & value1, const T & value2)

This is an overloaded function.

Appends value1 and value2 to this array.

void QArray::append(const T & value1, const T & value2, const T & value3)

This is an overloaded function.

Appends value1, value2, and value3 to this array.

void QArray::append(const T & value1, const T & value2, const T & value3, const T & value4)

This is an overloaded function.

Appends value1, value2, value3, and value4 to this array.

const T & QArray::at(int index) const

Returns the item at position index in the array.

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

See also operator[](), constData(), and value().

reference QArray::back()

This function is provided for STL compatibility. It is equivalent to last().

const_reference QArray::back() const

This is an overloaded function.

iterator QArray::begin()

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

See also end(), constBegin(), and QArray::iterator.

const_iterator QArray::begin() const

This is an overloaded function.

int QArray::capacity() const

Returns the number of elements that can be stored in this array before reallocation.

See also reserve() and size().

void QArray::clear()

Clears all elements from this array and sets the size to zero.

This function will deallocate any memory that is used on the heap to store the array's elements. To reuse the same memory as before, call resize() with an argument of zero.

See also resize() and isEmpty().

const_iterator QArray::constBegin() const

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

See also constEnd(), begin(), and QArray::const_iterator.

const T * QArray::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 also data() and operator[]().

const_iterator QArray::constEnd() const

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

See also constBegin(), end(), and QArray::const_iterator.

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

Returns true if the array contains an occurrence of value; false otherwise.

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

See also indexOf() and count().

int QArray::count(const T & value) const

Returns the number of occurrences of value in the array.

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

See also contains() and indexOf().

int QArray::count() const

This is an overloaded function.

Same as size(), provided for convenience.

T * QArray::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.

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. It may make a deep copy of the array's elements if the array is implicitly shared.

See also constData() and operator[]().

const T * QArray::data() const

This is an overloaded function.

bool QArray::empty() const

This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the array is empty; otherwise returns false.

iterator QArray::end()

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

See also begin(), constEnd(), and QArray::iterator.

const_iterator QArray::end() const

This is an overloaded function.

bool QArray::endsWith(const T & value) const

Returns true if this array is not empty and its last item is equal to value; otherwise returns false.

See also isEmpty() and last().

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

See also insert() and remove().

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

This is an overloaded function.

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 * QArray::extend(int size)

Extends this array by size elements and returns a pointer to the storage, which is not initialized. The pointer is only valid until the array is reallocated or destroyed.

The append() or resize() functions are recommended if T is a complex type, with extend() only used for simple types. Because the storage is not initialized, the caller should use the in-place new operator to set elements:

 QArray<QRegExp> array;
 QRegExp *space = array.extend(1);
 new (space) QRegExp(QLatin1String("exp"));

See also append() and resize().

QArray<T, PreallocSize> & QArray::fill(const T & value, int size = -1)

Assigns value to all items in the array. If size is different from -1 (the default), the array is resized to size beforehand. Returns a reference to the array.

See also resize().

T & QArray::first()

Returns a reference to the first item in the array. This function assumes that the array isn't empty.

See also last() and isEmpty().

const T & QArray::first() const

This is an overloaded function.

QArray<T, PreallocSize> QArray::fromRawData(const T * data, int size) [static]

Returns an array consisting of the size elements from data.

This function takes a reference to data, but does not copy the elements until the array is modified. The memory at data must remain valid until the returned array is destroyed or modified.

Use append() instead of fromRawData() to force a copy to be made of the elements at data when the array is created:

 // Makes a copy of the data immediately.
 QArray<float> array;
 array.append(data, size);

 // Does not make a copy of the data until the array is modified.
 QArray<float> array;
 array = QArray<float>::fromRawData(data, size);

See also fromWritableRawData() and append().

QArray<T, PreallocSize> QArray::fromWritableRawData(T * data, int size) [static]

Returns an array consisting of the size elements from data.

This function takes a reference to data, but does not copy the elements until the array is reallocated to a larger size. The memory at data must remain valid until the returned array is destroyed or reallocated.

The elements of data will be modified in-place. This differs from fromRawData() which will make a copy of the elements of data when the array is modified.

If the returned array is resized to less than size, then a copy will not be made, and append() can be used to append new items up to size. Further calls to append() after size will force the array to be reallocated.

If the returned array is resized to more than size, then a copy of the data will be made and further modifications will not affect the elements at data.

See also fromRawData().

reference QArray::front()

This function is provided for STL compatibility. It is equivalent to first().

const_reference QArray::front() const

This is an overloaded function.

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

If from is negative, then it indicates an index position relative to the end of the array, -1 being the last index position.

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

See also lastIndexOf() and contains().

void QArray::insert(int index, const T & value)

Inserts value at position index in this array. If index is 0, then value is prepended to the array. If index is size(), then value is appended to the array.

See also append() and prepend().

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

void QArray::insert(int index, int count, const T & value)

This is an overloaded function.

Inserts count copies of value at position index in this array.

iterator QArray::insert(iterator before, const T & value)

This is an overloaded function.

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

bool QArray::isEmpty() const

Returns true if this array is empty; false otherwise.

See also size() and clear().

T & QArray::last()

Returns a reference to the last item in the array. This function assumes that the array isn't empty.

See also first() and isEmpty().

const T & QArray::last() const

This is an overloaded function.

int QArray::lastIndexOf(const T & value, int from = -1) const

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

If from is negative, then it indicates an index position relative to the end of the array, -1 being the last index position. The default for from is -1.

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

See also indexOf() and contains().

QArray<T, PreallocSize> QArray::left(int length) const

Returns an array containing the first length elements of this array. If length is less than zero, or greater than size(), then all elements in this array will be included in the return value.

See also mid() and right().

QArray<T, PreallocSize> QArray::mid(int index, int length = -1) const

Returns an array containing the length elements of this array, starting at index. If length is less than zero, or extends further than the end of the array, then all elements extending from index to the end of the array will be included in the return value.

See also left() and right().

void QArray::pop_back()

This function is provided for STL compatibility. It is equivalent to removeLast().

void QArray::pop_front()

This function is provided for STL compatibility. It is equivalent to removeFirst().

void QArray::prepend(const T & value)

Prepends value to this array.

See also append() and insert().

void QArray::push_back(const T & value)

This function is provided for STL compatibility. It is equivalent to append(value).

void QArray::push_front(const T & value)

This function is provided for STL compatibility. It is equivalent to prepend(value).

void QArray::remove(int index, int count)

Removes the count elements starting at position index in this array. If index or count is out of range, the set of removed elements will be truncated to those that are in range.

void QArray::remove(int index)

This is an overloaded function.

Removes the element at position index in this array.

void QArray::removeFirst()

Removes the first element from this array. Does nothing if the array is empty.

See also remove() and removeLast().

void QArray::removeLast()

Removes the last element from this array. Does nothing if the array is empty.

See also remove() and removeFirst().

void QArray::replace(int index, const T & value)

Replaces the element at index with value.

See also operator[]() and remove().

void QArray::replace(int index, const T * values, int count)

This is an overloaded function.

Replaces the count elements of this array with the contents of values, starting at index.

If (index + count) is larger than the current size of this array, the array will be extended to that size.

See also append().

void QArray::reserve(int size)

Increases the capacity of this array to reserve space for at least size elements. If the capacity is already larger than size, this function does nothing; in particular, it does not remove elements from the array like resize() does.

This function can be useful when you know how roughly many elements will be appended ahead of time. Reserving the space once can avoid unnecessary realloc operations later.

See also capacity(), resize(), and squeeze().

void QArray::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 and are initialized to a default-constructed value. If size is less than the current size, elements are removed from the end.

See also size(), reserve(), and squeeze().

void QArray::reverse()

Reverses the order of this array in place.

See also reversed().

QArray<T, PreallocSize> QArray::reversed() const

Returns a copy of this array with elements in the reverse order.

See also reverse().

QArray<T, PreallocSize> QArray::right(int length) const

Returns an array containing the last length elements of this array. If length is less than zero, or greater than size(), then all elements in this array will be included in the return value.

See also mid() and left().

int QArray::size() const

Returns the number of elements in this array.

See also resize(), capacity(), and isEmpty().

void QArray::squeeze()

Releases any memory not required to store the array's elements by reducing its capacity() to size().

This function is intended for reclaiming memory in an array that is being used over and over with different contents. As elements are added to an array, it will be constantly expanded in size. This function can realloc the array to a smaller size to reclaim unused memory.

See also reserve() and capacity().

bool QArray::startsWith(const T & value) const

Returns true if this array is not empty and its first item is equal to value; otherwise returns false.

See also isEmpty() and first().

T QArray::value(int index) const

Returns the value at position index in the vector.

If the index is out of bounds, the function returns a default-constructed value. If you are certain that index is within bounds, you can use at() instead, which is slightly faster.

See also at() and operator[]().

T QArray::value(int index, const T & defaultValue) const

This is an overloaded function.

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

bool QArray::operator!=(const QArray<T, PreallocSize> & other) const

Returns true if other is not equal to this array; otherwise returns false.

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

See also operator==().

QArray<T, PreallocSize> & QArray::operator+=(const QArray<T, PreallocSize> & other)

Appends the elements of the other array to this array and returns a reference to this array.

See also operator<<() and append().

QArray<T, PreallocSize> & QArray::operator+=(const T & value)

This is an overloaded function.

Appends value to this array and returns a reference to this array.

See also operator<<() and append().

QArray<T, PreallocSize> & QArray::operator<<(const QArray<T, PreallocSize> & other)

Appends the elements of the other array to this array and returns a reference to this array.

See also operator+=() and append().

QArray<T, PreallocSize> & QArray::operator<<(const T & value)

This is an overloaded function.

Appends value to this array and returns a reference to this array.

See also operator+=() and append().

QArray<T, PreallocSize> & QArray::operator=(const QArray<T, PreallocSize> & other)

Assigns other to this array and returns a reference to this array.

bool QArray::operator==(const QArray<T, PreallocSize> & other) const

Returns true if other is equal to this array; otherwise returns false.

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

See also operator!=().

T & QArray::operator[](int index)

Returns the item at position index as a modifiable reference.

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

Note that using non-const operators can cause QArray to do a deep copy.

See also at() and value().

const T & QArray::operator[](int index) const

This is an overloaded function.

Same as at(index).

Related Non-Members

QDataStream & operator<<(QDataStream & stream, const QArray<T, PreallocSize> & array)

Writes array to the given stream and returns a reference to the stream.

QDataStream & operator>>(QDataStream & stream, QArray<T, PreallocSize> & array)

Reads array from the given stream and returns a reference to the stream.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web