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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QVarLengthArray Class Reference

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

 #include <QVarLengthArray>

Note: All functions in this class are reentrant.

Public Types

typedef const_pointer
typedef const_reference
typedef difference_type
typedef pointer
typedef reference
typedef size_type
typedef value_type

Public Functions

QVarLengthArray ( int size = 0 )
QVarLengthArray ( const QVarLengthArray<T, Prealloc> & other )
~QVarLengthArray ()
void append ( const T & t )
void append ( const T * buf, int size )
const T & at ( int i ) const
int capacity () const
void clear ()
const T * constData () const
int count () const
T * data ()
const T * data () const
bool isEmpty () const
void removeLast ()
void reserve ( int size )
void resize ( int size )
int size () const
T value ( int i ) const
T value ( int i, const T & defaultValue ) const
QVarLengthArray<T, Prealloc> & operator= ( const QVarLengthArray<T, Prealloc> & other )
T & operator[] ( int i )
const T & operator[] ( int i ) const

Detailed Description

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.

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 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 assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *.

QVarLengthArray, like QVector, 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 of QVector's functionality.
  • QVarLengthArray doesn't initialize the memory if the value is a basic type. (QVector always does.)
  • QVector uses implicit 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 also QVector, QList, and QLinkedList.

Member Type Documentation

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

Constructs a copy of other.

QVarLengthArray::~QVarLengthArray ()

Destroys the array.

void QVarLengthArray::append ( const T & t )

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

See also removeLast().

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

Appends size amount of items referenced by buf to this array.

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

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

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

See also reserve().

void QVarLengthArray::clear ()

Removes all the elements from the array.

Same as resize(0).

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.

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

int QVarLengthArray::count () const

Same as size().

See also isEmpty() and resize().

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 also constData() and operator[]().

const T * QVarLengthArray::data () const

This is an overloaded function.

bool QVarLengthArray::isEmpty () const

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

See also size() and resize().

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

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

See also capacity().

void QVarLengthArray::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 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 also size().

int QVarLengthArray::size () const

Returns the number of elements in the array.

See also isEmpty() and 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 default-constructed value. If you are certain that i is within bounds, you can use at() instead, which is slightly faster.

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

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

This is an overloaded function.

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

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

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

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

See also data() and at().

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

This is an overloaded function.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 56
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Combiner licence, à propos et fermer d'une dernière manière

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.7
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