- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- Class List
- Class Hierarchy
- Class Members

Public Types | Public Member Functions | Static Public Member Functions | Friends

# QVector< T > Class Template Reference

Search for all occurrences

## Detailed Description

### template<typename T>
class QVector< T >

Examples:

  CurveBrush/CurveCreator.cpp, CurveBrush/CurveCreator.h, CurveBrush/CurveDisplayer.cpp, PLYImport/Importer.cpp, PtexExtractor/PtexPaintExporter.cpp, PtexExtractor/PtexPaintExporter.h, PtexExtractor/PtexUtilizer.cpp, PtexExtractor/PtexUtilizer.h, PtexImporter/PtexImporter.h, and STLExport/STLExporter.cpp.

Definition at line 102 of file qvector.h.

`#include <qvector.h>`

Inheritance diagram for QVector< T >:
[legend]

List of all members.

## Public Types
typedef T * | iterator
typedef const T * | const_iterator
typedef T | value_type
typedef value_type * | pointer
typedef const value_type * | const_pointer
typedef value_type & | reference
typedef const value_type & | const_reference
typedef qptrdiff | difference_type
typedef iterator | Iterator
typedef const_iterator | ConstIterator
typedef int | size_type
## Public Member Functions
| QVector ()
| QVector (int size)
| QVector (int size, const T &t)
| QVector (const QVector< T > &v)
| ~QVector ()
QVector< T > & | operator= (const QVector< T > &v)
bool | operator== (const QVector< T > &v) const
bool | operator!= (const QVector< T > &v) const
int | size () const
bool | isEmpty () const
void | resize (int size)
int | capacity () const
void | reserve (int size)
void | squeeze ()
void | detach ()
bool | isDetached () const
void | setSharable (bool sharable)
bool | isSharedWith (const QVector< T > &other) const
T * | data ()
const T * | data () const
const T * | constData () const
void | clear ()
const T & | at (int i) const
T & | operator[] (int i)
const T & | operator[] (int i) const
void | append (const T &t)
void | prepend (const T &t)
void | insert (int i, const T &t)
void | insert (int i, int n, const T &t)
void | replace (int i, const T &t)
void | remove (int i)
void | remove (int i, int n)
QVector< T > & | fill (const T &t, int size=-1)
int | indexOf (const T &t, int from=0) const
int | lastIndexOf (const T &t, int from=-1) const
bool | contains (const T &t) const
int | count (const T &t) const
iterator | begin ()
const_iterator | begin () const
const_iterator | constBegin () const
iterator | end ()
const_iterator | end () const
const_iterator | constEnd () const
iterator | insert (iterator before, int n, const T &x)
iterator | insert (iterator before, const T &x)
iterator | erase (iterator begin, iterator end)
iterator | erase (iterator pos)
int | count () const
T & | first ()
const T & | first () const
T & | last ()
const T & | last () const
bool | startsWith (const T &t) const
bool | endsWith (const T &t) const
QVector< T > | mid (int pos, int length=-1) const
T | value (int i) const
T | value (int i, const T &defaultValue) const
void | push_back (const T &t)
void | push_front (const T &t)
void | pop_back ()
void | pop_front ()
bool | empty () const
T & | front ()
const_reference | front () const
reference | back ()
const_reference | back () const
QVector< T > & | operator+= (const QVector< T > &l)
QVector< T > | operator+ (const QVector< T > &l) const
QVector< T > & | operator+= (const T &t)
QVector< T > & | operator<< (const T &t)
QVector< T > & | operator<< (const QVector< T > &l)
QList< T > | toList () const
std::vector< T > | toStdVector () const
## Static Public Member Functions
static QVector< T > | fromList (const QList< T > &list)
static QVector< T > | fromStdVector (const std::vector< T > &vector)
## Friends
class | QRegion

## Member Typedef Documentation

typedef T* iterator

Search for all occurrences

Definition at line 231 of file qvector.h.

typedef const T* const_iterator

Search for all occurrences

Definition at line 232 of file qvector.h.

typedef T value_type

Search for all occurrences

Definition at line 259 of file qvector.h.

typedef value_type* pointer

Search for all occurrences

Definition at line 260 of file qvector.h.

typedef const value_type* const_pointer

Search for all occurrences

Definition at line 261 of file qvector.h.

typedef value_type& reference

Search for all occurrences

Definition at line 262 of file qvector.h.

typedef const value_type& const_reference

Search for all occurrences

Definition at line 263 of file qvector.h.

typedef qptrdiff difference_type

Search for all occurrences

Definition at line 264 of file qvector.h.

typedef iterator Iterator

Search for all occurrences

Definition at line 265 of file qvector.h.

typedef const_iterator ConstIterator

Search for all occurrences

Definition at line 266 of file qvector.h.

typedef int size_type

Search for all occurrences

Definition at line 267 of file qvector.h.

## Constructor & Destructor Documentation

QVector | ( |  | ) | `[inline]`

Definition at line 115 of file qvector.h.

    : d(&QVectorData::shared_null) { d->ref.ref(); }

QVector | ( | int | size | ) | `[explicit]`

Definition at line 399 of file qvector.h.

    {
        d = malloc(asize);
        d->ref = 1;
        d->alloc = d->size = asize;
        d->sharable = true;
        d->capacity = false;
        if (QTypeInfo<T>::isComplex) {
            T* b = p->array;
            T* i = p->array + d->size;
            while (i != b)
                new (--i) T;
        } else {
            qMemSet(p->array, 0, asize * sizeof(T));
        }
    }

QVector | ( | int | size,
|  | const T & | t
| ) |  |

Definition at line 417 of file qvector.h.

    {
        d = malloc(asize);
        d->ref = 1;
        d->alloc = d->size = asize;
        d->sharable = true;
        d->capacity = false;
        T* i = p->array + d->size;
        while (i != p->array)
            new (--i) T(t);
    }

QVector | ( | const QVector< T > & | v | ) | `[inline]`

Definition at line 118 of file qvector.h.

    : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }

~QVector | ( |  | ) | `[inline]`

Definition at line 119 of file qvector.h.

    { if (!d) return; if (!d->ref.deref()) free(p); }

## Member Function Documentation

QVector< T > & operator= | ( | const QVector< T > & | v | ) |

Definition at line 378 of file qvector.h.

    {
        QVectorData *o = v.d;
        o->ref.ref();
        if (!d->ref.deref())
            free(p);
        d = o;
        if (!d->sharable)
            detach_helper();
        return *this;
    }

bool operator== | ( | const QVector< T > & | v | ) | const

Definition at line 621 of file qvector.h.

    {
        if (d->size != v.d->size)
            return false;
        if (d == v.d)
            return true;
        T* b = p->array;
        T* i = b + d->size;
        T* j = v.p->array + d->size;
        while (i != b)
            if (!(*--i == *--j))
                return false;
        return true;
    }

bool operator!= | ( | const QVector< T > & | v | ) | const `[inline]`

Definition at line 122 of file qvector.h.

    { return !(*this == v); }

int size | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 124 of file qvector.h.

    { return d->size; }

bool isEmpty | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 126 of file qvector.h.

    { return d->size == 0; }

void resize | ( | int | size | ) |

Search for all occurrences

Definition at line 330 of file qvector.h.

    { realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
              QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic)
              : d->alloc); }

int capacity | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 130 of file qvector.h.

    { return d->alloc; }

void reserve | ( | int | size | ) |

Search for all occurrences

Definition at line 327 of file qvector.h.

    { if (asize > d->alloc) realloc(d->size, asize); if (d->ref == 1) d->capacity = 1; }

void squeeze | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 132 of file qvector.h.

    { realloc(d->size, d->size); d->capacity = 0; }

void detach | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 134 of file qvector.h.

    { if (d->ref != 1) detach_helper(); }

bool isDetached | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 135 of file qvector.h.

    { return d->ref == 1; }

void setSharable | ( | bool | sharable | ) | `[inline]`

Search for all occurrences

Definition at line 136 of file qvector.h.

    { if (!sharable) detach(); d->sharable = sharable; }

bool isSharedWith | ( | const QVector< T > & | other | ) | const `[inline]`

Search for all occurrences

Definition at line 137 of file qvector.h.

    { return d == other.d; }

T* data | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 139 of file qvector.h.

    { detach(); return p->array; }

const T* data | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 140 of file qvector.h.

    { return p->array; }

const T* constData | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 141 of file qvector.h.

    { return p->array; }

void clear | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 335 of file qvector.h.

    { *this = QVector<T>(); }

const T & at | ( | int | i | ) | const `[inline]`

Search for all occurrences

Definition at line 338 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
      return p->array[i]; }

T & operator[] | ( | int | i | ) | `[inline]`

Definition at line 346 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
      return data()[i]; }

const T & operator[] | ( | int | i | ) | const `[inline]`

Definition at line 342 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
      return p->array[i]; }

void append | ( | const T & | t | ) |

Search for all occurrences

Definition at line 545 of file qvector.h.

    {
        if (d->ref != 1 || d->size + 1 > d->alloc) {
            const T copy(t);
            realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T),
                                               QTypeInfo<T>::isStatic));
            if (QTypeInfo<T>::isComplex)
                new (p->array + d->size) T(copy);
            else
                p->array[d->size] = copy;
        } else {
            if (QTypeInfo<T>::isComplex)
                new (p->array + d->size) T(t);
            else
                p->array[d->size] = t;
        }
        ++d->size;
    }

void prepend | ( | const T & | t | ) | `[inline]`

Search for all occurrences

Definition at line 366 of file qvector.h.

    { insert(begin(), 1, t); }

void insert | ( | int | i,
|  | const T & | t
| ) |  | `[inline]`

Search for all occurrences

Definition at line 350 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
      insert(begin() + i, 1, t); }

void insert | ( | int | i,
|  | int | n,
|  | const T & | t
| ) |  | `[inline]`

Search for all occurrences

Definition at line 354 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
      insert(begin() + i, n, t); }

void replace | ( | int | i,
|  | const T & | t
| ) |  | `[inline]`

Search for all occurrences

Definition at line 370 of file qvector.h.

    {
        Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
        const T copy(t);
        data()[i] = copy;
    }

void remove | ( | int | i | ) | `[inline]`

Search for all occurrences

Definition at line 362 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
      erase(begin() + i, begin() + i + 1); }

void remove | ( | int | i,
|  | int | n
| ) |  | `[inline]`

Search for all occurrences

Definition at line 358 of file qvector.h.

    { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
      erase(begin() + i, begin() + i + n); }

QVector< T > & fill | ( | const T & | t,
|  | int | size = `-1`
| ) |  |

Search for all occurrences

Definition at line 637 of file qvector.h.

    {
        const T copy(from);
        resize(asize < 0 ? d->size : asize);
        if (d->size) {
            T *i = p->array + d->size;
            T *b = p->array;
            while (i != b)
                *--i = copy;
        }
        return *this;
    }

int indexOf | ( | const T & | t,
|  | int | from = `0`
| ) |  | const

Search for all occurrences

Definition at line 670 of file qvector.h.

    {
        if (from < 0)
            from = qMax(from + d->size, 0);
        if (from < d->size) {
            T* n = p->array + from - 1;
            T* e = p->array + d->size;
            while (++n != e)
                if (*n == t)
                    return n - p->array;
        }
        return -1;
    }

int lastIndexOf | ( | const T & | t,
|  | int | from = `-1`
| ) |  | const

Search for all occurrences

Definition at line 685 of file qvector.h.

    {
        if (from < 0)
            from += d->size;
        else if (from >= d->size)
            from = d->size-1;
        if (from >= 0) {
            T* b = p->array;
            T* n = p->array + from + 1;
            while (n != b) {
                if (*--n == t)
                    return n - b;
            }
        }
        return -1;
    }

bool contains | ( | const T & | t | ) | const

Search for all occurrences

Definition at line 703 of file qvector.h.

    {
        T* b = p->array;
        T* i = p->array + d->size;
        while (i != b)
            if (*--i == t)
                return true;
        return false;
    }

int count | ( | const T & | t | ) | const

Search for all occurrences

Definition at line 714 of file qvector.h.

    {
        int c = 0;
        T* b = p->array;
        T* i = p->array + d->size;
        while (i != b)
            if (*--i == t)
                ++c;
        return c;
    }

iterator begin | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 234 of file qvector.h.

    { detach(); return p->array; }

const_iterator begin | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 235 of file qvector.h.

    { return p->array; }

const_iterator constBegin | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 236 of file qvector.h.

    { return p->array; }

iterator end | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 237 of file qvector.h.

    { detach(); return p->array + d->size; }

const_iterator end | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 238 of file qvector.h.

    { return p->array + d->size; }

const_iterator constEnd | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 239 of file qvector.h.

    { return p->array + d->size; }

Q_TYPENAME QVector< T >::iterator insert | ( | iterator | before,
|  | int | n,
|  | const T & | x
| ) |  |

Search for all occurrences

Definition at line 565 of file qvector.h.

    {
        int offset = int(before - p->array);
        if (n != 0) {
            const T copy(t);
            if (d->ref != 1 || d->size + n > d->alloc)
                realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + n, sizeof(T),
                                                   QTypeInfo<T>::isStatic));
            if (QTypeInfo<T>::isStatic) {
                T *b = p->array + d->size;
                T *i = p->array + d->size + n;
                while (i != b)
                    new (--i) T;
                i = p->array + d->size;
                T *j = i + n;
                b = p->array + offset;
                while (i != b)
                    *--j = *--i;
                i = b+n;
                while (i != b)
                    *--i = copy;
            } else {
                T *b = p->array + offset;
                T *i = b + n;
                memmove(i, b, (d->size - offset) * sizeof(T));
                while (i != b)
                    new (--i) T(copy);
            }
            d->size += n;
        }
        return p->array + offset;
    }

iterator insert | ( | iterator | before,
|  | const T & | x
| ) |  | `[inline]`

Search for all occurrences

Definition at line 241 of file qvector.h.

    { return insert(before, 1, x); }

Q_TYPENAME QVector< T >::iterator erase | ( | iterator | begin,
|  | iterator | end
| ) |  |

Search for all occurrences

Definition at line 599 of file qvector.h.

    {
        int f = int(abegin - p->array);
        int l = int(aend - p->array);
        int n = l - f;
        detach();
        if (QTypeInfo<T>::isComplex) {
            qCopy(p->array+l, p->array+d->size, p->array+f);
            T *i = p->array+d->size;
            T* b = p->array+d->size-n;
            while (i != b) {
                --i;
                i->~T();
            }
        } else {
            memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
        }
        d->size -= n;
        return p->array + f;
    }

iterator erase | ( | iterator | pos | ) | `[inline]`

Search for all occurrences

Definition at line 243 of file qvector.h.

    { return erase(pos, pos+1); }

int count | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 246 of file qvector.h.

    { return d->size; }

T& first | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 247 of file qvector.h.

    { Q_ASSERT(!isEmpty()); return *begin(); }

const T& first | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 248 of file qvector.h.

    { Q_ASSERT(!isEmpty()); return *begin(); }

T& last | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 249 of file qvector.h.

    { Q_ASSERT(!isEmpty()); return *(end()-1); }

const T& last | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 250 of file qvector.h.

    { Q_ASSERT(!isEmpty()); return *(end()-1); }

bool startsWith | ( | const T & | t | ) | const `[inline]`

Search for all occurrences

Definition at line 251 of file qvector.h.

    { return !isEmpty() && first() == t; }

bool endsWith | ( | const T & | t | ) | const `[inline]`

Search for all occurrences

Definition at line 252 of file qvector.h.

    { return !isEmpty() && last() == t; }

Q_OUTOFLINE_TEMPLATE QVector< T > mid | ( | int | pos,
|  | int | length = `-1`
| ) |  | const

Search for all occurrences

Definition at line 726 of file qvector.h.

    {
        if (length < 0)
            length = size() - pos;
        if (pos == 0 && length == size())
            return *this;
        if (pos + length > size())
            length = size() - pos;
        QVector<T> copy;
        copy.reserve(length);
        for (int i = pos; i < pos + length; ++i)
            copy += at(i);
        return copy;
    }

Q_OUTOFLINE_TEMPLATE T value | ( | int | i | ) | const

Search for all occurrences

Definition at line 531 of file qvector.h.

    {
        if (i < 0 || i >= d->size) {
            return T();
        }
        return p->array[i];
    }

Q_OUTOFLINE_TEMPLATE T value | ( | int | i,
|  | const T & | defaultValue
| ) |  | const

Search for all occurrences

Definition at line 539 of file qvector.h.

    {
        return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
    }

void push_back | ( | const T & | t | ) | `[inline]`

Search for all occurrences

Definition at line 268 of file qvector.h.

    { append(t); }

void push_front | ( | const T & | t | ) | `[inline]`

Search for all occurrences

Definition at line 269 of file qvector.h.

    { prepend(t); }

void pop_back | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 270 of file qvector.h.

    { Q_ASSERT(!isEmpty()); erase(end()-1); }

void pop_front | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 271 of file qvector.h.

    { Q_ASSERT(!isEmpty()); erase(begin()); }

bool empty | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 272 of file qvector.h.

        { return d->size == 0; }

T& front | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 274 of file qvector.h.

    { return first(); }

const_reference front | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 275 of file qvector.h.

    { return first(); }

reference back | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 276 of file qvector.h.

    { return last(); }

const_reference back | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 277 of file qvector.h.

    { return last(); }

QVector< T > & operator+= | ( | const QVector< T > & | l | ) |

Definition at line 651 of file qvector.h.

    {
        int newSize = d->size + l.d->size;
        realloc(d->size, newSize);

        T *w = p->array + newSize;
        T *i = l.p->array + l.d->size;
        T *b = l.p->array;
        while (i != b) {
            if (QTypeInfo<T>::isComplex)
                new (--w) T(*--i);
            else
                *--w = *--i;
        }
        d->size = newSize;
        return *this;
    }

QVector<T> operator+ | ( | const QVector< T > & | l | ) | const `[inline]`

Definition at line 281 of file qvector.h.

        { QVector n = *this; n += l; return n; }

QVector<T>& operator+= | ( | const T & | t | ) | `[inline]`

Definition at line 283 of file qvector.h.

        { append(t); return *this; }

QVector<T>& operator<< | ( | const T & | t | ) | `[inline]`

Definition at line 285 of file qvector.h.

        { append(t); return *this; }

QVector<T>& operator<< | ( | const QVector< T > & | l | ) | `[inline]`

Definition at line 287 of file qvector.h.

        { *this += l; return *this; }

Q_OUTOFLINE_TEMPLATE QList< T > toList | ( |  | ) | const

Search for all occurrences

Definition at line 742 of file qvector.h.

    {
        QList<T> result;
        result.reserve(size());
        for (int i = 0; i < size(); ++i)
            result.append(at(i));
        return result;
    }

QVector< T > fromList | ( | const QList< T > & | list | ) | `[static]`

Search for all occurrences

Definition at line 761 of file qvector.h.

    {
        return list.toVector();
    }

static QVector<T> fromStdVector | ( | const std::vector< T > & | vector | ) | `[inline, static]`

Search for all occurrences

Definition at line 295 of file qvector.h.

        { QVector<T> tmp; tmp.reserve(vector.size()); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }

std::vector<T> toStdVector | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 297 of file qvector.h.

        { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }

## Friends And Related Function Documentation

friend class QRegion `[friend]`

Search for all occurrences

Definition at line 302 of file qvector.h.

## Member Data Documentation

QVectorData* d

Search for all occurrences

Definition at line 106 of file qvector.h.

Data* p

Search for all occurrences

Definition at line 110 of file qvector.h.

The documentation for this class was generated from the following file:

- QtCore/qvector.h