Qt Toolkit - QArray Class

# QArray Class Reference

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

`#include <qarray.h>`

Inherits QGArray.

Inherited by QByteArray and QPointArray.

List of all member functions.

## Public Members

- QArray ()
- QArray ( int size )
- QArray ( const QArray<type> & a )
- ~QArray ()
- QArray<type>& operator= ( const QArray<type> & a )
- type* data () const
- uint nrefs () const
- uint size () const
- bool isEmpty () const
- bool isNull () const
- bool resize ( uint size )
- bool truncate ( uint pos )
- bool fill ( const type & v, int size=-1=-1 )
- virtual void detach ()
- QArray<type> copy () const
- QArray<type>& assign ( const QArray<type> & a )
- QArray<type>& assign ( const type * data, uint size )
- QArray<type>& duplicate ( const QArray<type> & a )
- QArray<type>& duplicate ( const type * data, uint size )
- QArray<type>& setRawData ( const type * data, uint size )
- void resetRawData ( const type * data, uint size )
- int find ( const type & v, uint index=0 ) const
- int contains ( const type & v ) const
- type& operator[] ( int index ) const
- type& at ( uint index ) const
- operator const type* ()const
- bool operator== ( const QArray<type> & a ) const
- bool operator!= ( const QArray<type> & a ) const

## Protected Members

- QArray ( int, int )

## Detailed Description

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

QArray is implemented as both a template and a macro class. Define a template instance QArray<X> to create an array that contains X items.

The QVector collection class is also a kind of array. Like all collection classes, it has pointers to the contained items.

QArray stores the array elements directly in the array. It can only deal with simple types, i.e. C++ types, structs and classes that have no constructors, destructors or virtual functions. QArray uses bitwise operations to copy and compare array elements.

QArray uses explicit sharing with a reference count. If more than one array share common data, and one array is modified, all arrays will be modified.

The benefit of sharing is that a program does not need to duplicate data when it is not required, which results in less memory usage and less copying of data.

Example:

        #include <qarray.h>
        #include <stdio.h>

        QArray<int> fib( int num )                  // returns fibonacci array
        {
            ASSERT( num > 2 );
            QArray<int> f( num );                   // array of ints

            f[0] = f[1] = 1;                        // initialize first two numbers
            for ( int i=2; i<num; i++ )
                f[i] = f[i-1] + f[i-2];

            return f;
        }

        void main()
        {
            QArray<int> a = fib( 6 );               // get 6 first fibonaccis
            int i;

            for ( i=0; i<a.size(); i++ )            // print them
                prinf( "%d: %d\n", i, a[i] );

            printf( "1 is found %d time(s)\n", a.contains(1) );
            printf( "5 is found at index %d\n", a.find(5) );
        }

Program output:

            0: 1
            1: 1
            2: 2
            3: 3
            4: 5
            5: 8
            1 is found 2 times
            5 is found at index 4

An array can also be instantiated through a macro expansion, but this is necessary only for compilers that do not support templates. We recommend using templates if your compiler supports it. Macros are much harder to debug.

Macro expansion:

          ...
        Q_DECLARE(QArrayM,X);                       // declares the array

        void main()
        {
            QArrayM(X) a;                   // array of X elements
          ...

If you want to make your code work for compilers that do not have template support, but use templates if they are available, you can use `typedef:`

          ...
        #if defined(USE_TEMPLATECLASS)      // defined in qglobal.h
        typedef QArrayT<X>         XArray;
        #else
        typedef Q_DECLARE(QArrayM,X) XArray;
        #endif

        void main()
        {
            XArray a;                       // array of X elements
          ...

QArrayT refers to the QArray template and QArrayM refers to the QArray macro. QArray defaults to QArrayT if templates are supported, QArrayM otherwise.

See also: Shared Classes

## Member Function Documentation

### QArray::QArray ()

Constructs a null array.

See also: isNull().

### QArray::QArray ( const QArray<type> & a )

Constructs a shallow copy of a.

See also: assign().

### QArray::QArray ( int size )

Constructs an array with room for size elements. Makes a null array if size == 0.

Note that the elements are not initialized.

See also: resize() and isNull().

### QArray::QArray ( int, int ) `[protected]`

Constructs an array without allocating array space. The arguments should be (0, 0). Use at own risk.

### QArray::~QArray ()

Dereferences the array data and deletes it if this was the last reference.

### QArray::operator const type * () const

Cast operator. Returns a pointer to the array.

See also: data().

### QArray<type> & QArray::assign ( const QArray<type> & a )

Shallow copy. Dereferences the current array and references the data contained in a instead. Returns a reference to this array.

See also: operator=().

### QArray<type> & QArray::assign ( const type * data, uint size )

Shallow copy. Dereferences the current array and references the array data data, which contains size elements. Returns a reference to this array.

Do not delete data later, QArray takes care of that.

### type & QArray::at ( uint index ) const

Returns a reference to the element at position index in the array.

This can be used to both read and set an element.

See also: operator[]().

### int QArray::contains ( const type & v ) const

Returns the number of times v occurs in the array.

See also: find().

### QArray<type> QArray::copy () const

Returns a deep copy of this array.

See also: detach() and duplicate().

### type * QArray::data () const

Returns a pointer to the actual array data.

The array is a null array if data() == 0 (null pointer).

See also: isNull().

### void QArray::detach () `[virtual]`

Detaches this array from shared array data, i.e. makes a private, deep copy of the data.

Copying will only be performed if the reference count is greater than one.

See also: copy().

Reimplemented from QGArray.

### QArray<type> & QArray::duplicate ( const QArray<type> & a )

Deep copy. Dereferences the current array and obtains a copy of the data contained in a instead. Returns a reference to this array.

See also: copy().

### QArray<type> & QArray::duplicate ( const type * data, uint size )

Deep copy. Dereferences the current array and obtains a copy of the array data data instead. Returns a reference to this array.

See also: copy().

### bool QArray::fill ( const type & v, int size=-1=-1 )

Fills the array with the value v. If size is specified as different from -1, then the array will be resized before filled.

Returns TRUE if successful, or FALSE if the memory cannot be allocated (only when size != -1).

See also: resize().

### int QArray::find ( const type & v, uint index=0 ) const

Finds the first occurrence of v, starting at position index.

Returns the position of v, or -1 if v could not be found.

See also: contains().

### bool QArray::isEmpty () const

Returns TRUE if the array is empty, i.e. size() == 0, otherwise FALSE.

isEmpty() is equivalent with isNull() for QArray. Note that this is not the case for QString::isEmpty().

### bool QArray::isNull () const

Returns TRUE if the array is null, otherwise FALSE.

A null array has size() == 0 and data() == 0.

### uint QArray::nrefs () const

Returns the reference count for the shared array data. This reference count is always greater than zero.

### bool QArray::operator!= ( const QArray<type> & a ) const

Returns TRUE if this array is different from a, otherwise FALSE.

The two arrays are bitwise compared.

See also: operator==().

### QArray<type> & QArray::operator= ( const QArray<type> & a )

Assigns a shallow copy of a to this array and returns a reference to this array.

Equivalent to assign( a ).

### bool QArray::operator== ( const QArray<type> & a ) const

Returns TRUE if this array is equal to a, otherwise FALSE.

The two arrays are bitwise compared.

See also: operator!=().

### type & QArray::operator[] ( int index ) const

Returns a reference to the element at position index in the array.

This can be used to both read and set an element. Equivalent to at().

See also: at().

### void QArray::resetRawData ( const type * data, uint size )

Resets raw data that was set using setRawData().

The arguments must be the data and length that were passed to setRawData(). This is for consistency checking.

See also: setRawData().

### bool QArray::resize ( uint size )

Resizes (expands or shrinks) the array to size elements. The array becomes a null array if size == 0.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

New elements will not be initialized.

See also: size().

### QArray<type> & QArray::setRawData ( const type * data, uint size )

Sets raw data and returns a reference to the array.

Dereferences the current array and sets the new array data to data and the new array size to size. Do not attempt to resize or re-assign the array data when raw data has been set. Call resetRawData(d,len) to reset the array.

Setting raw data is useful because it sets QArray data without allocating memory or copying data.

Example I (intended use):

        static char bindata[] = { 231, 1, 44, ... };
        QByteArray  a;
        a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
        QDataStream s( a, IO_ReadOnly );            // open on a's data
        s >> <something>;                           // read raw bindata
        a.resetRawData( bindata, sizeof(bindata) ); // finished

Example II (you don't want to do this):

        static char bindata[] = { 231, 1, 44, ... };
        QByteArray  a, b;
        a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
        a.resize( 8 );                              // will crash
        b = a;                                      // will crash
        a[2] = 123;                                 // might crash
          // forget to resetRawData - will crash

Warning: If you do not call resetRawData(), QArray will attempt to deallocate or reallocate the raw data, which might not be too good. Be careful.

See also: resetRawData().

### uint QArray::size () const

Returns the size of the array (max number of elements).

The array is a null array if size() == 0.

See also: isNull() and resize().

Examples: drawdemo/drawdemo.cpp

### bool QArray::truncate ( uint pos )

Truncates the array at position pos.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

Equivalent to resize(pos).

See also: resize().

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qarray.h: 1998/08/27
- qarray.doc: 1997/08/06