1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
7 \brief QVector is an alias for QList.
9 Please see the QList documentation for details.
15 \brief The QList class is a template class that provides a dynamic array.
22 QList<T> is one of Qt's generic \l{container classes}. It
23 stores its items in adjacent memory locations and provides fast
24 index-based access. QVector<T> used to be a different class in
25 Qt 5, but is now a simple alias to QList.
27 QList<T> and QVarLengthArray<T>
28 provide similar APIs and functionality. They are often interchangeable,
29 but there are performance consequences. Here is an overview of use cases:
32 \li QList should be your default first choice.
33 \li QVarLengthArray provides an array that reserves space on the stack,
34 but can dynamically grow onto the heap if required. It's good to
35 use for short lived containers that are usually small.
36 \li If you need a real linked list, which guarantees
37 \l{Algorithmic Complexity}{constant time} insertions mid-list and
38 uses iterators to items rather than indexes, use std::list.
41 \note QList and QVarLengthArray both guarantee C-compatible
43 \note QList in Qt 5 did not always have a C-compatible array layout and
44 we often recommended to use QVector instead for more predictable
45 performance. This is not the case in Qt 6 anymore, where both classes
46 now share an implementation and can be used interchangeably.
48 Here's an example of a QList that stores integers and a QList
49 that stores QString values:
51 \snippet code/src_corelib_tools_qlist.cpp 0
53 QList stores its items in an array of continuous memory. Typically, lists
54 are created with an initial size. For example, the following code
55 constructs a QList with 200 elements:
57 \snippet code/src_corelib_tools_qlist.cpp 1
59 The elements are automatically initialized with a
60 \l{default-constructed value}. If you want to initialize the
61 list with a different value, pass that value as the second
62 argument to the constructor:
64 \snippet code/src_corelib_tools_qlist.cpp 2
66 You can also call fill() at any time to fill the list with a
69 QList uses 0-based indexes, just like C++ arrays. To access the
70 item at a particular index position, you can use operator[](). On
71 non-const lists, operator[]() returns a reference to the item
72 that can be used on the left side of an assignment:
74 \snippet code/src_corelib_tools_qlist.cpp 3
76 For read-only access, an alternative syntax is to use at():
78 \snippet code/src_corelib_tools_qlist.cpp 4
80 at() can be faster than operator[](), because it never causes a
81 \l{deep copy} to occur.
83 Another way to access the data stored in a QList is to call
84 data(). The function returns a pointer to the first item in the
85 list. You can use the pointer to directly access and modify the
86 elements stored in the list. The pointer is also useful if you
87 need to pass a QList to a function that accepts a plain C++
90 If you want to find all occurrences of a particular value in a
91 list, use indexOf() or lastIndexOf(). The former searches
92 forward starting from a given index position, the latter searches
93 backward. Both return the index of the matching item if they found
94 one; otherwise, they return -1. For example:
96 \snippet code/src_corelib_tools_qlist.cpp 5
98 If you simply want to check whether a list contains a
99 particular value, use contains(). If you want to find out how
100 many times a particular value occurs in the list, use count().
102 QList provides these basic functions to add, move, and remove
103 items: insert(), replace(), remove(), prepend(), append(). With the
104 exception of append(), prepend() and replace(), these functions can be slow
105 (\l{linear time}) for large lists, because they require moving many items in
106 the list by one position in memory. If you want a container class that
107 provides fast insertion/removal in the middle, use std::list instead.
109 Unlike plain C++ arrays, QLists can be resized at any time by
110 calling resize(). If the new size is larger than the old size,
111 QList might need to reallocate the whole list. QList tries
112 to reduce the number of reallocations by preallocating up to twice
113 as much memory as the actual data needs.
115 If you're building a QList gradually and know in advance
116 approximately how many elements it will contain, you can call reserve(),
117 asking QList to preallocate a certain amount of memory.
118 You can also call capacity() to find out how much memory the
119 QList actually has allocated.
121 Note that using non-const operators and functions can cause QList
122 to do a deep copy of the data, due to \l{implicit sharing}.
124 QList's value type must be an \l{assignable data type}. This
125 covers most data types that are commonly used, but the compiler
126 won't let you, for example, store a QWidget as a value; instead,
127 store a QWidget *. A few functions have additional requirements;
128 for example, indexOf() and lastIndexOf() expect the value type to
129 support \c operator==(). These requirements are documented on a
132 For iterating over the items, see \l {Iterating over Containers}.
134 In addition to QList, Qt also provides QVarLengthArray, a very
135 low-level class with little functionality that is optimized for
138 \section2 More Information on Using Qt Containers
140 For a detailed discussion comparing Qt containers with each other and
141 with STL containers, see \l {Understand the Qt Containers}.
143 \section1 Maximum size and out-of-memory conditions
145 The maximum size of QList depends on the architecture. Most 64-bit
146 systems can allocate more than 2 GB of memory, with a typical limit
147 of 2^63 bytes. The actual value also depends on the overhead required for
148 managing the data block. As a result, you can expect the maximum size
149 of 2 GB minus overhead on 32-bit platforms, and 2^63 bytes minus overhead
150 on 64-bit platforms. The number of elements that can be stored in a
151 QList is this maximum size divided by the size of a stored element.
153 When memory allocation fails, QList uses the \l Q_CHECK_PTR macro,
154 which throws a \c std::bad_alloc exception if the application is being
155 compiled with exception support. If exceptions are disabled, then running
156 out of memory is undefined behavior.
158 Note that the operating system may impose further limits on applications
159 holding a lot of allocated memory, especially large, contiguous blocks.
160 Such considerations, the configuration of such behavior or any mitigation
161 are outside the scope of the Qt API.
165 \fn template <typename T> QList<T> QList<T>::mid(qsizetype pos, qsizetype length = -1) const
167 Returns a sub-list which contains elements from this list,
168 starting at position \a pos. If \a length is -1 (the default), all
169 elements after \a pos are included; otherwise \a length elements (or
170 all remaining elements if there are less than \a length elements)
175 \fn template <typename T> QList<T> QList<T>::first(qsizetype n) const
178 Returns a sub-list that contains the first \a n elements
181 \note The behavior is undefined when \a n < 0 or \a n > size().
187 \fn template <typename T> QList<T> QList<T>::last(qsizetype n) const
190 Returns a sub-list that contains the last \a n elements of this list.
192 \note The behavior is undefined when \a n < 0 or \a n > size().
194 \sa first(), sliced()
198 \fn template <typename T> QList<T> QList<T>::sliced(qsizetype pos, qsizetype n) const
201 Returns a sub-list that contains \a n elements of this list,
202 starting at position \a pos.
204 \note The behavior is undefined when \a pos < 0, \a n < 0,
205 or \a pos + \a n > size().
211 \fn template <typename T> QList<T> QList<T>::sliced(qsizetype pos) const
215 Returns a sub-list that contains the elements of this list starting at
216 position \a pos and extending to its end.
218 \note The behavior is undefined when \a pos < 0 or \a pos > size().
224/*! \fn template <typename T> QList<T>::QList()
226 Constructs an empty list.
232 \fn template <typename T> QList<T>::QList(QList<T> &&other)
234 Move-constructs a QList instance, making it point at the same
235 object that \a other was pointing to.
240/*! \fn template <typename T> QList<T>::QList(qsizetype size)
242 Constructs a list with an initial size of \a size elements.
244 The elements are initialized with a \l{default-constructed
250/*! \fn template <typename T> QList<T>::QList(qsizetype size, parameter_type value)
252 Constructs a list with an initial size of \a size elements.
253 Each element is initialized with \a value.
258/*! \fn template <typename T> QList<T>::QList(const QList<T> &other)
260 Constructs a copy of \a other.
262 This operation takes \l{Algorithmic Complexity}{constant time},
263 because QList is \l{implicitly shared}. This makes returning
264 a QList from a function very fast. If a shared instance is
265 modified, it will be copied (copy-on-write), and that takes
266 \l{Algorithmic Complexity}{linear time}.
271/*! \fn template <typename T> QList<T>::QList(std::initializer_list<T> args)
274 Constructs a list from the std::initializer_list given by \a args.
277/*! \fn template <typename T> template<typename InputIterator, if_input_iterator<InputIterator>> QList<T>::QList(InputIterator first, InputIterator last)
280 Constructs a list with the contents in the iterator range [\a first, \a last).
282 \note This constructor only participates in overload resolution if
283 \c InputIterator meets the requirements of a
284 \l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator}.
286 The value type of \c InputIterator must be convertible to \c T.
289/*! \fn template <typename T> QList<T>::~QList()
294/*! \fn template <typename T> QList<T> &QList<T>::operator=(const QList<T> &other)
296 Assigns \a other to this list and returns a reference to this
301 \fn template <typename T> QList<T> &QList<T>::operator=(QList<T> &&other)
303 Move-assigns \a other to this QList instance.
309 \fn template <typename T> QList<T> &QList<T>::operator=(std::initializer_list<T> args)
312 Assigns the collection of values in \a args to this QList instance.
315/*! \fn template <typename T> void QList<T>::swap(QList<T> &other)
318 Swaps list \a other with this list. This operation is very fast and
322/*! \fn template <typename T> void QList<T>::swapItemsAt(qsizetype i, qsizetype j)
324 Exchange the item at index position \a i with the item at index
325 position \a j. This function assumes that both \a i and \a j are
326 at least 0 but less than size(). To avoid failure, test that both
327 \a i and \a j are at least 0 and less than size().
331/*! \fn template <typename T> bool QList<T>::operator==(const QList<T> &other) const
333 Returns \c true if \a other is equal to this list; otherwise
336 Two lists are considered equal if they contain the same values
339 This function requires the value type to have an implementation
345/*! \fn template <typename T> bool QList<T>::operator!=(const QList<T> &other) const
347 Returns \c true if \a other is not equal to this list; otherwise
350 Two lists are considered equal if they contain the same values
353 This function requires the value type to have an implementation
359/*! \fn template <typename T> bool QList<T>::operator<(const QList<T> &other) const
362 Returns \c true if this list is
363 \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
364 {lexically less than} \a other; otherwise returns \c false.
366 This function requires the value type to have an implementation
370/*! \fn template <typename T> bool QList<T>::operator<=(const QList<T> &other) const
373 Returns \c true if this list is
374 \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
375 {lexically less than or equal to} \a other; otherwise returns \c false.
377 This function requires the value type to have an implementation
381/*! \fn template <typename T> bool QList<T>::operator>(const QList<T> &other) const
384 Returns \c true if this list is
385 \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
386 {lexically greater than} \a other; otherwise returns \c false.
388 This function requires the value type to have an implementation
392/*! \fn template <typename T> bool QList<T>::operator>=(const QList<T> &other) const
395 Returns \c true if this list is
396 \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
397 {lexically greater than or equal to} \a other; otherwise returns \c false.
399 This function requires the value type to have an implementation
404 \fn template <typename T> size_t qHash(const QList<T> &key, size_t seed = 0)
408 Returns the hash value for \a key,
409 using \a seed to seed the calculation.
411 This function requires qHash() to be overloaded for the value type \c T.
414/*! \fn template <typename T> qsizetype QList<T>::size() const
416 Returns the number of items in the list.
418 \sa isEmpty(), resize()
421/*! \fn template <typename T> bool QList<T>::isEmpty() const
423 Returns \c true if the list has size 0; otherwise returns \c false.
428/*! \fn template <typename T> void QList<T>::resize(qsizetype size)
431 Sets the size of the list to \a size. If \a size is greater than the
432 current size, elements are added to the end; the new elements are
433 initialized with a \l{default-constructed value}. If \a size is less
434 than the current size, elements are removed from the end.
436 If this list is not shared, the capacity() is preserved. Use squeeze()
437 to shed excess capacity.
439 \note In Qt versions prior to 5.7 (for QVector; QList lacked a resize()
440 until 6.0), this function released the memory used by the list instead of
441 preserving the capacity.
446/*! \fn template <typename T> qsizetype QList<T>::capacity() const
448 Returns the maximum number of items that can be stored in the
449 list without forcing a reallocation.
451 The sole purpose of this function is to provide a means of fine
452 tuning QList's memory usage. In general, you will rarely ever
453 need to call this function. If you want to know how many items are
454 in the list, call size().
456 \note a statically allocated list will report a capacity of 0,
457 even if it's not empty.
459 \warning The free space position in the allocated memory block is undefined.
460 In other words, you should not assume that the free memory is always located
461 at the end of the list. You can call reserve() to ensure that there is
462 enough space at the end.
464 \sa reserve(), squeeze()
467/*! \fn template <typename T> void QList<T>::reserve(qsizetype size)
469 Attempts to allocate memory for at least \a size elements.
471 If you know in advance how large the list will be, you should call this
472 function to prevent reallocations and memory fragmentation. If you resize
473 the list often, you are also likely to get better performance.
475 If in doubt about how much space shall be needed, it is usually better to
476 use an upper bound as \a size, or a high estimate of the most likely size,
477 if a strict upper bound would be much bigger than this. If \a size is an
478 underestimate, the list will grow as needed once the reserved size is
479 exceeded, which may lead to a larger allocation than your best overestimate
480 would have and will slow the operation that triggers it.
482 \warning reserve() reserves memory but does not change the size of the
483 list. Accessing data beyond the current end of the list is
484 undefined behavior. If you need to access memory beyond the current end of
485 the list, use resize().
487 \sa squeeze(), capacity(), resize()
490/*! \fn template <typename T> void QList<T>::squeeze()
492 Releases any memory not required to store the items.
494 The sole purpose of this function is to provide a means of fine
495 tuning QList's memory usage. In general, you will rarely ever
496 need to call this function.
498 \sa reserve(), capacity()
501/*! \fn template <typename T> void QList<T>::detach()
506/*! \fn template <typename T> bool QList<T>::isDetached() const
511/*! \fn template <typename T> void QList<T>::setSharable(bool sharable)
516/*! \fn template <typename T> bool QList<T>::isSharedWith(const QList<T> &other) const
521/*! \fn template <typename T> T *QList<T>::data()
523 Returns a pointer to the data stored in the list. The pointer
524 can be used to access and modify the items in the list.
527 \snippet code/src_corelib_tools_qlist.cpp 6
529 \warning The pointer is invalidated on detachment or when the QList is
532 This function is mostly useful to pass a list to a function
533 that accepts a plain C++ array.
535 \sa constData(), operator[]()
538/*! \fn template <typename T> const T *QList<T>::data() const
543/*! \fn template <typename T> const T *QList<T>::constData() const
545 Returns a const pointer to the data stored in the list. The
546 pointer can be used to access the items in the list.
548 \warning The pointer is invalidated on detachment or when the QList is
551 This function is mostly useful to pass a list to a function
552 that accepts a plain C++ array.
554 \sa data(), operator[]()
557/*! \fn template <typename T> void QList<T>::clear()
559 Removes all the elements from the list.
561 If this list is not shared, the capacity() is preserved. Use squeeze() to
562 shed excess capacity.
564 \note In Qt versions prior to 5.7 (for QVector) and 6.0 (for QList), this
565 function released the memory used by the list instead of preserving the
568 \sa resize(), squeeze()
571/*! \fn template <typename T> const T &QList<T>::at(qsizetype i) const
573 Returns the item at index position \a i in the list.
575 \a i must be a valid index position in the list (i.e., 0 <= \a
578 \sa value(), operator[]()
581/*! \fn template <typename T> T &QList<T>::operator[](qsizetype i)
583 Returns the item at index position \a i as a modifiable reference.
585 \a i must be a valid index position in the list (i.e., 0 <= \a i
588 Note that using non-const operators can cause QList to do a deep
594/*! \fn template <typename T> const T &QList<T>::operator[](qsizetype i) const
602 \fn template <typename T> void QList<T>::append(parameter_type value)
604 Inserts \a value at the end of the list.
607 \snippet code/src_corelib_tools_qlist.cpp 7
609 This is the same as calling resize(size() + 1) and assigning \a
610 value to the new last element in the list.
612 This operation is relatively fast, because QList typically
613 allocates more memory than necessary, so it can grow without
614 reallocating the entire list each time.
616 \sa operator<<(), prepend(), insert()
620 \fn template <typename T> void QList<T>::append(rvalue_ref value)
626 \snippet code/src_corelib_tools_qlist.cpp move-append
629/*! \fn template <typename T> void QList<T>::append(const QList<T> &value)
635 Appends the items of the \a value list to this list.
637 \sa operator<<(), operator+=()
640/*! \fn template <typename T> void QList<T>::append(QList<T> &&value)
645 Moves the items of the \a value list to the end of this list.
647 \sa operator<<(), operator+=()
651 \fn template <typename T> void QList<T>::prepend(parameter_type value)
652 \fn template <typename T> void QList<T>::prepend(rvalue_ref value)
654 Inserts \a value at the beginning of the list.
657 \snippet code/src_corelib_tools_qlist.cpp 8
659 This is the same as list.insert(0, \a value).
661 Normally this operation is relatively fast (amortized \l{constant time}).
662 QList is able to allocate extra memory at the beginning of the list data
663 and grow in that direction without reallocating or moving the data on each
664 operation. However if you want a container class with a guarantee of
665 \l{constant time} prepend, use std::list instead,
666 but prefer QList otherwise.
668 \sa append(), insert()
672 \fn template <typename T> template <typename ...Args> T &QList<T>::emplaceBack(Args&&... args)
673 \fn template <typename T> template <typename ...Args> T &QList<T>::emplace_back(Args&&... args)
675 Adds a new element to the end for the container. This new element
676 is constructed in-place using \a args as the arguments for its
679 Returns a reference to the new element.
682 \snippet code/src_corelib_tools_qlist.cpp emplace-back
684 It is also possible to access a newly created object by using
686 \snippet code/src_corelib_tools_qlist.cpp emplace-back-ref
688 This is the same as list.emplace(list.size(), \a args).
693/*! \fn template <typename T> void QList<T>::insert(qsizetype i, parameter_type value)
694 \fn template <typename T> void QList<T>::insert(qsizetype i, rvalue_ref value)
696 Inserts \a value at index position \a i in the list. If \a i is
697 0, the value is prepended to the list. If \a i is size(), the
698 value is appended to the list.
701 \snippet code/src_corelib_tools_qlist.cpp 9
703 For large lists, this operation can be slow (\l{linear time}),
704 because it requires moving all the items at indexes \a i and
705 above by one position further in memory. If you want a container
706 class that provides a fast insert() function, use std::list
709 \sa append(), prepend(), remove()
712/*! \fn template <typename T> void QList<T>::insert(qsizetype i, qsizetype count, parameter_type value)
716 Inserts \a count copies of \a value at index position \a i in the
720 \snippet code/src_corelib_tools_qlist.cpp 10
723/*! \fn template <typename T> QList<T>::iterator QList<T>::insert(const_iterator before, parameter_type value)
724 \fn template <typename T> QList<T>::iterator QList<T>::insert(const_iterator before, rvalue_ref value)
728 Inserts \a value in front of the item pointed to by the iterator
729 \a before. Returns an iterator pointing at the inserted item.
732/*! \fn template <typename T> QList<T>::iterator QList<T>::insert(const_iterator before, qsizetype count, parameter_type value)
734 Inserts \a count copies of \a value in front of the item pointed to
735 by the iterator \a before. Returns an iterator pointing at the
736 first of the inserted items.
740 \fn template <typename T> template <typename ...Args> QList<T>::iterator QList<T>::emplace(qsizetype i, Args&&... args)
742 Extends the container by inserting a new element at position \a i.
743 This new element is constructed in-place using \a args as the
744 arguments for its construction.
746 Returns an iterator to the new element.
749 \snippet code/src_corelib_tools_qlist.cpp emplace
751 \note It is guaranteed that the element will be created in place
752 at the beginning, but after that it might be copied or
753 moved to the right position.
759/*! \fn template <typename T> void QList<T>::replace(qsizetype i, parameter_type value)
760 \fn template <typename T> void QList<T>::replace(qsizetype i, rvalue_ref value)
762 Replaces the item at index position \a i with \a value.
764 \a i must be a valid index position in the list (i.e., 0 <= \a
767 \sa operator[](), remove()
770/*! \fn template <typename T> void QList<T>::remove(qsizetype i, qsizetype n = 1)
772 Removes \a n elements from the list, starting at index position \a i.
775 Element removal will preserve the list's capacity and not reduce the amount of
776 allocated memory. To shed extra capacity and free as much memory as possible,
780//! [iterator-invalidation-erase]
781 \note When QList is not \l{implicitly shared}, this function only
782 invalidates iterators at or after the specified position.
783//! [iterator-invalidation-erase]
785 \sa insert(), replace(), fill()
788/*! \fn template <typename T> void QList<T>::removeAt(qsizetype i)
791 Removes the element at index position \a i.
797 \include qlist.qdoc shrinking-erase
798 \include qlist.qdoc iterator-invalidation-erase
803/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::removeAll(const AT &t)
806 Removes all elements that compare equal to \a t from the
807 list. Returns the number of elements removed, if any.
809 \include qlist.qdoc shrinking-erase
814/*! \fn template <typename T> template <typename AT = T> bool QList<T>::removeOne(const AT &t)
817 Removes the first element that compares equal to \a t from the
818 list. Returns whether an element was, in fact, removed.
820 \include qlist.qdoc shrinking-erase
825/*! \fn template <typename T> template <typename Predicate> qsizetype QList<T>::removeIf(Predicate pred)
828 Removes all elements for which the predicate \a pred returns true
829 from the list. Returns the number of elements removed, if any.
834/*! \fn template <typename T> qsizetype QList<T>::length() const
837 Same as size() and count().
842/*! \fn template <typename T> T QList<T>::takeAt(qsizetype i)
845 Removes the element at index position \a i and returns it.
854 \include qlist.qdoc iterator-invalidation-erase
856 \sa takeFirst(), takeLast()
859/*! \fn template <typename T> void QList<T>::move(qsizetype from, qsizetype to)
862 Moves the item at index position \a from to index position \a to.
865/*! \fn template <typename T> void QList<T>::removeFirst()
867 Removes the first item in the list. Calling this function is
868 equivalent to calling remove(0). The list must not be empty. If
869 the list can be empty, call isEmpty() before calling this
872 \include qlist.qdoc shrinking-erase
874 \sa remove(), takeFirst(), isEmpty()
877/*! \fn template <typename T> void QList<T>::removeLast()
879 Removes the last item in the list. Calling this function is
880 equivalent to calling remove(size() - 1). The list must not be
881 empty. If the list can be empty, call isEmpty() before calling
884 \include qlist.qdoc shrinking-erase
886 \sa remove(), takeLast(), removeFirst(), isEmpty()
889/*! \fn template <typename T> T QList<T>::takeFirst()
892 Removes the first item in the list and returns it. This function
893 assumes the list is not empty. To avoid failure, call isEmpty()
894 before calling this function.
896 \sa takeLast(), removeFirst()
899/*! \fn template <typename T> T QList<T>::takeLast()
902 Removes the last item in the list and returns it. This function
903 assumes the list is not empty. To avoid failure, call isEmpty()
904 before calling this function.
906 If you don't use the return value, removeLast() is more
909 \sa takeFirst(), removeLast()
913 \fn template <typename T> template <typename ...Args> QList<T>::iterator QList<T>::emplace(const_iterator before, Args&&... args)
917 Creates a new element in front of the item pointed to by the
918 iterator \a before. This new element is constructed in-place
919 using \a args as the arguments for its construction.
921 Returns an iterator to the new element.
924/*! \fn template <typename T> QList<T> &QList<T>::fill(parameter_type value, qsizetype size = -1)
926 Assigns \a value to all items in the list. If \a size is
927 different from -1 (the default), the list is resized to \a size beforehand.
930 \snippet code/src_corelib_tools_qlist.cpp 11
935/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::indexOf(const AT &value, qsizetype from = 0) const
937 Returns the index position of the first occurrence of \a value in
938 the list, searching forward from index position \a from.
939 Returns -1 if no item matched.
942 \snippet code/src_corelib_tools_qlist.cpp 12
944 This function requires the value type to have an implementation of
947 \sa lastIndexOf(), contains()
950/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::lastIndexOf(const AT &value, qsizetype from = -1) const
952 Returns the index position of the last occurrence of the value \a
953 value in the list, searching backward from index position \a
954 from. If \a from is -1 (the default), the search starts at the
955 last item. Returns -1 if no item matched.
958 \snippet code/src_corelib_tools_qlist.cpp 13
960 This function requires the value type to have an implementation of
966/*! \fn template <typename T> template <typename AT = T> bool QList<T>::contains(const AT &value) const
968 Returns \c true if the list contains an occurrence of \a value;
969 otherwise returns \c false.
971 This function requires the value type to have an implementation of
974 \sa indexOf(), count()
977/*! \fn template <typename T> bool QList<T>::startsWith(parameter_type value) const
980 Returns \c true if this list is not empty and its first
981 item is equal to \a value; otherwise returns \c false.
983 \sa isEmpty(), first()
986/*! \fn template <typename T> bool QList<T>::endsWith(parameter_type value) const
989 Returns \c true if this list is not empty and its last
990 item is equal to \a value; otherwise returns \c false.
992 \sa isEmpty(), last()
996/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::count(const AT &value) const
998 Returns the number of occurrences of \a value in the list.
1000 This function requires the value type to have an implementation of
1003 \sa contains(), indexOf()
1006/*! \fn template <typename T> qsizetype QList<T>::count() const
1013/*! \fn template <typename T> QList<T>::iterator QList<T>::begin()
1015 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the
1016 first item in the list.
1018//! [iterator-invalidation-func-desc]
1019 \warning The returned iterator is invalidated on detachment or when the
1021//! [iterator-invalidation-func-desc]
1023 \sa constBegin(), end()
1026/*! \fn template <typename T> QList<T>::const_iterator QList<T>::begin() const
1031/*! \fn template <typename T> QList<T>::const_iterator QList<T>::cbegin() const
1034 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the
1035 first item in the list.
1037 \include qlist.qdoc iterator-invalidation-func-desc
1042/*! \fn template <typename T> QList<T>::const_iterator QList<T>::constBegin() const
1044 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the
1045 first item in the list.
1047 \include qlist.qdoc iterator-invalidation-func-desc
1049 \sa begin(), constEnd()
1052/*! \fn template <typename T> QList<T>::iterator QList<T>::end()
1054 Returns an \l{STL-style iterators}{STL-style iterator} pointing just after
1055 the last item in the list.
1057 \include qlist.qdoc iterator-invalidation-func-desc
1059 \sa begin(), constEnd()
1062/*! \fn template <typename T> QList<T>::const_iterator QList<T>::end() const
1067/*! \fn template <typename T> QList<T>::const_iterator QList<T>::cend() const
1070 Returns a const \l{STL-style iterators}{STL-style iterator} pointing just
1071 after the last item in the list.
1073 \include qlist.qdoc iterator-invalidation-func-desc
1078/*! \fn template <typename T> QList<T>::const_iterator QList<T>::constEnd() const
1080 Returns a const \l{STL-style iterators}{STL-style iterator} pointing just
1081 after the last item in the list.
1083 \include qlist.qdoc iterator-invalidation-func-desc
1085 \sa constBegin(), end()
1088/*! \fn template <typename T> QList<T>::reverse_iterator QList<T>::rbegin()
1091 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to
1092 the first item in the list, in reverse order.
1094 \include qlist.qdoc iterator-invalidation-func-desc
1096 \sa begin(), crbegin(), rend()
1099/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::rbegin() const
1104/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::crbegin() const
1107 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing
1108 to the first item in the list, in reverse order.
1110 \include qlist.qdoc iterator-invalidation-func-desc
1112 \sa begin(), rbegin(), rend()
1115/*! \fn template <typename T> QList<T>::reverse_iterator QList<T>::rend()
1118 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing just
1119 after the last item in the list, in reverse order.
1121 \include qlist.qdoc iterator-invalidation-func-desc
1123 \sa end(), crend(), rbegin()
1126/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::rend() const
1131/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::crend() const
1134 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing
1135 just after the last item in the list, in reverse order.
1137 \include qlist.qdoc iterator-invalidation-func-desc
1139 \sa end(), rend(), rbegin()
1142/*! \fn template <typename T> QList<T>::iterator QList<T>::erase(const_iterator pos)
1144 Removes the item pointed to by the iterator \a pos from the
1145 list, and returns an iterator to the next item in the list
1146 (which may be end()).
1148 \include qlist.qdoc shrinking-erase
1149 \include qlist.qdoc iterator-invalidation-erase
1151 \sa insert(), remove()
1154/*! \fn template <typename T> QList<T>::iterator QList<T>::erase(const_iterator begin, const_iterator end)
1158 Removes all the items from \a begin up to (but not including) \a
1159 end. Returns an iterator to the same item that \a end referred to
1162 \include qlist.qdoc shrinking-erase
1163 \include qlist.qdoc iterator-invalidation-erase
1166/*! \fn template <typename T> T& QList<T>::first()
1168 Returns a reference to the first item in the list. This
1169 function assumes that the list isn't empty.
1171 \sa last(), isEmpty(), constFirst()
1174/*! \fn template <typename T> const T& QList<T>::first() const
1179/*! \fn template <typename T> const T& QList<T>::constFirst() const
1182 Returns a const reference to the first item in the list. This
1183 function assumes that the list isn't empty.
1185 \sa constLast(), isEmpty(), first()
1188/*! \fn template <typename T> T& QList<T>::last()
1190 Returns a reference to the last item in the list. This function
1191 assumes that the list isn't empty.
1193 \sa first(), isEmpty(), constLast()
1196/*! \fn template <typename T> const T& QList<T>::last() const
1201/*! \fn template <typename T> const T& QList<T>::constLast() const
1204 Returns a const reference to the last item in the list. This function
1205 assumes that the list isn't empty.
1207 \sa constFirst(), isEmpty(), last()
1210/*! \fn template <typename T> T QList<T>::value(qsizetype i) const
1212 Returns the value at index position \a i in the list.
1214 If the index \a i is out of bounds, the function returns a
1215 \l{default-constructed value}. If you are certain that \a i is within
1216 bounds, you can use at() instead, which is slightly faster.
1218 \sa at(), operator[]()
1221/*! \fn template <typename T> T QList<T>::value(qsizetype i, parameter_type defaultValue) const
1225 If the index \a i is out of bounds, the function returns \a defaultValue.
1228/*! \fn template <typename T> void QList<T>::push_back(parameter_type value)
1230 This function is provided for STL compatibility. It is equivalent
1231 to append(\a value).
1234/*! \fn template <typename T> void QList<T>::push_back(rvalue_ref value)
1240 \fn template <typename T> void QList<T>::push_front(parameter_type value)
1241 \fn template <typename T> void QList<T>::push_front(rvalue_ref value)
1243 This function is provided for STL compatibility. It is equivalent
1244 to prepend(\a value).
1247/*! \fn template <typename T> void QList<T>::pop_front()
1249 This function is provided for STL compatibility. It is equivalent
1253/*! \fn template <typename T> void QList<T>::pop_back()
1255 This function is provided for STL compatibility. It is equivalent
1259/*! \fn template <typename T> T& QList<T>::front()
1261 This function is provided for STL compatibility. It is equivalent
1265/*! \fn template <typename T> QList<T>::const_reference QList<T>::front() const
1270/*! \fn template <typename T> QList<T>::reference QList<T>::back()
1272 This function is provided for STL compatibility. It is equivalent
1276/*! \fn template <typename T> QList<T>::const_reference QList<T>::back() const
1281/*! \fn template <typename T> void QList<T>::shrink_to_fit()
1284 This function is provided for STL compatibility. It is equivalent
1288/*! \fn template <typename T> bool QList<T>::empty() const
1290 This function is provided for STL compatibility. It is equivalent
1291 to isEmpty(), returning \c true if the list is empty; otherwise
1295/*! \fn template <typename T> QList<T> &QList<T>::operator+=(const QList<T> &other)
1297 Appends the items of the \a other list to this list and
1298 returns a reference to this list.
1300 \sa operator+(), append()
1303/*! \fn template <typename T> QList<T> &QList<T>::operator+=(QList<T> &&other)
1308 \sa operator+(), append()
1311/*! \fn template <typename T> void QList<T>::operator+=(parameter_type value)
1315 Appends \a value to the list.
1317 \sa append(), operator<<()
1320/*! \fn template <typename T> void QList<T>::operator+=(rvalue_ref value)
1325 \sa append(), operator<<()
1329 \fn template <typename T> QList<T> QList<T>::operator+(const QList<T> &other) const &
1330 \fn template <typename T> QList<T> QList<T>::operator+(const QList<T> &other) &&
1331 \fn template <typename T> QList<T> QList<T>::operator+(QList<T> &&other) const &
1332 \fn template <typename T> QList<T> QList<T>::operator+(QList<T> &&other) &&
1334 Returns a list that contains all the items in this list
1335 followed by all the items in the \a other list.
1340/*! \fn template <typename T> QList<T> &QList<T>::operator<<(parameter_type value)
1342 Appends \a value to the list and returns a reference to this list.
1344 \sa append(), operator+=()
1347/*! \fn template <typename T> QList<T> &QList<T>::operator<<(rvalue_ref value)
1352 \sa append(), operator+=()
1356/*! \fn template <typename T> QList<T> &QList<T>::operator<<(const QList<T> &other)
1358 Appends \a other to the list and returns a reference to the list.
1361/*! \fn template <typename T> QList<T> &QList<T>::operator<<(QList<T> &&other)
1367/*! \class QList::iterator
1369 \brief Provides an STL-style non-const iterator for QList and QStack.
1371 QList provides both \l{STL-style iterators} and \l{Java-style
1374//! [iterator-invalidation-class-desc]
1375 \warning Iterators on implicitly shared containers do not work
1376 exactly like STL-iterators. You should avoid copying a container
1377 while iterators are active on that container. For more information,
1378 read \l{Implicit sharing iterator problem}.
1380 \warning Iterators are invalidated when QList is modified. Consider that all
1381 iterators are invalidated by default. Exceptions to this rule are explicitly
1383//! [iterator-invalidation-class-desc]
1385 \sa QList::begin(), QList::end(), QList::const_iterator, QMutableListIterator
1388/*! \class QList::const_iterator
1390 \brief Provides an STL-style const iterator for QList and QStack.
1392 QList provides both \l{STL-style iterators} and \l{Java-style
1395 \include qlist.qdoc iterator-invalidation-class-desc
1397 \sa QList::constBegin(), QList::constEnd(), QList::iterator, QListIterator
1400/*! \typedef QList::reverse_iterator
1403 The QList::reverse_iterator typedef provides an STL-style non-const
1404 reverse iterator for QList.
1406 \include qlist.qdoc iterator-invalidation-class-desc
1408 \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator
1411/*! \typedef QList::const_reverse_iterator
1414 The QList::const_reverse_iterator typedef provides an STL-style const
1415 reverse iterator for QList.
1417 \include qlist.qdoc iterator-invalidation-class-desc
1419 \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator
1422/*! \typedef QList::Iterator
1424 Qt-style synonym for QList::iterator.
1427/*! \typedef QList::ConstIterator
1429 Qt-style synonym for QList::const_iterator.
1432/*! \typedef QList::const_pointer
1434 Provided for STL compatibility.
1437/*! \typedef QList::const_reference
1439 Provided for STL compatibility.
1442/*! \typedef QList::difference_type
1444 Provided for STL compatibility.
1447/*! \typedef QList::pointer
1449 Provided for STL compatibility.
1452/*! \typedef QList::reference
1454 Provided for STL compatibility.
1457/*! \typedef QList::size_type
1459 Provided for STL compatibility.
1462/*! \typedef QList::value_type
1464 Provided for STL compatibility.
1467/*! \typedef QList::parameter_type
1471/*! \typedef QList::rvalue_ref
1475/*! \fn template <typename T> QList<T> QList<T>::toList() const
1476 \fn template <typename T> QList<T> QList<T>::toVector() const
1479 A no-op in Qt 6. Provided for backwards compatibility with
1480 Qt 5, where QList and QVector where two different types.
1485/*! \fn template <typename T> QList<T> QList<T>::fromList(const QList<T> &list)
1486 \fn template <typename T> QList<T> QList<T>::fromVector(const QList<T> &list)
1489 A no-op in Qt 6. Provided for backwards compatibility with
1490 Qt 5, where QList and QVector were two different types.
1495/*! \fn template <typename T> QDataStream &operator<<(QDataStream &out, const QList<T> &list)
1498 Writes the list \a list to stream \a out.
1500 This function requires the value type to implement \c operator<<().
1502 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1505/*! \fn template <typename T> QDataStream &operator>>(QDataStream &in, QList<T> &list)
1508 Reads a list from stream \a in into \a list.
1510 This function requires the value type to implement \c operator>>().
1512 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1515/*! \fn template <typename T, typename AT> qsizetype erase(QList<T> &list, const AT &t)
1519 Removes all elements that compare equal to \a t from the
1520 list \a list. Returns the number of elements removed, if any.
1522 \note Unlike QList::removeAll, \a t is not allowed to be a
1523 reference to an element inside \a list. If you cannot be sure that
1524 this is not the case, take a copy of \a t and call this function
1527 \sa QList::removeAll(), erase_if
1530/*! \fn template <typename T, typename Predicate> qsizetype erase_if(QList<T> &list, Predicate pred)
1534 Removes all elements for which the predicate \a pred returns true
1535 from the list \a list. Returns the number of elements removed, if
1541/*! \fn template <typename T> QList<T>::assign(qsizetype n, parameter_type t)
1544 Replaces the contents of this list with \a n copies of \a t.
1546 The size of this list will be equal to \a n.
1548 This function will only allocate memory if \a n exceeds the capacity of the
1549 list or this list is shared.
1552/*! \fn template <typename T> template <typename InputIterator, if_input_iterator<InputIterator>> QList<T>::assign(InputIterator first, InputIterator last)
1555 Replaces the contents of this list with a copy of the elements in the
1556 iterator range [\a first, \a last).
1558 The size of this list will be equal to the number of elements in the
1559 range [\a first, \a last).
1561 This function will only allocate memory if the number of elements in the
1562 range exceeds the capacity of this list or this list is shared.
1564 \note This function overload only participates in overload resolution if
1565 \c InputIterator meets the requirements of a
1566 \l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator}.
1568 \note The behavior is undefined if either argument is an iterator into
1572/*! \fn template <typename T> QList<T>::assign(std::initializer_list<T> l)
1575 Replaces the contents of this list with a copy of the elements of
1578 The size of this list will be equal to the number of elements in
1581 This function only allocates memory if the number of elements in \a l
1582 exceeds the capacity of this list or this list is shared.