Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qlist.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \class QVector
6 \inmodule QtCore
7 \brief QVector is an alias for QList.
8
9 Please see the QList documentation for details.
10*/
11
12/*!
13 \class QList
14 \inmodule QtCore
15 \brief The QList class is a template class that provides a dynamic array.
16
17 \ingroup tools
18 \ingroup shared
19
20 \reentrant
21
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.
26
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:
30
31 \list
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.
39 \endlist
40
41 \note QList and QVarLengthArray both guarantee C-compatible
42 array layout.
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.
47
48 Here's an example of a QList that stores integers and a QList
49 that stores QString values:
50
51 \snippet code/src_corelib_tools_qlist.cpp 0
52
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:
56
57 \snippet code/src_corelib_tools_qlist.cpp 1
58
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:
63
64 \snippet code/src_corelib_tools_qlist.cpp 2
65
66 You can also call fill() at any time to fill the list with a
67 value.
68
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:
73
74 \snippet code/src_corelib_tools_qlist.cpp 3
75
76 For read-only access, an alternative syntax is to use at():
77
78 \snippet code/src_corelib_tools_qlist.cpp 4
79
80 at() can be faster than operator[](), because it never causes a
81 \l{deep copy} to occur.
82
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++
88 array.
89
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:
95
96 \snippet code/src_corelib_tools_qlist.cpp 5
97
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().
101
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.
108
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.
114
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.
120
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}.
123
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
130 per-function basis.
131
132 For iterating over the items, see \l {Iterating over Containers}.
133
134 In addition to QList, Qt also provides QVarLengthArray, a very
135 low-level class with little functionality that is optimized for
136 speed.
137
138 \section2 More Information on Using Qt Containers
139
140 For a detailed discussion comparing Qt containers with each other and
141 with STL containers, see \l {Understand the Qt Containers}.
142
143 \section1 Maximum size and out-of-memory conditions
144
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.
152
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.
157
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.
162*/
163
164/*!
165 \fn template <typename T> QList<T> QList<T>::mid(qsizetype pos, qsizetype length = -1) const
166
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)
171 are included.
172*/
173
174/*!
175 \fn template <typename T> QList<T> QList<T>::first(qsizetype n) const
176 \since 6.0
177
178 Returns a sub-list that contains the first \a n elements
179 of this list.
180
181 \note The behavior is undefined when \a n < 0 or \a n > size().
182
183 \sa last(), sliced()
184*/
185
186/*!
187 \fn template <typename T> QList<T> QList<T>::last(qsizetype n) const
188 \since 6.0
189
190 Returns a sub-list that contains the last \a n elements of this list.
191
192 \note The behavior is undefined when \a n < 0 or \a n > size().
193
194 \sa first(), sliced()
195*/
196
197/*!
198 \fn template <typename T> QList<T> QList<T>::sliced(qsizetype pos, qsizetype n) const
199 \since 6.0
200
201 Returns a sub-list that contains \a n elements of this list,
202 starting at position \a pos.
203
204 \note The behavior is undefined when \a pos < 0, \a n < 0,
205 or \a pos + \a n > size().
206
207 \sa first(), last()
208*/
209
210/*!
211 \fn template <typename T> QList<T> QList<T>::sliced(qsizetype pos) const
212 \since 6.0
213 \overload
214
215 Returns a sub-list that contains the elements of this list starting at
216 position \a pos and extending to its end.
217
218 \note The behavior is undefined when \a pos < 0 or \a pos > size().
219
220 \sa first(), last()
221*/
222
223
224/*! \fn template <typename T> QList<T>::QList()
225
226 Constructs an empty list.
227
228 \sa resize()
229*/
230
231/*!
232 \fn template <typename T> QList<T>::QList(QList<T> &&other)
233
234 Move-constructs a QList instance, making it point at the same
235 object that \a other was pointing to.
236
237 \since 5.2
238*/
239
240/*! \fn template <typename T> QList<T>::QList(qsizetype size)
241
242 Constructs a list with an initial size of \a size elements.
243
244 The elements are initialized with a \l{default-constructed
245 value}.
246
247 \sa resize()
248*/
249
250/*! \fn template <typename T> QList<T>::QList(qsizetype size, parameter_type value)
251
252 Constructs a list with an initial size of \a size elements.
253 Each element is initialized with \a value.
254
255 \sa resize(), fill()
256*/
257
258/*! \fn template <typename T> QList<T>::QList(const QList<T> &other)
259
260 Constructs a copy of \a other.
261
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}.
267
268 \sa operator=()
269*/
270
271/*! \fn template <typename T> QList<T>::QList(std::initializer_list<T> args)
272 \since 4.8
273
274 Constructs a list from the std::initializer_list given by \a args.
275*/
276
277/*! \fn template <typename T> template<typename InputIterator, if_input_iterator<InputIterator>> QList<T>::QList(InputIterator first, InputIterator last)
278 \since 5.14
279
280 Constructs a list with the contents in the iterator range [\a first, \a last).
281
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}.
285
286 The value type of \c InputIterator must be convertible to \c T.
287*/
288
289/*! \fn template <typename T> QList<T>::~QList()
290
291 Destroys the list.
292*/
293
294/*! \fn template <typename T> QList<T> &QList<T>::operator=(const QList<T> &other)
295
296 Assigns \a other to this list and returns a reference to this
297 list.
298*/
299
300/*!
301 \fn template <typename T> QList<T> &QList<T>::operator=(QList<T> &&other)
302
303 Move-assigns \a other to this QList instance.
304
305 \since 5.2
306*/
307
308/*!
309 \fn template <typename T> QList<T> &QList<T>::operator=(std::initializer_list<T> args)
310 \since 5.14
311
312 Assigns the collection of values in \a args to this QList instance.
313*/
314
315/*! \fn template <typename T> void QList<T>::swap(QList<T> &other)
316 \since 4.8
317
318 Swaps list \a other with this list. This operation is very fast and
319 never fails.
320*/
321
322/*! \fn template <typename T> void QList<T>::swapItemsAt(qsizetype i, qsizetype j)
323
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().
328*/
329
330
331/*! \fn template <typename T> bool QList<T>::operator==(const QList<T> &other) const
332
333 Returns \c true if \a other is equal to this list; otherwise
334 returns \c false.
335
336 Two lists are considered equal if they contain the same values
337 in the same order.
338
339 This function requires the value type to have an implementation
340 of \c operator==().
341
342 \sa operator!=()
343*/
344
345/*! \fn template <typename T> bool QList<T>::operator!=(const QList<T> &other) const
346
347 Returns \c true if \a other is not equal to this list; otherwise
348 returns \c false.
349
350 Two lists are considered equal if they contain the same values
351 in the same order.
352
353 This function requires the value type to have an implementation
354 of \c operator==().
355
356 \sa operator==()
357*/
358
359/*! \fn template <typename T> bool QList<T>::operator<(const QList<T> &other) const
360 \since 5.6
361
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.
365
366 This function requires the value type to have an implementation
367 of \c operator<().
368*/
369
370/*! \fn template <typename T> bool QList<T>::operator<=(const QList<T> &other) const
371 \since 5.6
372
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.
376
377 This function requires the value type to have an implementation
378 of \c operator<().
379*/
380
381/*! \fn template <typename T> bool QList<T>::operator>(const QList<T> &other) const
382 \since 5.6
383
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.
387
388 This function requires the value type to have an implementation
389 of \c operator<().
390*/
391
392/*! \fn template <typename T> bool QList<T>::operator>=(const QList<T> &other) const
393 \since 5.6
394
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.
398
399 This function requires the value type to have an implementation
400 of \c operator<().
401*/
402
403/*!
404 \fn template <typename T> size_t qHash(const QList<T> &key, size_t seed = 0)
405 \since 5.6
406 \relates QList
407
408 Returns the hash value for \a key,
409 using \a seed to seed the calculation.
410
411 This function requires qHash() to be overloaded for the value type \c T.
412*/
413
414/*! \fn template <typename T> qsizetype QList<T>::size() const
415
416 Returns the number of items in the list.
417
418 \sa isEmpty(), resize()
419*/
420
421/*! \fn template <typename T> bool QList<T>::isEmpty() const
422
423 Returns \c true if the list has size 0; otherwise returns \c false.
424
425 \sa size(), resize()
426*/
427
428/*! \fn template <typename T> void QList<T>::resize(qsizetype size)
429 \since 6.0
430
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.
435
436 If this list is not shared, the capacity() is preserved. Use squeeze()
437 to shed excess capacity.
438
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.
442
443 \sa size()
444*/
445
446/*! \fn template <typename T> qsizetype QList<T>::capacity() const
447
448 Returns the maximum number of items that can be stored in the
449 list without forcing a reallocation.
450
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().
455
456 \note a statically allocated list will report a capacity of 0,
457 even if it's not empty.
458
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.
463
464 \sa reserve(), squeeze()
465*/
466
467/*! \fn template <typename T> void QList<T>::reserve(qsizetype size)
468
469 Attempts to allocate memory for at least \a size elements.
470
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.
474
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.
481
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().
486
487 \sa squeeze(), capacity(), resize()
488*/
489
490/*! \fn template <typename T> void QList<T>::squeeze()
491
492 Releases any memory not required to store the items.
493
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.
497
498 \sa reserve(), capacity()
499*/
500
501/*! \fn template <typename T> void QList<T>::detach()
502
503 \internal
504*/
505
506/*! \fn template <typename T> bool QList<T>::isDetached() const
507
508 \internal
509*/
510
511/*! \fn template <typename T> void QList<T>::setSharable(bool sharable)
512
513 \internal
514*/
515
516/*! \fn template <typename T> bool QList<T>::isSharedWith(const QList<T> &other) const
517
518 \internal
519*/
520
521/*! \fn template <typename T> T *QList<T>::data()
522
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.
525
526 Example:
527 \snippet code/src_corelib_tools_qlist.cpp 6
528
529 \warning The pointer is invalidated on detachment or when the QList is
530 modified.
531
532 This function is mostly useful to pass a list to a function
533 that accepts a plain C++ array.
534
535 \sa constData(), operator[]()
536*/
537
538/*! \fn template <typename T> const T *QList<T>::data() const
539
540 \overload
541*/
542
543/*! \fn template <typename T> const T *QList<T>::constData() const
544
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.
547
548 \warning The pointer is invalidated on detachment or when the QList is
549 modified.
550
551 This function is mostly useful to pass a list to a function
552 that accepts a plain C++ array.
553
554 \sa data(), operator[]()
555*/
556
557/*! \fn template <typename T> void QList<T>::clear()
558
559 Removes all the elements from the list.
560
561 If this list is not shared, the capacity() is preserved. Use squeeze() to
562 shed excess capacity.
563
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
566 capacity.
567
568 \sa resize(), squeeze()
569*/
570
571/*! \fn template <typename T> const T &QList<T>::at(qsizetype i) const
572
573 Returns the item at index position \a i in the list.
574
575 \a i must be a valid index position in the list (i.e., 0 <= \a
576 i < size()).
577
578 \sa value(), operator[]()
579*/
580
581/*! \fn template <typename T> T &QList<T>::operator[](qsizetype i)
582
583 Returns the item at index position \a i as a modifiable reference.
584
585 \a i must be a valid index position in the list (i.e., 0 <= \a i
586 < size()).
587
588 Note that using non-const operators can cause QList to do a deep
589 copy.
590
591 \sa at(), value()
592*/
593
594/*! \fn template <typename T> const T &QList<T>::operator[](qsizetype i) const
595
596 \overload
597
598 Same as at(\a i).
599*/
600
601/*!
602 \fn template <typename T> void QList<T>::append(parameter_type value)
603
604 Inserts \a value at the end of the list.
605
606 Example:
607 \snippet code/src_corelib_tools_qlist.cpp 7
608
609 This is the same as calling resize(size() + 1) and assigning \a
610 value to the new last element in the list.
611
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.
615
616 \sa operator<<(), prepend(), insert()
617*/
618
619/*!
620 \fn template <typename T> void QList<T>::append(rvalue_ref value)
621 \since 5.6
622
623 \overload
624
625 Example:
626 \snippet code/src_corelib_tools_qlist.cpp move-append
627*/
628
629/*! \fn template <typename T> void QList<T>::append(const QList<T> &value)
630
631 \overload
632
633 \since 5.5
634
635 Appends the items of the \a value list to this list.
636
637 \sa operator<<(), operator+=()
638*/
639
640/*! \fn template <typename T> void QList<T>::append(QList<T> &&value)
641 \overload
642
643 \since 6.0
644
645 Moves the items of the \a value list to the end of this list.
646
647 \sa operator<<(), operator+=()
648*/
649
650/*!
651 \fn template <typename T> void QList<T>::prepend(parameter_type value)
652 \fn template <typename T> void QList<T>::prepend(rvalue_ref value)
653
654 Inserts \a value at the beginning of the list.
655
656 Example:
657 \snippet code/src_corelib_tools_qlist.cpp 8
658
659 This is the same as list.insert(0, \a value).
660
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.
667
668 \sa append(), insert()
669*/
670
671/*!
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)
674
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
677 construction.
678
679 Returns a reference to the new element.
680
681 Example:
682 \snippet code/src_corelib_tools_qlist.cpp emplace-back
683
684 It is also possible to access a newly created object by using
685 returned reference:
686 \snippet code/src_corelib_tools_qlist.cpp emplace-back-ref
687
688 This is the same as list.emplace(list.size(), \a args).
689
690 \sa emplace
691*/
692
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)
695
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.
699
700 Example:
701 \snippet code/src_corelib_tools_qlist.cpp 9
702
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
707 instead.
708
709 \sa append(), prepend(), remove()
710*/
711
712/*! \fn template <typename T> void QList<T>::insert(qsizetype i, qsizetype count, parameter_type value)
713
714 \overload
715
716 Inserts \a count copies of \a value at index position \a i in the
717 list.
718
719 Example:
720 \snippet code/src_corelib_tools_qlist.cpp 10
721*/
722
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)
725
726 \overload
727
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.
730*/
731
732/*! \fn template <typename T> QList<T>::iterator QList<T>::insert(const_iterator before, qsizetype count, parameter_type value)
733
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.
737*/
738
739/*!
740 \fn template <typename T> template <typename ...Args> QList<T>::iterator QList<T>::emplace(qsizetype i, Args&&... args)
741
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.
745
746 Returns an iterator to the new element.
747
748 Example:
749 \snippet code/src_corelib_tools_qlist.cpp emplace
750
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.
754
755 \sa emplaceBack
756*/
757
758
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)
761
762 Replaces the item at index position \a i with \a value.
763
764 \a i must be a valid index position in the list (i.e., 0 <= \a
765 i < size()).
766
767 \sa operator[](), remove()
768*/
769
770/*! \fn template <typename T> void QList<T>::remove(qsizetype i, qsizetype n = 1)
771
772 Removes \a n elements from the list, starting at index position \a i.
773
774//! [shrinking-erase]
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,
777 call squeeze().
778//! [shrinking-erase]
779
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]
784
785 \sa insert(), replace(), fill()
786*/
787
788/*! \fn template <typename T> void QList<T>::removeAt(qsizetype i)
789 \since 5.2
790
791 Removes the element at index position \a i.
792 Equivalent to
793 \code
794 remove(i);
795 \endcode
796
797 \include qlist.qdoc shrinking-erase
798 \include qlist.qdoc iterator-invalidation-erase
799
800 \sa remove()
801*/
802
803/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::removeAll(const AT &t)
804 \since 5.4
805
806 Removes all elements that compare equal to \a t from the
807 list. Returns the number of elements removed, if any.
808
809 \include qlist.qdoc shrinking-erase
810
811 \sa removeOne()
812*/
813
814/*! \fn template <typename T> template <typename AT = T> bool QList<T>::removeOne(const AT &t)
815 \since 5.4
816
817 Removes the first element that compares equal to \a t from the
818 list. Returns whether an element was, in fact, removed.
819
820 \include qlist.qdoc shrinking-erase
821
822 \sa removeAll()
823*/
824
825/*! \fn template <typename T> template <typename Predicate> qsizetype QList<T>::removeIf(Predicate pred)
826 \since 6.1
827
828 Removes all elements for which the predicate \a pred returns true
829 from the list. Returns the number of elements removed, if any.
830
831 \sa removeAll()
832*/
833
834/*! \fn template <typename T> qsizetype QList<T>::length() const
835 \since 5.2
836
837 Same as size() and count().
838
839 \sa size(), count()
840*/
841
842/*! \fn template <typename T> T QList<T>::takeAt(qsizetype i)
843 \since 5.2
844
845 Removes the element at index position \a i and returns it.
846
847 Equivalent to
848 \code
849 T t = at(i);
850 remove(i);
851 return t;
852 \endcode
853
854 \include qlist.qdoc iterator-invalidation-erase
855
856 \sa takeFirst(), takeLast()
857*/
858
859/*! \fn template <typename T> void QList<T>::move(qsizetype from, qsizetype to)
860 \since 5.6
861
862 Moves the item at index position \a from to index position \a to.
863*/
864
865/*! \fn template <typename T> void QList<T>::removeFirst()
866 \since 5.1
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
870 function.
871
872 \include qlist.qdoc shrinking-erase
873
874 \sa remove(), takeFirst(), isEmpty()
875*/
876
877/*! \fn template <typename T> void QList<T>::removeLast()
878 \since 5.1
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
882 this function.
883
884 \include qlist.qdoc shrinking-erase
885
886 \sa remove(), takeLast(), removeFirst(), isEmpty()
887*/
888
889/*! \fn template <typename T> T QList<T>::takeFirst()
890 \since 5.1
891
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.
895
896 \sa takeLast(), removeFirst()
897*/
898
899/*! \fn template <typename T> T QList<T>::takeLast()
900 \since 5.1
901
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.
905
906 If you don't use the return value, removeLast() is more
907 efficient.
908
909 \sa takeFirst(), removeLast()
910*/
911
912/*!
913 \fn template <typename T> template <typename ...Args> QList<T>::iterator QList<T>::emplace(const_iterator before, Args&&... args)
914
915 \overload
916
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.
920
921 Returns an iterator to the new element.
922*/
923
924/*! \fn template <typename T> QList<T> &QList<T>::fill(parameter_type value, qsizetype size = -1)
925
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.
928
929 Example:
930 \snippet code/src_corelib_tools_qlist.cpp 11
931
932 \sa resize()
933*/
934
935/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::indexOf(const AT &value, qsizetype from = 0) const
936
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.
940
941 Example:
942 \snippet code/src_corelib_tools_qlist.cpp 12
943
944 This function requires the value type to have an implementation of
945 \c operator==().
946
947 \sa lastIndexOf(), contains()
948*/
949
950/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::lastIndexOf(const AT &value, qsizetype from = -1) const
951
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.
956
957 Example:
958 \snippet code/src_corelib_tools_qlist.cpp 13
959
960 This function requires the value type to have an implementation of
961 \c operator==().
962
963 \sa indexOf()
964*/
965
966/*! \fn template <typename T> template <typename AT = T> bool QList<T>::contains(const AT &value) const
967
968 Returns \c true if the list contains an occurrence of \a value;
969 otherwise returns \c false.
970
971 This function requires the value type to have an implementation of
972 \c operator==().
973
974 \sa indexOf(), count()
975*/
976
977/*! \fn template <typename T> bool QList<T>::startsWith(parameter_type value) const
978 \since 4.5
979
980 Returns \c true if this list is not empty and its first
981 item is equal to \a value; otherwise returns \c false.
982
983 \sa isEmpty(), first()
984*/
985
986/*! \fn template <typename T> bool QList<T>::endsWith(parameter_type value) const
987 \since 4.5
988
989 Returns \c true if this list is not empty and its last
990 item is equal to \a value; otherwise returns \c false.
991
992 \sa isEmpty(), last()
993*/
994
995
996/*! \fn template <typename T> template <typename AT = T> qsizetype QList<T>::count(const AT &value) const
997
998 Returns the number of occurrences of \a value in the list.
999
1000 This function requires the value type to have an implementation of
1001 \c operator==().
1002
1003 \sa contains(), indexOf()
1004*/
1005
1006/*! \fn template <typename T> qsizetype QList<T>::count() const
1007
1008 \overload
1009
1010 Same as size().
1011*/
1012
1013/*! \fn template <typename T> QList<T>::iterator QList<T>::begin()
1014
1015 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the
1016 first item in the list.
1017
1018//! [iterator-invalidation-func-desc]
1019 \warning The returned iterator is invalidated on detachment or when the
1020 QList is modified.
1021//! [iterator-invalidation-func-desc]
1022
1023 \sa constBegin(), end()
1024*/
1025
1026/*! \fn template <typename T> QList<T>::const_iterator QList<T>::begin() const
1027
1028 \overload
1029*/
1030
1031/*! \fn template <typename T> QList<T>::const_iterator QList<T>::cbegin() const
1032 \since 5.0
1033
1034 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the
1035 first item in the list.
1036
1037 \include qlist.qdoc iterator-invalidation-func-desc
1038
1039 \sa begin(), cend()
1040*/
1041
1042/*! \fn template <typename T> QList<T>::const_iterator QList<T>::constBegin() const
1043
1044 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the
1045 first item in the list.
1046
1047 \include qlist.qdoc iterator-invalidation-func-desc
1048
1049 \sa begin(), constEnd()
1050*/
1051
1052/*! \fn template <typename T> QList<T>::iterator QList<T>::end()
1053
1054 Returns an \l{STL-style iterators}{STL-style iterator} pointing just after
1055 the last item in the list.
1056
1057 \include qlist.qdoc iterator-invalidation-func-desc
1058
1059 \sa begin(), constEnd()
1060*/
1061
1062/*! \fn template <typename T> QList<T>::const_iterator QList<T>::end() const
1063
1064 \overload
1065*/
1066
1067/*! \fn template <typename T> QList<T>::const_iterator QList<T>::cend() const
1068 \since 5.0
1069
1070 Returns a const \l{STL-style iterators}{STL-style iterator} pointing just
1071 after the last item in the list.
1072
1073 \include qlist.qdoc iterator-invalidation-func-desc
1074
1075 \sa cbegin(), end()
1076*/
1077
1078/*! \fn template <typename T> QList<T>::const_iterator QList<T>::constEnd() const
1079
1080 Returns a const \l{STL-style iterators}{STL-style iterator} pointing just
1081 after the last item in the list.
1082
1083 \include qlist.qdoc iterator-invalidation-func-desc
1084
1085 \sa constBegin(), end()
1086*/
1087
1088/*! \fn template <typename T> QList<T>::reverse_iterator QList<T>::rbegin()
1089 \since 5.6
1090
1091 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to
1092 the first item in the list, in reverse order.
1093
1094 \include qlist.qdoc iterator-invalidation-func-desc
1095
1096 \sa begin(), crbegin(), rend()
1097*/
1098
1099/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::rbegin() const
1100 \since 5.6
1101 \overload
1102*/
1103
1104/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::crbegin() const
1105 \since 5.6
1106
1107 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing
1108 to the first item in the list, in reverse order.
1109
1110 \include qlist.qdoc iterator-invalidation-func-desc
1111
1112 \sa begin(), rbegin(), rend()
1113*/
1114
1115/*! \fn template <typename T> QList<T>::reverse_iterator QList<T>::rend()
1116 \since 5.6
1117
1118 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing just
1119 after the last item in the list, in reverse order.
1120
1121 \include qlist.qdoc iterator-invalidation-func-desc
1122
1123 \sa end(), crend(), rbegin()
1124*/
1125
1126/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::rend() const
1127 \since 5.6
1128 \overload
1129*/
1130
1131/*! \fn template <typename T> QList<T>::const_reverse_iterator QList<T>::crend() const
1132 \since 5.6
1133
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.
1136
1137 \include qlist.qdoc iterator-invalidation-func-desc
1138
1139 \sa end(), rend(), rbegin()
1140*/
1141
1142/*! \fn template <typename T> QList<T>::iterator QList<T>::erase(const_iterator pos)
1143
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()).
1147
1148 \include qlist.qdoc shrinking-erase
1149 \include qlist.qdoc iterator-invalidation-erase
1150
1151 \sa insert(), remove()
1152*/
1153
1154/*! \fn template <typename T> QList<T>::iterator QList<T>::erase(const_iterator begin, const_iterator end)
1155
1156 \overload
1157
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
1160 before the call.
1161
1162 \include qlist.qdoc shrinking-erase
1163 \include qlist.qdoc iterator-invalidation-erase
1164*/
1165
1166/*! \fn template <typename T> T& QList<T>::first()
1167
1168 Returns a reference to the first item in the list. This
1169 function assumes that the list isn't empty.
1170
1171 \sa last(), isEmpty(), constFirst()
1172*/
1173
1174/*! \fn template <typename T> const T& QList<T>::first() const
1175
1176 \overload
1177*/
1178
1179/*! \fn template <typename T> const T& QList<T>::constFirst() const
1180 \since 5.6
1181
1182 Returns a const reference to the first item in the list. This
1183 function assumes that the list isn't empty.
1184
1185 \sa constLast(), isEmpty(), first()
1186*/
1187
1188/*! \fn template <typename T> T& QList<T>::last()
1189
1190 Returns a reference to the last item in the list. This function
1191 assumes that the list isn't empty.
1192
1193 \sa first(), isEmpty(), constLast()
1194*/
1195
1196/*! \fn template <typename T> const T& QList<T>::last() const
1197
1198 \overload
1199*/
1200
1201/*! \fn template <typename T> const T& QList<T>::constLast() const
1202 \since 5.6
1203
1204 Returns a const reference to the last item in the list. This function
1205 assumes that the list isn't empty.
1206
1207 \sa constFirst(), isEmpty(), last()
1208*/
1209
1210/*! \fn template <typename T> T QList<T>::value(qsizetype i) const
1211
1212 Returns the value at index position \a i in the list.
1213
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.
1217
1218 \sa at(), operator[]()
1219*/
1220
1221/*! \fn template <typename T> T QList<T>::value(qsizetype i, parameter_type defaultValue) const
1222
1223 \overload
1224
1225 If the index \a i is out of bounds, the function returns \a defaultValue.
1226*/
1227
1228/*! \fn template <typename T> void QList<T>::push_back(parameter_type value)
1229
1230 This function is provided for STL compatibility. It is equivalent
1231 to append(\a value).
1232*/
1233
1234/*! \fn template <typename T> void QList<T>::push_back(rvalue_ref value)
1235 \since 5.6
1236 \overload
1237*/
1238
1239/*!
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)
1242
1243 This function is provided for STL compatibility. It is equivalent
1244 to prepend(\a value).
1245*/
1246
1247/*! \fn template <typename T> void QList<T>::pop_front()
1248
1249 This function is provided for STL compatibility. It is equivalent
1250 to removeFirst().
1251*/
1252
1253/*! \fn template <typename T> void QList<T>::pop_back()
1254
1255 This function is provided for STL compatibility. It is equivalent
1256 to removeLast().
1257*/
1258
1259/*! \fn template <typename T> T& QList<T>::front()
1260
1261 This function is provided for STL compatibility. It is equivalent
1262 to first().
1263*/
1264
1265/*! \fn template <typename T> QList<T>::const_reference QList<T>::front() const
1266
1267 \overload
1268*/
1269
1270/*! \fn template <typename T> QList<T>::reference QList<T>::back()
1271
1272 This function is provided for STL compatibility. It is equivalent
1273 to last().
1274*/
1275
1276/*! \fn template <typename T> QList<T>::const_reference QList<T>::back() const
1277
1278 \overload
1279*/
1280
1281/*! \fn template <typename T> void QList<T>::shrink_to_fit()
1282 \since 5.10
1283
1284 This function is provided for STL compatibility. It is equivalent
1285 to squeeze().
1286*/
1287
1288/*! \fn template <typename T> bool QList<T>::empty() const
1289
1290 This function is provided for STL compatibility. It is equivalent
1291 to isEmpty(), returning \c true if the list is empty; otherwise
1292 returns \c false.
1293*/
1294
1295/*! \fn template <typename T> QList<T> &QList<T>::operator+=(const QList<T> &other)
1296
1297 Appends the items of the \a other list to this list and
1298 returns a reference to this list.
1299
1300 \sa operator+(), append()
1301*/
1302
1303/*! \fn template <typename T> QList<T> &QList<T>::operator+=(QList<T> &&other)
1304 \since 6.0
1305
1306 \overload
1307
1308 \sa operator+(), append()
1309*/
1310
1311/*! \fn template <typename T> void QList<T>::operator+=(parameter_type value)
1312
1313 \overload
1314
1315 Appends \a value to the list.
1316
1317 \sa append(), operator<<()
1318*/
1319
1320/*! \fn template <typename T> void QList<T>::operator+=(rvalue_ref value)
1321 \since 5.11
1322
1323 \overload
1324
1325 \sa append(), operator<<()
1326*/
1327
1328/*!
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) &&
1333
1334 Returns a list that contains all the items in this list
1335 followed by all the items in the \a other list.
1336
1337 \sa operator+=()
1338*/
1339
1340/*! \fn template <typename T> QList<T> &QList<T>::operator<<(parameter_type value)
1341
1342 Appends \a value to the list and returns a reference to this list.
1343
1344 \sa append(), operator+=()
1345*/
1346
1347/*! \fn template <typename T> QList<T> &QList<T>::operator<<(rvalue_ref value)
1348 \since 5.11
1349
1350 \overload
1351
1352 \sa append(), operator+=()
1353*/
1354
1355
1356/*! \fn template <typename T> QList<T> &QList<T>::operator<<(const QList<T> &other)
1357
1358 Appends \a other to the list and returns a reference to the list.
1359*/
1360
1361/*! \fn template <typename T> QList<T> &QList<T>::operator<<(QList<T> &&other)
1362 \since 6.0
1363
1364 \overload
1365*/
1366
1367/*! \class QList::iterator
1368 \inmodule QtCore
1369 \brief Provides an STL-style non-const iterator for QList and QStack.
1370
1371 QList provides both \l{STL-style iterators} and \l{Java-style
1372 iterators}.
1373
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}.
1379
1380 \warning Iterators are invalidated when QList is modified. Consider that all
1381 iterators are invalidated by default. Exceptions to this rule are explicitly
1382 documented.
1383//! [iterator-invalidation-class-desc]
1384
1385 \sa QList::begin(), QList::end(), QList::const_iterator, QMutableListIterator
1386*/
1387
1388/*! \class QList::const_iterator
1389 \inmodule QtCore
1390 \brief Provides an STL-style const iterator for QList and QStack.
1391
1392 QList provides both \l{STL-style iterators} and \l{Java-style
1393 iterators}.
1394
1395 \include qlist.qdoc iterator-invalidation-class-desc
1396
1397 \sa QList::constBegin(), QList::constEnd(), QList::iterator, QListIterator
1398*/
1399
1400/*! \typedef QList::reverse_iterator
1401 \since 5.6
1402
1403 The QList::reverse_iterator typedef provides an STL-style non-const
1404 reverse iterator for QList.
1405
1406 \include qlist.qdoc iterator-invalidation-class-desc
1407
1408 \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator
1409*/
1410
1411/*! \typedef QList::const_reverse_iterator
1412 \since 5.6
1413
1414 The QList::const_reverse_iterator typedef provides an STL-style const
1415 reverse iterator for QList.
1416
1417 \include qlist.qdoc iterator-invalidation-class-desc
1418
1419 \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator
1420*/
1421
1422/*! \typedef QList::Iterator
1423
1424 Qt-style synonym for QList::iterator.
1425*/
1426
1427/*! \typedef QList::ConstIterator
1428
1429 Qt-style synonym for QList::const_iterator.
1430*/
1431
1432/*! \typedef QList::const_pointer
1433
1434 Provided for STL compatibility.
1435*/
1436
1437/*! \typedef QList::const_reference
1438
1439 Provided for STL compatibility.
1440*/
1441
1442/*! \typedef QList::difference_type
1443
1444 Provided for STL compatibility.
1445*/
1446
1447/*! \typedef QList::pointer
1448
1449 Provided for STL compatibility.
1450*/
1451
1452/*! \typedef QList::reference
1453
1454 Provided for STL compatibility.
1455*/
1456
1457/*! \typedef QList::size_type
1458
1459 Provided for STL compatibility.
1460*/
1461
1462/*! \typedef QList::value_type
1463
1464 Provided for STL compatibility.
1465*/
1466
1467/*! \typedef QList::parameter_type
1468
1469*/
1470
1471/*! \typedef QList::rvalue_ref
1472
1473*/
1474
1475/*! \fn template <typename T> QList<T> QList<T>::toList() const
1476 \fn template <typename T> QList<T> QList<T>::toVector() const
1477 \deprecated
1478
1479 A no-op in Qt 6. Provided for backwards compatibility with
1480 Qt 5, where QList and QVector where two different types.
1481
1482 Returns this list.
1483*/
1484
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)
1487 \deprecated
1488
1489 A no-op in Qt 6. Provided for backwards compatibility with
1490 Qt 5, where QList and QVector were two different types.
1491
1492 Returns this list.
1493*/
1494
1495/*! \fn template <typename T> QDataStream &operator<<(QDataStream &out, const QList<T> &list)
1496 \relates QList
1497
1498 Writes the list \a list to stream \a out.
1499
1500 This function requires the value type to implement \c operator<<().
1501
1502 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1503*/
1504
1505/*! \fn template <typename T> QDataStream &operator>>(QDataStream &in, QList<T> &list)
1506 \relates QList
1507
1508 Reads a list from stream \a in into \a list.
1509
1510 This function requires the value type to implement \c operator>>().
1511
1512 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1513*/
1514
1515/*! \fn template <typename T, typename AT> qsizetype erase(QList<T> &list, const AT &t)
1516 \relates QList
1517 \since 6.1
1518
1519 Removes all elements that compare equal to \a t from the
1520 list \a list. Returns the number of elements removed, if any.
1521
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
1525 with the copy.
1526
1527 \sa QList::removeAll(), erase_if
1528*/
1529
1530/*! \fn template <typename T, typename Predicate> qsizetype erase_if(QList<T> &list, Predicate pred)
1531 \relates QList
1532 \since 6.1
1533
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
1536 any.
1537
1538 \sa erase
1539*/
1540
1541/*! \fn template <typename T> QList<T>::assign(qsizetype n, parameter_type t)
1542 \since 6.6
1543
1544 Replaces the contents of this list with \a n copies of \a t.
1545
1546 The size of this list will be equal to \a n.
1547
1548 This function will only allocate memory if \a n exceeds the capacity of the
1549 list or this list is shared.
1550*/
1551
1552/*! \fn template <typename T> template <typename InputIterator, if_input_iterator<InputIterator>> QList<T>::assign(InputIterator first, InputIterator last)
1553 \since 6.6
1554
1555 Replaces the contents of this list with a copy of the elements in the
1556 iterator range [\a first, \a last).
1557
1558 The size of this list will be equal to the number of elements in the
1559 range [\a first, \a last).
1560
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.
1563
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}.
1567
1568 \note The behavior is undefined if either argument is an iterator into
1569 *this.
1570*/
1571
1572/*! \fn template <typename T> QList<T>::assign(std::initializer_list<T> l)
1573 \since 6.6
1574
1575 Replaces the contents of this list with a copy of the elements of
1576 \a l.
1577
1578 The size of this list will be equal to the number of elements in
1579 \a l.
1580
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.
1583*/