Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |
The QVector class is a template class that provides a dynamic array. More...
#include <QVector>
Inherited by Q3ValueVector, QPolygon, QPolygonF, QStack, and QXmlStreamAttributes.
Note: All the functions in this class are reentrant.
The QVector class is a template class that provides a dynamic array.
QVector<T> is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.
QList<T>, QLinkedList<T>, and QVarLengthArray<T> provide similar functionality. Here's an overview:
Here's an example of a QVector that stores integers and a QVector that stores QString values:
QVector<int> integerVector; QVector<QString> stringVector;
QVector stores a vector (or array) of items. Typically, vectors are created with an initial size. For example, the following code constructs a QVector with 200 elements:
QVector<QString> vector(200);
The elements are automatically initialized with a default-constructed value. If you want to initialize the vector with a different value, pass that value as the second argument to the constructor:
QVector<QString> vector(200, "Pass");
You can also call fill() at any time to fill the vector with a value.
QVector uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const vectors, operator[]() returns a reference to the item that can be used on the left side of an assignment:
if (vector[0] == "Liz") vector[0] = "Elizabeth";
For read-only access, an alternative syntax is to use at():
for (int i = 0; i < vector.size(); ++i) { if (vector.at(i) == "Alfonso") cout << "Found Alfonso at position " << i << endl; }
at() can be faster than operator[](), because it never causes a deep copy to occur.
Another way to access the data stored in a QVector is to call data(). The function returns a pointer to the first item in the vector. You can use the pointer 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 function that accepts a plain 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 item if they found one; otherwise, they return -1. For example:
int i = vector.indexOf("Harumi"); if (i != -1) cout << "First occurrence of Harumi is at position " << i << endl;
If you simply want to check whether a vector contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the vector, use count().
QVector provides these basic functions to add, move, and remove items: insert(), replace(), remove(), prepend(), append(). With the exception of append(), these functions can be slow (linear time) for large vectors, because they require moving many items 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.
Unlike plain C++ arrays, QVectors can be resized at any time by calling resize(). If the new size is larger than the old size, QVector might need to reallocate the whole vector. QVector tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs.
If you know in advance approximately how many items the QVector will contain, you can call reserve(), asking QVector to preallocate a certain amount of memory. You can also call capacity() to find out how much memory QVector actually allocated.
QVector'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 *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.
Like the other container classes, QVector provides Java-style iterators (QVectorIterator and QMutableVectorIterator) and STL-style iterators (QVector::const_iterator and QVector::iterator). In practice, these are rarely used, because you can use indexes into the QVector.
In addition to QVector, Qt also provides QVarLengthArray, a very low-level class with little functionality that is optimized for speed.
QVector does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.
See also QVectorIterator, QMutableVectorIterator, QList, and QLinkedList.
Qt-style synonym for QVector::const_iterator.
Qt-style synonym for QVector::iterator.
The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack.
QVector provides both STL-style iterators and Java-style iterators. The STL-style const iterator is simply a typedef for "const T *" (pointer to const T).
See also QVector::constBegin(), QVector::constEnd(), QVector::iterator, and QVectorIterator.
Typedef for const T *. Provided for STL compatibility.
Typedef for T &. Provided for STL compatibility.
Typedef for ptrdiff_t. Provided for STL compatibility.
The QVector::iterator typedef provides an STL-style non-const iterator for QVector and QStack.
QVector provides both STL-style iterators and Java-style iterators. The STL-style non-const iterator is simply a typedef for "T *" (pointer to T).
See also QVector::begin(), QVector::end(), QVector::const_iterator, and QMutableVectorIterator.
Typedef for T *. Provided for STL compatibility.
Typedef for T &. Provided for STL compatibility.
Typedef for int. Provided for STL compatibility.
Typedef for T. Provided for STL compatibility.
Constructs an empty vector.
See also resize().
Constructs a vector with an initial size of size elements.
The elements are initialized with a default-constructed value.
See also resize().
Constructs a vector with an initial size of size elements. Each element is initialized with value.
Constructs a copy of other.
This operation takes constant time, because QVector is implicitly shared. This makes returning a QVector from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.
See also operator=().
Destroys the vector.
Inserts value at the end of the vector.
Example:
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, so it can grow without reallocating the entire vector each time.
See also operator<<(), prepend(), and insert().
Returns the item at index position i in the vector.
i must be a valid index position in the vector (i.e., 0 <= i < size()).
See also value() and operator[]().
This function is provided for STL compatibility. It is equivalent to last().
This is an overloaded member function, provided for convenience.
Returns an STL-style iterator pointing to the first item in the vector.
See also constBegin() and end().
This is an overloaded member function, provided for convenience.
Returns the maximum number of items that can be stored in the vector without forcing a reallocation.
The sole purpose of this function is to provide a means of fine tuning QVector'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 vector, call size().
See also reserve() and squeeze().
Removes all the elements from the vector.
Same as resize(0).
Returns a const STL-style iterator pointing to the first item in the vector.
See also begin() and constEnd().
Returns a const pointer to the data stored in the vector. The pointer can be used to access the items in the vector. The pointer remains valid as long as the vector isn't reallocated.
This function is mostly useful to pass a vector to a function that accepts a plain C++ array.
See also data() and operator[]().
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector.
See also constBegin() and end().
Returns true if the vector contains an occurrence of value; otherwise returns false.
This function requires the value type to have an implementation of operator==().
See also indexOf() and count().
Returns the number of occurrences of value in the vector.
This function requires the value type to have an implementation of operator==().
See also contains() and indexOf().
This is an overloaded member function, provided for convenience.
Same as size().
Returns a pointer to the data stored in the vector. The pointer can be used to access and modify the items in the vector.
Example:
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 isn't reallocated.
This function is mostly useful to pass a vector to a function that accepts a plain C++ array.
See also constData() and operator[]().
This is an overloaded member function, provided for convenience.
This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the vector is empty; otherwise returns false.
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector.
See also begin() and constEnd().
This is an overloaded member function, provided for convenience.
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().
This is an overloaded member function, provided for convenience.
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.
Assigns value to all items in the vector. If size is different from -1 (the default), the vector is resized to size size beforehand.
Example:
QVector<QString> vector(3); vector.fill("Yes"); // vector: ["Yes", "Yes", "Yes"] vector.fill("oh", 5); // vector: ["oh", "oh", "oh", "oh", "oh"]
See also resize().
Returns a reference to the first item in the vector. This function assumes that the vector isn't empty.
See also last() and isEmpty().
This is an overloaded member function, provided for convenience.
Returns a QVector object with the data contained in list.
Example:
QStringList list;
list << "Sven" << "Kim" << "Ola";
QVector<QString> vect = QVector<QString>::fromList(list);
// vect: ["Sven", "Kim", "Ola"]
See also toList() and QList::toVector().
Returns a QVector object with the data contained in vector. The order of the elements in the QVector is the same as in vector.
Example:
std::vector<double> stdvector; vector.push_back(1.2); vector.push_back(0.5); vector.push_back(3.14); QVector<double> vector = QVector<double>::fromStdVector(stdvector);
See also toStdVector() and QList::fromStdList().
This function is provided for STL compatibility. It is equivalent to first().
This is an overloaded member function, provided for convenience.
Returns the index position of the first occurrence of value in the vector, searching forward from index position from. Returns -1 if no item matched.
Example:
QVector<QString> vector; 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 function requires the value type to have an implementation of operator==().
See also lastIndexOf() and contains().
Inserts value at index position i in the vector. If i is 0, the value is prepended to the vector. If i is size(), the value is appended to the vector.
Example:
QVector<QString> 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 items at indexes i and above by one position further in memory. If you want a container class that provides a fast insert() function, use QLinkedList instead.
See also append(), prepend(), and remove().
This is an overloaded member function, provided for convenience.
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.
This is an overloaded member function, provided for convenience.
Inserts count copies of value at index position i in the vector.
Example:
QVector<double> 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]
This is an overloaded member function, provided for convenience.
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
Returns true if the vector has size 0; otherwise returns false.
Returns a reference to the last item in the vector. This function assumes that the vector isn't empty.
See also first() and isEmpty().
This is an overloaded member function, provided for convenience.
Returns the index position of the last occurrence of the value value in the vector, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.
Example:
QList<QString> vector; 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 function requires the value type to have an implementation of operator==().
See also indexOf().
Returns a vector whose elements are copied from this vector, starting at position pos. If length is -1 (the default), all elements after pos are copied; otherwise length elements (or all remaining elements if there are less than length elements) are copied.
This function is provided for STL compatibility. It is equivalent to erase(end() - 1).
This function is provided for STL compatibility. It is equivalent to erase(begin()).
Inserts value at the beginning of the vector.
Example:
QVector<QString> vector;
vector.prepend("one");
vector.prepend("two");
vector.prepend("three");
// vector: ["three", "two", "one"]
This is the same as vector.insert(0, value).
For large vectors, this operation can be slow (linear time), because it requires moving all the items in the vector by one position further in memory. If you want a container class that provides a fast prepend() function, use QList or QLinkedList instead.
See also append() and insert().
This function is provided for STL compatibility. It is equivalent to append(value).
This function is provided for STL compatibility. It is equivalent to prepend(value).
Removes the element at index position i.
See also insert(), replace(), and fill().
This is an overloaded member function, provided for convenience.
Removes count elements from the middle of the vector, starting at index position i.
See also insert(), replace(), and fill().
Replaces the item at index position i with value.
i must be a valid index position in the vector (i.e., 0 <= i < size()).
See also operator[]() and remove().
Attempts to allocate memory for at least size elements. If you know in advance how large the vector will be, 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 QVector will be a bit slower.
The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the vector, call resize().
See also squeeze() and capacity().
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 initialized with a default-constructed value. If size is less than the current size, elements are removed from the end.
See also size().
Returns the number of items in the vector.
See also isEmpty() and resize().
Releases any memory not required to store the items.
The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. In general, you will rarely ever need to call this function.
See also reserve() and capacity().
Returns a QList object with the data contained in this QVector.
Example:
QVector<double> vect;
vect << "red" << "green" << "blue" << "black";
QList<double> list = vect.toList();
// list: ["red", "green", "blue", "black"]
See also fromList() and QList::fromVector().
Returns a std::vector object with the data contained in this QVector. Example:
QVector<double> vector; vector << 1.2 << 0.5 << 3.14; std::vector<double> stdvector = vector.toStdVector();
See also fromStdVector() and QList::toStdList().
Returns the value at index position i in the vector.
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[]().
This is an overloaded member function, provided for convenience.
If the index i is out of bounds, the function returns defaultValue.
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 function requires the value type to have an implementation of operator==().
See also operator==().
Returns a vector that contains all the items in this vector followed by all the items in the other vector.
See also operator+=().
Appends the items of the other vector to this vector and returns a reference to this vector.
See also operator+() and append().
This is an overloaded member function, provided for convenience.
Appends value to the vector.
See also append() and operator<<().
Appends value to the vector and returns a reference to this vector.
See also append() and operator+=().
This is an overloaded member function, provided for convenience.
Appends other to the vector and returns a reference to the vector.
Assigns other to this vector and returns a reference to this vector.
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 function requires the value type to have an implementation of operator==().
See also operator!=().
Returns the item at index position i as a modifiable reference.
i must be a valid index position in the vector (i.e., 0 <= i < size()).
This is an overloaded member function, provided for convenience.
Same as at(i).
This is an overloaded member function, provided for convenience.
Writes the vector vector to stream out.
This function requires the value type to implement operator<<().
See also Format of the QDataStream operators.
This is an overloaded member function, provided for convenience.
Reads a vector from stream in into vector.
This function requires the value type to implement operator>>().
See also Format of the QDataStream operators.
Copyright © 2008 Trolltech | Trademarks | Qt 4.3.5 |