CopperSpice API  1.9.1
QVector< T > Class Template Reference

Template class which provides a dynamic or resizable array. More...

Inheritance diagram for QVector< T >:
QStack< T >

Classes

class  const_iterator
 The QVector::const_iterator class provides an STL style const iterator for QVector and QStack More...
 
class  iterator
 The QVector::iterator class provides an STL style iterator for QVector and QStack More...
 

Public Typedefs

using allocator_type = typename std::vector< T >::allocator_type
 
using const_iterator = typename std::vector< T >::const_iterator
 
using const_pointer = typename std::vector< T >::const_pointer
 
using const_reference = typename std::vector< T >::const_reference
 
using const_reverse_iterator = typename std::vector< T >::const_reverse_iterator
 
using difference_type = typename std::vector< T >::difference_type
 
using iterator = typename std::vector< T >::iterator
 
using Java_Iterator = QVectorIterator< T >
 
using Java_MutableIterator = QMutableVectorIterator< T >
 
using pointer = typename std::vector< T >::pointer
 
using reference = typename std::vector< T >::reference
 
using reverse_iterator = typename std::vector< T >::reverse_iterator
 
using size_type = typename std::vector< T >::difference_type
 
using value_type = typename std::vector< T >::value_type
 

Public Methods

 QVector () = default
 
 QVector (const QVector< T > &other) = default
 
template<class Input_Iterator >
 QVector (Input_Iterator first, Input_Iterator last)
 
 QVector (QVector< T > &&other) = default
 
 QVector (size_type size)
 
 QVector (size_type size, const T &value)
 
 QVector (std::initializer_list< T > args)
 
 ~QVector () = default
 
void append (const QVector< T > &other)
 
void append (const T &value)
 
template<typename Iter >
void append (Iter begin, Iter end)
 
void append (T &&value)
 
const_reference at (size_type pos) const
 
reference back ()
 
const_reference back () const
 
iterator begin ()
 
const_iterator begin () const
 
size_type capacity () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const T * constData () const
 
const_iterator constEnd () const
 
const_reference constFirst () const
 
const_reference constLast () const
 
bool contains (const T &value) const
 
size_type count () const
 
size_type count (const T &value) 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
 
bool endsWith (const T &value) const
 
iterator erase (const_iterator begin, const_iterator end)
 
iterator erase (const_iterator pos)
 
QVector< T > & fill (const T &value, size_type size=-1)
 
reference first ()
 
const_reference first () const
 
reference front ()
 
const_reference front () const
 
size_type indexOf (const T &value, size_type from=0) const
 
iterator insert (iterator before, const T &value)
 
iterator insert (iterator before, size_type count, const T &value)
 
template<typename Iter >
iterator insert (iterator pos, Iter begin, Iter end)
 
void insert (size_type pos, const T &value)
 
void insert (size_type pos, size_type count, const T &value)
 
bool isEmpty () const
 
reference last ()
 
const_reference last () const
 
size_type lastIndexOf (const T &value, size_type from=-1) const
 
size_type length () const
 
QVector< T > mid (size_type pos, size_type length=-1) const
 
void move (size_type from, size_type to)
 
bool operator!= (const QVector< T > &other) const
 
QVector< T > operator+ (const QVector< T > &other) const
 
QVector< T > & operator+= (const QVector< T > &other)
 
QVector< T > & operator+= (const T &value)
 
QVector< T > & operator<< (const QVector< T > &other)
 
QVector< T > & operator<< (const T &value)
 
QVector< T > & operator= (const QVector< T > &other) = default
 
QVector< T > & operator= (QVector< T > &&other) = default
 
bool operator== (const QVector< T > &other) const
 
reference operator[] (size_type i)
 
const_reference operator[] (size_type i) const
 
void pop_back ()
 
void pop_front ()
 
void prepend (const T &value)
 
void push_back (const T &value)
 
void push_back (T &&value)
 
void push_front (const T &value)
 
void push_front (T &&value)
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
void remove (size_type pos)
 
void remove (size_type pos, size_type count)
 
size_type removeAll (const T &value)
 
void removeAt (size_type pos)
 
void removeFirst ()
 
void removeLast ()
 
bool removeOne (const T &value)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
void replace (size_type i, const T &value)
 
void reserve (size_type size)
 
void resize (size_type size)
 
size_type size () const
 
void squeeze ()
 
bool startsWith (const T &value) const
 
void swap (QVector< T > &other)
 
takeAt (size_type pos)
 
takeFirst ()
 
takeLast ()
 
QList< T > toList () const
 
std::vector< T > toStdVector () const
 
value (size_type pos) const
 
value (size_type pos, const T &defaultValue) const
 

Static Public Methods

static QVector< T > fromList (const QList< T > &list)
 
static QVector< T > fromStdVector (const std::vector< T > &vector)
 

Related Functions

These are not member functions

bool operator< (const QVector< T > &lhs, const QVector< T > &rhs)
 
QDataStreamoperator<< (QDataStream &stream, const QVector< T > &vector)
 
bool operator<= (const QVector< T > &lhs, const QVector< T > &rhs)
 
bool operator> (const QVector< T > &lhs, const QVector< T > &rhs)
 
bool operator>= (const QVector< T > &lhs, const QVector< T > &rhs)
 
QDataStreamoperator>> (QDataStream &stream, QVector< T > &vector)
 

Detailed Description

template<typename T>
class QVector< T >

QVector is a template class which provides a dynamic or resizable array. This container stores elements in contiguous memory locations and provides fast index access.

  • Operations like prepend() and insert() are usually faster using QList than QVector
  • When elements need to occupy adjacent memory locations, to support a C style API, use QVector
  • If you want a low level variable size array, QVarLengthArray may be sufficient.
  • QVector and QList support access by index, for a QLinkedList iterators must be used

For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.

Constructors

A QVector can be created with an initial size. The following code constructs a QVector with 200 elements and each element will be default initialized. Refer to default constructed elements for additional information.

QVector<QString> vector(200);

If you want to declare and initialize the new elements with a specific value, pass the value as the second argument to the constructor as shown below. The fill() method could also be called after the vector is created to assign a given value to every element.

QVector<QString> myVector(200, "Pass");

Accessing Elements

QVector containers in C++ use 0 based indexes. To access the element at a particular index position, use operator[]. For non-const vectors, operator[] returns an lvalue reference to the element. If this operator appears on the left side of an assignment then the element in the container will be modified.

if (vector[0] == "Liz") {
vector[0] = "Elizabeth";
}

For read only access an alternative syntax is to use the at() method.

for (int i = 0; i < vector.size(); ++i) {
if (vector.at(i) == "Paris") {
cout << "Found Paris at position " << i << endl;
}
}

Another way to access the elements of a QVector is to call data(). This method returns a pointer to the first element in the vector. This pointer can be used to directly access and modify the elements stored in the vector. The pointer is also useful if you need to pass a QVector to a method which requires a C array.

If you want to find all occurrences of a particular value in a vector, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of the matching element if they found one, otherwise they return -1.

int pos = vector.indexOf("Amsterdam");
if (pos != -1) {
cout << "First occurrence of Amsterdam is at position " << pos << endl;
}

Basic Operations

QVector provides basic methods to add, move, and remove elements: insert(), replace(), remove(), prepend(), append().

For large vectors methods like insert(), remove(), and prepend() may be slow because they require moving elements in the vector by one position in memory. If you want a container class that provides fast insertion/removal in the middle, use QList or QLinkedList instead. Refer to linear time for more information.

If you want to check whether a vector contains a particular value use the method contains(). If you want to find out how many times a particular value occurs in the vector use count(). Unlike C arrays QVector can be resized at any time by calling the method resize(). If the new size is larger than the old size QVector might need to reallocate the entire vector.

If you know the approximate number of elements the QVector will contain, you can call reserve(). This method preallocates a certain amount of memory which is more efficient than reallocating several times. You can also call capacity() to find out how much memory QVector actually allocated.

Constraints on type T

The QVector 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.

A few methods have additional requirements. For example, indexOf() and lastIndexOf() need the value type T to support operator==(). If any other constraints exist, they will appear in the documentation for the given method.

QVector provides Java style iterators (QVectorIterator and QMutableVectorIterator) and STL style iterators (QVector::const_iterator and QVector::iterator). QVector is implemented so that direct index access is just as fast as using iterators.

Additional Information

In addition to QVector there is also QVarLengthArray which is a low-level class with a smaller API. It is optimized for speed. QVector does not support inserting, prepending, appending, or replacing with references to other elements in the vector.

See also
QList, QLinkedList

Member Typedef Documentation

template<typename T >
QVector< T >::allocator_type

Typedef for allocator used by the container.

template<typename T >
QVector< T >::const_iterator

Typedef for an STL style const iterator.

template<typename T >
QVector< T >::const_pointer

Typedef for const T *.

template<typename T >
QVector< T >::const_reference

Typedef for const T &.

template<typename T >
QVector< T >::const_reverse_iterator

Typedef for an STL style const reverse iterator.

template<typename T >
QVector< T >::difference_type

Typedef for integral type used to represent the distance between two elements.

template<typename T >
QVector< T >::iterator

Typedef for an STL style iterator.

template<typename T >
QVector< T >::Java_Iterator

Typedef for the java style const iterator.

See also
QSetIterator()
template<typename T >
QVector< T >::Java_MutableIterator

Typedef for the java style mutable iterator.

See also
QMutableSetIterator()
template<typename T >
QVector< T >::pointer

Typedef for T *.

template<typename T >
QVector< T >::reference

Typedef for T &.

template<typename T >
QVector< T >::reverse_iterator

Typedef for an STL style reverse iterator.

template<typename T >
QVector< T >::size_type

Typedef for a signed integer of the appropriate size for your platform.

template<typename T >
QVector< T >::value_type

Typedef for T.

Constructor & Destructor Documentation

template<typename T >
QVector< T >::QVector ( )
default

Constructs an empty QVector.

template<typename T >
QVector< T >::QVector ( size_type  size)
inlineexplicit

Constructs a QVector with an initial size of size elements. The elements are default initialized with a default-constructed value.

See also
resize()
template<typename T >
QVector< T >::QVector ( size_type  size,
const T &  value 
)
inline

Constructs a QVector with an initial size of size elements. Each element is initialized with value.

See also
resize(), fill()
template<typename T >
QVector< T >::QVector ( const QVector< T > &  other)
default

Copy constructs a new QVector from other.

template<typename T >
QVector< T >::QVector ( QVector< T > &&  other)
default

Move constructs a new QVector from other.

template<typename T >
QVector< T >::QVector ( std::initializer_list< T >  args)
inline

Construct a QVector from the given args.

template<typename T >
template<class Input_Iterator >
QVector< T >::QVector ( Input_Iterator  first,
Input_Iterator  last 
)
inline

Construct a QVector containing the elements from first to last.

template<typename T >
QVector< T >::~QVector ( )
default

Destroys the QVector.

Method Documentation

template<typename T >
void QVector< T >::append ( const QVector< T > &  other)
inline

Appends the elements of the other vector to this vector.

template<typename T >
void QVector< T >::append ( const T &  value)
inline

Copy appends value at the end of the vector.

QVector<QString> vector(0);
vector.append("one");
vector.append("two");
vector.append("three"); // vector: ["one", "two", "three"]

This is the same as calling resize(size() + 1) and assigning value to the new last element in the vector.

This operation is relatively fast because QVector typically allocates more memory than necessary. This means the vector can grow without reallocating the entire vector each time.

See also
operator<<(), prepend(), insert()
template<typename T >
template<typename Iter >
void QVector< T >::append ( Iter  begin,
Iter  end 
)
inline

Appends the elements in the range from begin to end to this QVector.

template<typename T >
void QVector< T >::append ( T &&  value)
inline

Move appends from value to this QVector.

template<typename T >
QVector< T >::const_reference QVector< T >::at ( size_type  pos) const
inline

Returns the element at index position pos in this QVector. The value for pos must be a valid index position in the QVector.

See also
value(), operator[]()
template<typename T >
reference QVector< T >::back ( )
inline

Returns a reference to the last element. Equivalent to calling last().

template<typename T >
const_reference QVector< T >::back ( ) const
inline

Returns a const reference to the last element. Equivalent to calling last().

template<typename T >
iterator QVector< T >::begin ( )
inline

Returns an STL style iterator pointing to the first element in the vector.

See also
constBegin(), end()
template<typename T >
const_iterator QVector< T >::begin ( ) const
inline

Returns a const STL style iterator pointing to the first element in the vector.

template<typename T >
size_type QVector< T >::capacity ( ) const
inline

Returns the maximum number of elements which can be stored in this QVector without forcing a reallocation.

See also
reserve(), squeeze()
template<typename T >
const_iterator QVector< T >::cbegin ( ) const
inline

Returns a const STL style iterator positioned at the first element in the QVector.

See also
cend()
template<typename T >
const_iterator QVector< T >::cend ( ) const
inline

Returns a const STL style iterator pointing to the imaginary element after the last element in the QVector.

See also
cbegin()
template<typename T >
void QVector< T >::clear ( )
inline

Removes all the elements from this QVector.

template<typename T >
const_iterator QVector< T >::constBegin ( ) const
inline

Returns a const STL style iterator pointing to the first element in the QVector.

See also
cbegin()
template<typename T >
const T * QVector< T >::constData ( ) const
inline

Returns a const pointer to the data which can be used to access the elements in the vector. The pointer remains valid as long as the vector is not reallocated.

This method is useful when you need to pass a vector to a method which accepts a C array.

See also
data(), operator[]()
template<typename T >
const_iterator QVector< T >::constEnd ( ) const
inline

Returns a const STL style iterator pointing to the imaginary element after the last element in the QVector.

See also
constBegin()
template<typename T >
const_reference QVector< T >::constFirst ( ) const
inline

Returns a reference to the first element in the QVector. This method assumes the QVector is not empty.

See also
last(), isEmpty()
template<typename T >
const_reference QVector< T >::constLast ( ) const
inline

Returns a const reference to the last element in the QVector. This method assumes the vector is not empty.

See also
first(), isEmpty()
template<typename T >
bool QVector< T >::contains ( const T &  value) const

Returns true if the vector contains an occurrence of value, otherwise returns false. This method requires the type T to support operator==().

See also
indexOf(), count()
template<typename T >
size_type QVector< T >::count ( ) const
inline

Equivalent to calling size().

template<typename T >
QVector< T >::size_type QVector< T >::count ( const T &  value) const

Returns the number of occurrences of value in the vector. This method requires the type T to support operator==().

See also
contains(), indexOf()
template<typename T >
const_reverse_iterator QVector< T >::crbegin ( ) const
inline

Returns a const STL style reverse iterator pointing to the first element in the vector, in reverse order.

template<typename T >
const_reverse_iterator QVector< T >::crend ( ) const
inline

Returns a const STL style reverse iterator pointing to one past the last element in the vector, in reverse order.

template<typename T >
T * QVector< T >::data ( )
inline

Returns a pointer to the data stored in the vector. The pointer can be used to access and modify the elements in the vector.

QVector<int> vector(10);
int *data = vector.data();
for (int i = 0; i < 10; ++i) {
data[i] = 2 * i;
}

The pointer remains valid as long as the vector is not reallocated. This method is mostly useful to pass a vector to a method which accepts a C array.

See also
constData(), operator[]()
template<typename T >
const T * QVector< T >::data ( ) const
inline

Returns a const pointer to the data stored in the QVector.

template<typename T >
bool QVector< T >::empty ( ) const
inline

This method is equivalent to calling isEmpty(). It returns true if the vector is empty, otherwise it returns false.

template<typename T >
iterator QVector< T >::end ( )
inline

Returns an STL style iterator pointing to the imaginary element after the last element in the vector.

See also
begin(), constEnd()
template<typename T >
const_iterator QVector< T >::end ( ) const
inline

Returns a const STL style iterator pointing to the imaginary element after the last element in the vector.

template<typename T >
bool QVector< T >::endsWith ( const T &  value) const
inline

Returns true if this vector is not empty and the last element is equal to value, otherwise returns false.

See also
isEmpty(), last()
template<typename T >
iterator QVector< T >::erase ( const_iterator  begin,
const_iterator  end 
)
inline

Removes all the elements from begin to end, not including the element pointed to by end. Returns an iterator to the element which end referred to, prior to calling this method.

template<typename T >
iterator QVector< T >::erase ( const_iterator  pos)
inline

Removes the element pointed to by pos from the vector. Returns an iterator to the next element in the QVector.

See also
insert(), remove()
template<typename T >
QVector< T > & QVector< T >::fill ( const T &  value,
size_type  size = -1 
)

Assigns value to all elements in the vector. If size is different from -1 the vector is first resized.

QVector<QString> vector(3);
vector.fill("Yes"); // vector: ["Yes", "Yes", "Yes"]
vector.fill("oh", 5); // vector: ["oh", "oh", "oh", "oh", "oh"]
See also
resize()
template<typename T >
reference QVector< T >::first ( )
inline

Returns a reference to the first element in the vector. This method assumes the vector is not empty.

See also
last(), isEmpty()
template<typename T >
const_reference QVector< T >::first ( ) const
inline

Returns a reference to the first element in the vector. This method assumes the vector is not empty.

template<typename T >
QVector< T > QVector< T >::fromList ( const QList< T > &  list)
inlinestatic

Returns a QVector object with the data contained in list.

list << "Sven" << "Kim" << "Ola";
// vect: ["Sven", "Kim", "Ola"]
See also
toList(), QList::toVector()
template<typename T >
QVector< T > QVector< T >::fromStdVector ( const std::vector< T > &  vector)
inlinestatic

Returns a QVector object with the data contained in vector.

std::vector<double> vector1;
vector1.push_back(1.2);
vector1.push_back(0.5);
vector1.push_back(3.14);
See also
toStdVector(), QList::fromStdList()
template<typename T >
reference QVector< T >::front ( )
inline

Equivalent to calling first().

See also
first()
template<typename T >
const_reference QVector< T >::front ( ) const
inline

Equivalent to calling first().

See also
first()
template<typename T >
size_type QVector< T >::indexOf ( const T &  value,
size_type  from = 0 
) const
inline

Returns the index position of the first occurrence of value in the vector, searching forward from index position from. Returns -1 if no element matched.

vector << "A" << "B" << "C" << "B" << "A";
vector.indexOf("B"); // returns 1
vector.indexOf("B", 1); // returns 1
vector.indexOf("B", 2); // returns 3
vector.indexOf("X"); // returns -1

This method requires the type T to support operator==().

See also
lastIndexOf(), contains()
template<typename T >
iterator QVector< T >::insert ( iterator  before,
const T &  value 
)
inline

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

template<typename T >
iterator QVector< T >::insert ( iterator  before,
size_type  count,
const T &  value 
)
inline

Inserts count copies of value in front of the element pointed to by the iterator before. Returns an iterator pointing at the first of the inserted elements.

template<typename T >
template<typename Iter >
iterator QVector< T >::insert ( iterator  pos,
Iter  begin,
Iter  end 
)
inline

Inserts the elements in the range from begin to end to this vector at iterator position pos.

template<typename T >
void QVector< T >::insert ( size_type  pos,
const T &  value 
)
inline

Inserts value at index position pos in the vector. If pos is 0 the value is prepended to the vector. If pos is equal to size(), the value is appended to the vector.

vector << "alpha" << "beta" << "delta";
vector.insert(2, "gamma"); // vector: ["alpha", "beta", "gamma", "delta"]

For large vectors this operation can be slow (linear time) because it requires moving all the elements past the current index. If you want a container class which provides a fast insert() method use QLinkedList instead.

See also
append(), prepend(), remove()
template<typename T >
void QVector< T >::insert ( size_type  pos,
size_type  count,
const T &  value 
)
inline

Inserts count copies of value at index position pos in the vector.

vector << 2.718 << 1.442 << 0.4342;
vector.insert(1, 3, 9.9); // vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
template<typename T >
bool QVector< T >::isEmpty ( ) const
inline

Returns true if the vector has size 0, otherwise returns false.

See also
size(), resize()
template<typename T >
reference QVector< T >::last ( )
inline

Returns a reference to the last element in the vector. This method assumes the vector is not empty.

See also
first(), isEmpty()
template<typename T >
const_reference QVector< T >::last ( ) const
inline

Returns a const reference to the last element in the vector. This method assumes the vector is not empty.

template<typename T >
size_type QVector< T >::lastIndexOf ( const T &  value,
size_type  from = -1 
) const
inline

Returns the index position of the last occurrence of the value in the vector, searching backward from index position from. If from is -1 the search starts at the last element. Returns -1 if no element matched.

vector << "A" << "B" << "C" << "B" << "A";
vector.lastIndexOf("B"); // returns 3
vector.lastIndexOf("B", 3); // returns 3
vector.lastIndexOf("B", 2); // returns 1
vector.lastIndexOf("X"); // returns -1

This method requires the type T to support operator==().

See also
indexOf()
template<typename T >
size_type QVector< T >::length ( ) const
inline

Equivalent to calling size() or count().

template<typename T >
QVector< T > QVector< T >::mid ( size_type  pos,
size_type  length = -1 
) const

Returns a vector whose elements are copied from this vector, starting at position pos. If length is -1 then all elements after pos are returned, otherwise the given length of elements is returned. If the length is greater than number of elements the value for length is ignored.

template<typename T >
void QVector< T >::move ( size_type  from,
size_type  to 
)
inline

Moves the element at index position from to index position to. Provided for compatibility with QList.

template<typename T >
bool QVector< T >::operator!= ( const QVector< T > &  other) const
inline

Returns true if other is not equal to this vector, otherwise returns false. Two vectors are considered equal if they contain the same values in the same order.

This method requires the type T to support operator==().

See also
operator==()
template<typename T >
QVector< T > QVector< T >::operator+ ( const QVector< T > &  other) const
inline

Returns a vector that contains all the elements in this vector followed by all the elements in the other vector.

See also
operator+=()
template<typename T >
QVector< T > & QVector< T >::operator+= ( const QVector< T > &  other)

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

See also
operator+(), append()
template<typename T >
QVector< T > & QVector< T >::operator+= ( const T &  value)
inline

Appends value to the vector.

See also
append(), operator<<()
template<typename T >
QVector< T > & QVector< T >::operator<< ( const QVector< T > &  other)
inline

Appends other to the vector and returns a reference to the vector.

template<typename T >
QVector< T > & QVector< T >::operator<< ( const T &  value)
inline

Appends value to the vector and returns a reference to this vector.

See also
append(), operator+=()
template<typename T >
QVector< T > & QVector< T >::operator= ( const QVector< T > &  other)
default

Copy assigns from other and returns a reference to this object.

template<typename T >
QVector< T > & QVector< T >::operator= ( QVector< T > &&  other)
default

Move assigns from other and returns a reference to this object.

This version takes an rvalue. Refer to references for a detailed description of rvalues and move semantics.

template<typename T >
bool QVector< T >::operator== ( const QVector< T > &  other) const
inline

Returns true if other is equal to this vector, otherwise returns false. Two vectors are considered equal if they contain the same values in the same order.

This method requires the type T to support operator==().

See also
operator!=()
template<typename T >
QVector< T >::reference QVector< T >::operator[] ( size_type  i)
inline

Returns a reference to the element at index position i. The value for i must be a valid index position in the vector.

See also
at(), value()
template<typename T >
QVector< T >::const_reference QVector< T >::operator[] ( size_type  i) const
inline

Equivalent to calling at(i).

template<typename T >
void QVector< T >::pop_back ( )
inline

This method is equivalent to calling erase(end() - 1).

template<typename T >
void QVector< T >::pop_front ( )
inline

This method is equivalent to calling erase(begin()).

template<typename T >
void QVector< T >::prepend ( const T &  value)
inline

Inserts value at the beginning of the vector.

vector.prepend("one");
vector.prepend("two");
vector.prepend("three"); // vector: ["three", "two", "one"]

Equivalent to calling vector.insert(0, value).

For large vectors this operation can be slow (linear time), because it requires moving all the elements by one position. If you want a container class which provides a fast prepend() method use QList instead.

See also
append(), insert()
template<typename T >
void QVector< T >::push_back ( const T &  value)
inline

Equivalent to calling append(value).

template<typename T >
void QVector< T >::push_back ( T &&  value)
inline

Equivalent to calling append(value).

template<typename T >
void QVector< T >::push_front ( const T &  value)
inline

Equivalent to calling prepend(value).

template<typename T >
void QVector< T >::push_front ( T &&  value)
inline

Equivalent to calling prepend(value).

template<typename T >
reverse_iterator QVector< T >::rbegin ( )
inline

Returns an STL style reverse iterator pointing to the last element in the vector.

template<typename T >
const_reverse_iterator QVector< T >::rbegin ( ) const
inline

Returns a const STL style reverse iterator pointing to the last element in the vector.

template<typename T >
void QVector< T >::remove ( size_type  pos)
inline

Removes the element at index position pos.

For large vectors this operation can be slow (linear time), because it requires moving all the elements at indexes i and above by one position further in memory. If you want a container class which provides a fast remove() method use QLinkedList instead.

See also
insert(), replace(), fill()
template<typename T >
void QVector< T >::remove ( size_type  pos,
size_type  count 
)
inline

Removes count elements from the middle of the vector, starting at index position pos.

See also
insert(), replace(), fill()
template<typename T >
QVector< T >::size_type QVector< T >::removeAll ( const T &  value)

Removes all elements which compare equal to value. Returns the number of elements removed, if any.

template<typename T >
void QVector< T >::removeAt ( size_type  pos)
inline

Removes the element at index position pos. Equivalent to calling remove(pos).

See also
insert(), replace(), fill()
template<typename T >
void QVector< T >::removeFirst ( )
inline

Removes the first element in the list. Calling this method is equivalent to calling removeAt(0). The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
removeAt(), takeFirst()
template<typename T >
void QVector< T >::removeLast ( )
inline

Removes the last element in the list. Calling this method is equivalent to calling removeAt(size() - 1). The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
removeAt(), takeLast()
template<typename T >
bool QVector< T >::removeOne ( const T &  value)
inline

Removes the first occurrence of value in the list and returns true on success, otherwise returns false.

data << "sun" << "cloud" << "sun" << "rain";
data.removeOne("sun");
// data: ["cloud", ,"sun", "rain"]

This method requires the type T to support operator==().

See also
removeAll(), removeAt(), takeAt(), replace()
template<typename T >
reverse_iterator QVector< T >::rend ( )
inline

Returns an STL style reverse iterator pointing to one before the first element in the vector.

template<typename T >
const_reverse_iterator QVector< T >::rend ( ) const
inline

Returns a const STL style reverse iterator pointing to one before the first element in the vector.

template<typename T >
void QVector< T >::replace ( size_type  i,
const T &  value 
)
inline

Replaces the element at index position i with value. The value for i must be a valid index position in the vector, i >= 0 and i < size().

See also
operator[](), remove()
template<typename T >
void QVector< T >::reserve ( size_type  size)
inline

Attempts to allocate memory for at least size elements. If you know in advance how large the vector will be, 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 QVector will be a bit slower.

The sole purpose of this method is to provide a means of fine tuning QVector's memory usage. In general, you will rarely ever need to call this method. If you want to change the size of the vector, call resize().

See also
squeeze(), capacity()
template<typename T >
void QVector< T >::resize ( size_type  size)
inline

Sets the size of the vector to size. If size is greater than the current size, elements are added to the end. The new elements are default initialized with a default-constructed value. If size is less than the current size, elements are removed from the end.

See also
size()
template<typename T >
size_type QVector< T >::size ( ) const
inline

Returns the number of elements in the vector.

See also
isEmpty(), resize()
template<typename T >
void QVector< T >::squeeze ( )
inline

Releases any memory not required to store the elements in the vector.

See also
reserve(), capacity()
template<typename T >
bool QVector< T >::startsWith ( const T &  value) const
inline

Returns true if this vector is not empty and its first element is equal to value, otherwise it returns false.

See also
isEmpty(), first()
template<typename T >
void QVector< T >::swap ( QVector< T > &  other)
inline

Swaps vector other with this vector. This operation is very fast and never fails.

template<typename T >
T QVector< T >::takeAt ( size_type  pos)
inline

Removes the element at index position pos and returns it. The value for pos must be a valid index position in the list, pos >= 0 and pos < size().

If you do not use the return value removeAt() is more efficient.

See also
removeAt(), takeFirst(), takeLast()
template<typename T >
T QVector< T >::takeFirst ( )
inline

Removes the first element in the list and returns it. This is the same as takeAt(0). This method assumes the list is not empty. To avoid failure, call isEmpty() before calling this method.

If you do not use the return value removeFirst() is more efficient.

See also
takeLast(), takeAt(), removeFirst()
template<typename T >
T QVector< T >::takeLast ( )
inline

Removes the last element in the list and returns it. This is the same as takeAt(size() - 1). This method assumes the list is not empty. To avoid failure, call isEmpty() before calling this method.

If you do not use the return value removeLast() is more efficient.

See also
takeFirst(), takeAt(), removeLast()
template<typename T >
QList< T > QVector< T >::toList ( ) const

Returns a QList object with the data contained in this QVector.

vect << "red" << "green" << "blue" << "black";
QList<QString> list = vect.toList();
// list: ["red", "green", "blue", "black"]
See also
fromList(), QList::fromVector()
template<typename T >
std::vector< T > QVector< T >::toStdVector ( ) const
inline

Returns a std::vector object with the data contained in this QVector.

vector2 << 1.2 << 0.5 << 3.14;
std::vector<double> vector2 = vector1.toStdVector();
See also
fromStdVector(), QList::toStdList()
template<typename T >
T QVector< T >::value ( size_type  pos) const

Returns the value at index position pos in the vector. If you are certain that pos is within bounds, use the method at() which is slightly faster. If the value for pos is out of bounds, a value initialized object of type T is returned. Refer to default constructed elements for additional information.

See also
at(), operator[]()
template<typename T >
T QVector< T >::value ( size_type  pos,
const T &  defaultValue 
) const

Returns the value at index position pos in the vector. If the index pos out of bounds then defaultValue is returned.

Friends And Related Function Documentation

bool operator< ( const QVector< T > &  lhs,
const QVector< T > &  rhs 
)
related

Returns true if vector lhs is lexicographically less than rhs, otherwise returns false. This method requires the type T to support operator<().

QDataStream & operator<< ( QDataStream stream,
const QVector< T > &  vector 
)
related

Writes the given vector to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

bool operator<= ( const QVector< T > &  lhs,
const QVector< T > &  rhs 
)
related

Returns true if vector lhs is lexicographically less than or equal to rhs, otherwise returns false. This method requires the type T to support operator<().

bool operator> ( const QVector< T > &  lhs,
const QVector< T > &  rhs 
)
related

Returns true if vector lhs is lexicographically greater than rhs, otherwise returns false. This method requires the type T to support operator<().

bool operator>= ( const QVector< T > &  lhs,
const QVector< T > &  rhs 
)
related

Returns true if vector lhs is lexicographically greater than or equal to rhs, otherwise returns false. This method requires the type T to support operator<().

QDataStream & operator>> ( QDataStream stream,
QVector< T > &  vector 
)
related

Reads from the stream into the given vector. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.