QVector Class | Qt Core 5.15.18 (original) (raw)
Member Function Documentation
void QVector::push_front(T &&value)
void QVector::push_front(const T &value)
This function is provided for STL compatibility. It is equivalent to prepend(value).
QVector::iterator QVector::insert(QVector::iterator before, T &&value)
QVector::iterator QVector::insert(QVector::iterator before, const T &value)
This is an overloaded function.
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
void QVector::insert(int i, T &&value)
void QVector::insert(int i, const T &value)
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().
void QVector::prepend(T &&value)
void QVector::prepend(const T &value)
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().
template QVector::QVector(InputIterator first, InputIterator last)
Constructs a vector with the contents in the iterator range [first, last).
The value type of InputIterator
must be convertible to T
.
This function was introduced in Qt 5.14.
QVector::QVector(std::initializer_list<T> args)
Constructs a vector from the std::initializer_list given by args.
This constructor is only enabled if the compiler supports C++11 initializer lists.
This function was introduced in Qt 4.8.
QVector::QVector(QVector<T> &&other)
Move-constructs a QVector instance, making it point at the same object that other was pointing to.
This function was introduced in Qt 5.2.
QVector::QVector(const QVector<T> &other)
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=().
QVector::QVector(int size, const T &value)
Constructs a vector with an initial size of size elements. Each element is initialized with value.
QVector::QVector(int size)
Constructs a vector with an initial size of size elements.
The elements are initialized with a default-constructed value.
See also resize().
QVector::QVector()
Constructs an empty vector.
See also resize().
QVector<T> &QVector::operator=(QVector<T> &&other)
Move-assigns other to this QVector instance.
This function was introduced in Qt 5.2.
QVector<T> &QVector::operator=(const QVector<T> &other)
Assigns other to this vector and returns a reference to this vector.
QVector::~QVector()
Destroys the vector.
void QVector::append(const T &value)
Inserts value at the end of the vector.
Example:
QVector<QString> vector; vector.append("one"); vector.append("two"); QString three = "three"; vector.append(three); // vector: ["one", "two", "three"] // three: "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().
void QVector::append(T &&value)
This is an overloaded function.
Example:
QVector<QString> vector; vector.append("one"); vector.append("two"); QString three = "three"; vector.append(std::move(three)); // vector: ["one", "two", "three"] // three: ""
This function was introduced in Qt 5.6.
void QVector::append(const QVector<T> &value)
This is an overloaded function.
Appends the items of the value vector to this vector.
This function was introduced in Qt 5.5.
See also operator<<() and operator+=().
const T &QVector::at(int i) const
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[]().
QVector::reference QVector::back()
This function is provided for STL compatibility. It is equivalent to last().
QVector::const_reference QVector::back() const
This is an overloaded function.
QVector::iterator QVector::begin()
Returns an STL-style iterator pointing to the first item in the vector.
See also constBegin() and end().
QVector::const_iterator QVector::begin() const
This is an overloaded function.
int QVector::capacity() const
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().
QVector::const_iterator QVector::cbegin() const
Returns a const STL-style iterator pointing to the first item in the vector.
This function was introduced in Qt 5.0.
QVector::const_iterator QVector::cend() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector.
This function was introduced in Qt 5.0.
void QVector::clear()
Removes all the elements from the vector.
Note: Until Qt 5.6, this also released the memory used by the vector. From Qt 5.7, the capacity is preserved. To shed all capacity, swap with a default-constructed vector:
or call squeeze().
See also squeeze().
QVector::const_iterator QVector::constBegin() const
Returns a const STL-style iterator pointing to the first item in the vector.
See also begin() and constEnd().
const T *QVector::constData() const
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[]().
QVector::const_iterator QVector::constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector.
See also constBegin() and end().
const T &QVector::constFirst() const
Returns a const reference to the first item in the vector. This function assumes that the vector isn't empty.
This function was introduced in Qt 5.6.
See also constLast(), isEmpty(), and first().
const T &QVector::constLast() const
Returns a const reference to the last item in the vector. This function assumes that the vector isn't empty.
This function was introduced in Qt 5.6.
See also constFirst(), isEmpty(), and last().
bool QVector::contains(const T &value) const
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().
int QVector::count(const T &value) const
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().
int QVector::count() const
This is an overloaded function.
Same as size().
QVector::const_reverse_iterator QVector::crbegin() const
Returns a const STL-style reverse iterator pointing to the first item in the vector, in reverse order.
This function was introduced in Qt 5.6.
See also begin(), rbegin(), and rend().
QVector::const_reverse_iterator QVector::crend() const
Returns a const STL-style reverse iterator pointing to one past the last item in the vector, in reverse order.
This function was introduced in Qt 5.6.
See also end(), rend(), and rbegin().
T *QVector::data()
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 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[]().
const T *QVector::data() const
This is an overloaded function.
bool QVector::empty() const
This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true
if the vector is empty; otherwise returns false
.
QVector::iterator QVector::end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector.
See also begin() and constEnd().
QVector::const_iterator QVector::end() const
This is an overloaded function.
bool QVector::endsWith(const T &value) const
Returns true
if this vector is not empty and its last item is equal to value; otherwise returns false
.
This function was introduced in Qt 4.5.
See also isEmpty() and last().
QVector::iterator QVector::erase(QVector::iterator pos)
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().
QVector::iterator QVector::erase(QVector::iterator begin, QVector::iterator end)
This is an overloaded function.
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.
QVector<T> &QVector::fill(const T &value, int size = -1)
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().
T &QVector::first()
Returns a reference to the first item in the vector. This function assumes that the vector isn't empty.
See also last(), isEmpty(), and constFirst().
const T &QVector::first() const
This is an overloaded function.
[static]
QVector<T> QVector::fromList(const QList<T> &list)
Returns a QVector object with the data contained in list.
Example:
Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.
See also toList() and QList::toVector().
[static]
QVector<T> QVector::fromStdVector(const std::vector<T> &vector)
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 stdvector; vector.push_back(1.2); vector.push_back(0.5); vector.push_back(3.14);
QVector vector = QVector::fromStdVector(stdvector);
Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.
See also toStdVector() and QList::fromStdList().
T &QVector::front()
This function is provided for STL compatibility. It is equivalent to first().
QVector::const_reference QVector::front() const
This is an overloaded function.
int QVector::indexOf(const T &value, int from = 0) const
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().
void QVector::insert(int i, int count, const T &value)
This is an overloaded function.
Inserts count copies of value at index position i in the vector.
Example:
QVector 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]
QVector::iterator QVector::insert(QVector::iterator before, int count, const T &value)
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.
bool QVector::isEmpty() const
Returns true
if the vector has size 0; otherwise returns false
.
T &QVector::last()
Returns a reference to the last item in the vector. This function assumes that the vector isn't empty.
See also first(), isEmpty(), and constLast().
const T &QVector::last() const
This is an overloaded function.
int QVector::lastIndexOf(const T &value, int from = -1) const
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().
int QVector::length() const
Provided for compatibility with QList.
This function was introduced in Qt 5.2.
See also size(), count(), and QList::length().
QVector<T> QVector::mid(int pos, int length = -1) const
Returns a sub-vector which contains elements from this vector, starting at position pos. If length is -1 (the default), all elements after pos are included; otherwise length elements (or all remaining elements if there are less than length elements) are included.
void QVector::move(int from, int to)
Moves the item at index position from to index position to.
Provided for compatibility with QList.
This function was introduced in Qt 5.6.
See also QList::move().
void QVector::pop_back()
This function is provided for STL compatibility. It is equivalent to removeLast().
void QVector::pop_front()
This function is provided for STL compatibility. It is equivalent to removeFirst().
void QVector::push_back(const T &value)
This function is provided for STL compatibility. It is equivalent to append(value).
void QVector::push_back(T &&value)
This is an overloaded function.
This function was introduced in Qt 5.6.
QVector::reverse_iterator QVector::rbegin()
Returns a STL-style reverse iterator pointing to the first item in the vector, in reverse order.
This function was introduced in Qt 5.6.
See also begin(), crbegin(), and rend().
QVector::const_reverse_iterator QVector::rbegin() const
This is an overloaded function.
This function was introduced in Qt 5.6.
void QVector::remove(int i)
This is an overloaded function.
Removes the element at index position i.
See also insert(), replace(), and fill().
void QVector::remove(int i, int count)
This is an overloaded function.
Removes count elements from the middle of the vector, starting at index position i.
See also insert(), replace(), and fill().
int QVector::removeAll(const T &t)
Removes all elements that compare equal to t from the vector. Returns the number of elements removed, if any.
Provided for compatibility with QList.
This function was introduced in Qt 5.4.
See also removeOne() and QList::removeAll().
void QVector::removeAt(int i)
Removes the element at index position i. Equivalent to
Provided for compatibility with QList.
This function was introduced in Qt 5.2.
See also remove() and QList::removeAt().
void QVector::removeFirst()
Removes the first item in the vector. Calling this function is equivalent to calling remove(0). The vector must not be empty. If the vector can be empty, call isEmpty() before calling this function.
This function was introduced in Qt 5.1.
See also remove(), takeFirst(), and isEmpty().
void QVector::removeLast()
Removes the last item in the vector. Calling this function is equivalent to calling remove(size() - 1). The vector must not be empty. If the vector can be empty, call isEmpty() before calling this function.
This function was introduced in Qt 5.1.
See also remove(), takeLast(), removeFirst(), and isEmpty().
bool QVector::removeOne(const T &t)
Removes the first element that compares equal to t from the vector. Returns whether an element was, in fact, removed.
Provided for compatibility with QList.
This function was introduced in Qt 5.4.
See also removeAll() and QList::removeOne().
QVector::reverse_iterator QVector::rend()
Returns a STL-style reverse iterator pointing to one past the last item in the vector, in reverse order.
This function was introduced in Qt 5.6.
See also end(), crend(), and rbegin().
QVector::const_reverse_iterator QVector::rend() const
This is an overloaded function.
This function was introduced in Qt 5.6.
void QVector::replace(int i, const T &value)
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().
void QVector::reserve(int size)
Attempts to allocate memory for at least size elements. If you know in advance how large the vector will be, you should call this function to prevent reallocations and memory fragmentation.
If size is an underestimate, the worst that will happen is that the QVector will be a bit slower. If size is an overestimate, you may have used more memory than the normal QVector growth strategy would have allocated—or you may have used less.
An alternative to reserve() is calling resize(). Whether or not that is faster than reserve() depends on the element type, because resize() default-constructs all elements, and requires assignment to existing entries rather than calling append(), which copy- or move-constructs. For simple types, like int
or double
, resize() is typically faster, but for anything more complex, you should prefer reserve().
Warning: If the size passed to resize() was underestimated, you run out of allocated space and into undefined behavior. This problem does not exist with reserve(), because it treats the size as just a hint.
See also squeeze() and capacity().
void QVector::resize(int size)
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.
Since Qt 5.6, resize() doesn't shrink the capacity anymore. To shed excess capacity, use squeeze().
See also size().
void QVector::shrink_to_fit()
This function is provided for STL compatibility. It is equivalent to squeeze().
This function was introduced in Qt 5.10.
int QVector::size() const
Returns the number of items in the vector.
See also isEmpty() and resize().
void QVector::squeeze()
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().
bool QVector::startsWith(const T &value) const
Returns true
if this vector is not empty and its first item is equal to value; otherwise returns false
.
This function was introduced in Qt 4.5.
See also isEmpty() and first().
void QVector::swap(QVector<T> &other)
Swaps vector other with this vector. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
void QVector::swapItemsAt(int i, int j)
Exchange the item at index position i with the item at index position j. This function assumes that both i and j are at least 0 but less than size(). To avoid failure, test that both i and j are at least 0 and less than size().
This function was introduced in Qt 5.14.
T QVector::takeAt(int i)
Removes the element at index position i and returns it.
Equivalent to
T t = at(i); remove(i); return t;
Provided for compatibility with QList.
This function was introduced in Qt 5.2.
See also takeFirst(), takeLast(), and QList::takeAt().
T QVector::takeFirst()
Removes the first item in the vector and returns it. This function assumes the vector is not empty. To avoid failure, call isEmpty() before calling this function.
This function was introduced in Qt 5.1.
See also takeLast() and removeFirst().
T QVector::takeLast()
Removes the last item in the list and returns it. This function assumes the vector is not empty. To avoid failure, call isEmpty() before calling this function.
If you don't use the return value, removeLast() is more efficient.
This function was introduced in Qt 5.1.
See also takeFirst() and removeLast().
QList<T> QVector::toList() const
Returns a QList object with the data contained in this QVector.
Example:
QVector<QString> vect; vect << "red" << "green" << "blue" << "black";
QList<QString> list = vect.toList(); // list: ["red", "green", "blue", "black"]
Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.
See also fromList() and QList::fromVector().
std::vector<T> QVector::toStdVector() const
Returns a std::vector object with the data contained in this QVector. Example:
QVector vector; vector << 1.2 << 0.5 << 3.14;
std::vector stdvector = vector.toStdVector();
Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.
See also fromStdVector() and QList::toStdList().
T QVector::value(int i) const
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[]().
T QVector::value(int i, const T &defaultValue) const
This is an overloaded function.
If the index i is out of bounds, the function returns defaultValue.
bool QVector::operator!=(const QVector<T> &other) const
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==().
QVector<T> QVector::operator+(const QVector<T> &other) const
Returns a vector that contains all the items in this vector followed by all the items in the other vector.
See also operator+=().
QVector<T> &QVector::operator+=(const QVector<T> &other)
Appends the items of the other vector to this vector and returns a reference to this vector.
See also operator+() and append().
QVector<T> &QVector::operator+=(const T &value)
This is an overloaded function.
Appends value to the vector.
See also append() and operator<<().
QVector<T> &QVector::operator+=(T &&value)
This is an overloaded function.
This function was introduced in Qt 5.11.
See also append() and operator<<().
QVector<T> &QVector::operator<<(const T &value)
Appends value to the vector and returns a reference to this vector.
See also append() and operator+=().
QVector<T> &QVector::operator<<(const QVector<T> &other)
Appends other to the vector and returns a reference to the vector.
QVector<T> &QVector::operator<<(T &&value)
This is an overloaded function.
This function was introduced in Qt 5.11.
See also append() and operator+=().
QVector<T> &QVector::operator=(std::initializer_list<T> args)
Assigns the collection of values in args to this QVector instance.
This operator is only enabled if the compiler supports C++11 initializer lists.
This function was introduced in Qt 5.14.
bool QVector::operator==(const QVector<T> &other) const
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!=().
T &QVector::operator[](int _i_)
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()).
Note that using non-const operators can cause QVector to do a deep copy.
const T &QVector::operator[](int _i_) const
This is an overloaded function.
Same as at(i).