Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qvarlengtharray.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\class QVarLengthArray
6
\inmodule QtCore
7
\brief The QVarLengthArray class provides a low-level variable-length array.
8
9
\ingroup tools
10
\ingroup containers
11
\reentrant
12
13
The C++ language doesn't support variable-length arrays on the stack.
14
For example, the following code won't compile:
15
16
\snippet code/doc_src_qvarlengtharray.cpp 0
17
18
The alternative is to allocate the array on the heap (with
19
\c{new}):
20
21
\snippet code/doc_src_qvarlengtharray.cpp 1
22
23
However, if myfunc() is called very frequently from the
24
application's inner loop, heap allocation can be a major source
25
of slowdown.
26
27
QVarLengthArray is an attempt to work around this gap in the C++
28
language. It allocates a certain number of elements on the stack,
29
and if you resize the array to a larger size, it automatically
30
uses the heap instead. Stack allocation has the advantage that
31
it is much faster than heap allocation.
32
33
Example:
34
\snippet code/doc_src_qvarlengtharray.cpp 2
35
36
In the example above, QVarLengthArray will preallocate 1024
37
elements on the stack and use them unless \c{n + 1} is greater
38
than 1024. The template parameter \a T specifies the element type
39
and \a Prealloc specifies how many elements to preallocate on the
40
stack; if omitted, \a Prealloc defaults to 256.
41
42
QVarLengthArray's value type must be an \l{assignable data type}.
43
This covers most data types that are commonly used, but the
44
compiler won't let you, for example, store a QWidget as a value;
45
instead, store a QWidget *.
46
47
QVarLengthArray, like QList, provides a resizable array data
48
structure. The main differences between the two classes are:
49
50
\list
51
\li QVarLengthArray's API is much more low-level and it lacks
52
some of QList's functionality.
53
54
\li QVarLengthArray doesn't initialize the memory if the value is
55
a basic type. (QList always does.)
56
57
\li QList uses \l{implicit sharing} as a memory optimization.
58
QVarLengthArray doesn't provide that feature; however, it
59
usually produces slightly better performance due to reduced
60
overhead, especially in tight loops.
61
\endlist
62
63
In summary, QVarLengthArray is a low-level optimization class
64
that only makes sense in very specific cases. It is used a few
65
places inside Qt and was added to Qt's public API for the
66
convenience of advanced users.
67
68
\sa QList
69
*/
70
71
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray()
72
73
Constructs an array with an initial size of zero.
74
*/
75
76
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray(qsizetype size)
77
78
Constructs an array with an initial size of \a size elements.
79
80
If the value type is a primitive type (e.g., char, int, float) or
81
a pointer type (e.g., QWidget *), the elements are not
82
initialized. For other types, the elements are initialized with a
83
\l{default-constructed value}.
84
*/
85
86
/*!
87
\fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray(qsizetype size, const T &v)
88
\since 6.4
89
90
Constructs an array with an initial size of \a size elements filled with
91
copies of \a v.
92
93
\note This constructor is only available when \c T is copy-constructible.
94
95
\sa size(), squeeze()
96
*/
97
98
99
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray(std::initializer_list<T> args)
100
\since 5.5
101
102
Constructs an array from the std::initializer_list given by \a args.
103
*/
104
105
/*! \fn template<class T, qsizetype Prealloc> template<typename InputIterator, QVarLengthArray<T, Prealloc>::if_input_iterator<InputIterator>> QVarLengthArray<T, Prealloc>::QVarLengthArray(InputIterator first, InputIterator last)
106
\since 5.14
107
108
Constructs an array with the contents in the iterator range [\a first, \a last).
109
110
The value type of \c InputIterator must be convertible to \c T.
111
112
\constraints
113
\c InputIterator meets the requirements of an
114
\l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator}.
115
*/
116
117
118
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::~QVarLengthArray()
119
120
Destroys the array.
121
*/
122
123
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::size() const
124
125
Returns the number of elements in the array.
126
127
\sa isEmpty(), resize()
128
*/
129
130
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::count() const
131
132
Same as size().
133
134
\sa isEmpty(), resize()
135
*/
136
137
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::length() const
138
\since 5.0
139
140
Same as size().
141
142
\sa isEmpty(), resize()
143
*/
144
145
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::max_size() const
146
\fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::maxSize()
147
\since 6.8
148
149
It returns the maximum number of elements that the array can
150
theoretically hold. In practice, the number can be much smaller,
151
limited by the amount of memory available to the system.
152
*/
153
154
/*! \fn template<class T, qsizetype Prealloc> T& QVarLengthArray<T, Prealloc>::first()
155
156
Returns a reference to the first item in the array. The array must
157
not be empty. If the array can be empty, check isEmpty() before
158
calling this function.
159
160
\sa last(), isEmpty()
161
*/
162
163
/*! \fn template<class T, qsizetype Prealloc> const T& QVarLengthArray<T, Prealloc>::first() const
164
165
\overload
166
*/
167
168
/*! \fn template<class T, qsizetype Prealloc> T& QVarLengthArray<T, Prealloc>::front()
169
\since 5.0
170
171
Same as first(). Provided for STL-compatibility.
172
*/
173
174
/*! \fn template<class T, qsizetype Prealloc> const T& QVarLengthArray<T, Prealloc>::front() const
175
\since 5.0
176
177
\overload
178
*/
179
180
/*! \fn template<class T, qsizetype Prealloc> T& QVarLengthArray<T, Prealloc>::last()
181
182
Returns a reference to the last item in the array. The array must
183
not be empty. If the array can be empty, check isEmpty() before
184
calling this function.
185
186
\sa first(), isEmpty()
187
*/
188
189
/*! \fn template<class T, qsizetype Prealloc> const T& QVarLengthArray<T, Prealloc>::last() const
190
191
\overload
192
*/
193
194
/*! \fn template<class T, qsizetype Prealloc> T& QVarLengthArray<T, Prealloc>::back()
195
\since 5.0
196
197
Same as last(). Provided for STL-compatibility.
198
*/
199
200
/*! \fn template<class T, qsizetype Prealloc> const T& QVarLengthArray<T, Prealloc>::back() const
201
\since 5.0
202
203
\overload
204
*/
205
206
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::shrink_to_fit()
207
\since 5.10
208
209
Same as squeeze(). Provided for STL-compatibility.
210
*/
211
212
/*! \fn template<class T, qsizetype Prealloc> bool QVarLengthArray<T, Prealloc>::isEmpty() const
213
214
Returns \c true if the array has size 0; otherwise returns \c false.
215
216
\sa size(), resize()
217
*/
218
219
/*! \fn template<class T, qsizetype Prealloc> bool QVarLengthArray<T, Prealloc>::empty() const
220
\since 5.0
221
222
Returns \c true if the array has size 0; otherwise returns \c false.
223
224
Same as isEmpty(). Provided for STL-compatibility.
225
*/
226
227
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::clear()
228
229
Removes all the elements from the array.
230
231
Same as resize(0).
232
*/
233
234
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::resize(qsizetype size)
235
236
Sets the size of the array to \a size. If \a size is greater than
237
the current size, elements are added to the end. If \a size is
238
less than the current size, elements are removed from the end.
239
240
If the value type is a primitive type (e.g., char, int, float) or
241
a pointer type (e.g., QWidget *), new elements are not
242
initialized. For other types, the elements are initialized with a
243
\l{default-constructed value}.
244
245
\sa size(), squeeze()
246
*/
247
248
/*!
249
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::resize(qsizetype size, const T &v)
250
\since 6.4
251
252
Sets the size of the array to \a size. If \a size is greater than
253
the current size, copies of \a v are added to the end. If \a size is
254
less than the current size, elements are removed from the end.
255
256
\note This function is only available when \c T is copy-constructible.
257
258
\sa size(), squeeze()
259
*/
260
261
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::capacity() const
262
263
Returns the maximum number of elements that can be stored in the
264
array without forcing a reallocation.
265
266
The sole purpose of this function is to provide a means of fine
267
tuning QVarLengthArray's memory usage. In general, you will rarely ever
268
need to call this function. If you want to know how many items are
269
in the array, call size().
270
271
\sa reserve(), squeeze()
272
*/
273
274
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::reserve(qsizetype size)
275
276
Attempts to allocate memory for at least \a size elements. If you
277
know in advance how large the array can get, you can call this
278
function and if you call resize() often, you are likely to get
279
better performance. If \a size is an underestimate, the worst
280
that will happen is that the QVarLengthArray will be a bit
281
slower.
282
283
The sole purpose of this function is to provide a means of fine
284
tuning QVarLengthArray's memory usage. In general, you will
285
rarely ever need to call this function. If you want to change the
286
size of the array, call resize().
287
288
\sa capacity(), squeeze()
289
*/
290
291
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::squeeze()
292
\since 5.1
293
294
Releases any memory not required to store the items.
295
If the container can fit its storage on the stack allocation,
296
it will free the heap allocation and copy the elements back to the stack.
297
298
The sole purpose of this function is to provide a means of fine
299
tuning QVarLengthArray's memory usage. In general, you will rarely ever
300
need to call this function.
301
302
\sa reserve(), capacity(), resize()
303
*/
304
305
/*! \fn template<class T, qsizetype Prealloc> T &QVarLengthArray<T, Prealloc>::operator[](qsizetype i)
306
307
Returns a reference to the item at index position \a i.
308
309
//! [valid-i]
310
\a i must be a valid index position in the array (i.e., 0 <= \a i
311
< size()).
312
//! [valid-i]
313
314
\sa data(), at()
315
*/
316
317
/*! \fn template<class T, qsizetype Prealloc> const T &QVarLengthArray<T, Prealloc>::operator[](qsizetype i) const
318
319
\overload
320
*/
321
322
323
/*!
324
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::append(const T &t)
325
326
Appends item \a t to the array, extending the array if necessary.
327
328
\sa removeLast()
329
*/
330
331
/*!
332
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::push_back(const T &t)
333
\since 5.0
334
335
Appends item \a t to the array, extending the array if necessary.
336
Provided for STL-compatibility.
337
*/
338
339
/*!
340
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::append(T &&t)
341
\overload append
342
\since 5.9
343
344
\note Unlike the lvalue overload of append(), passing a reference to
345
an object that is already an element of \c *this leads to undefined
346
behavior:
347
348
\code
349
vla.append(std::move(vla[0])); // BUG: passing an object that is already in the container
350
\endcode
351
*/
352
353
/*!
354
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::push_back(T &&t)
355
\overload push_back
356
\since 5.9
357
358
\note Unlike the lvalue overload of push_back(), passing a reference to
359
an object that is already an element of \c *this leads to undefined
360
behavior:
361
362
\code
363
vla.push_back(std::move(vla[0])); // BUG: passing an object that is already in the container
364
\endcode
365
*/
366
367
/*!
368
\fn template<class T, qsizetype Prealloc> inline void QVarLengthArray<T, Prealloc>::removeLast()
369
\since 4.5
370
371
Decreases the size of the array by one. The allocated size is not changed.
372
373
\sa append()
374
*/
375
376
/*!
377
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::pop_back()
378
\since 5.0
379
380
Same as removeLast(). Provided for STL-compatibility.
381
*/
382
383
/*!
384
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::append(const T *buf, qsizetype size)
385
386
Appends \a size amount of items referenced by \a buf to this array.
387
*/
388
389
390
/*! \fn template<class T, qsizetype Prealloc> T *QVarLengthArray<T, Prealloc>::data()
391
392
Returns a pointer to the data stored in the array. The pointer can
393
be used to access and modify the items in the array.
394
395
Example:
396
\snippet code/doc_src_qvarlengtharray.cpp 3
397
398
The pointer remains valid as long as the array isn't reallocated.
399
400
This function is mostly useful to pass an array to a function
401
that accepts a plain C++ array.
402
403
\sa constData(), operator[]()
404
*/
405
406
/*! \fn template<class T, qsizetype Prealloc> const T *QVarLengthArray<T, Prealloc>::data() const
407
408
\overload
409
*/
410
411
/*! \fn template<class T, qsizetype Prealloc> const T *QVarLengthArray<T, Prealloc>::constData() const
412
413
Returns a const pointer to the data stored in the array. The
414
pointer can be used to access the items in the array. The
415
pointer remains valid as long as the array isn't reallocated.
416
417
This function is mostly useful to pass an array to a function
418
that accepts a plain C++ array.
419
420
\sa data(), operator[]()
421
*/
422
423
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator=(const QVarLengthArray<T, Prealloc> &other)
424
Assigns \a other to this array and returns a reference to this array.
425
*/
426
427
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator=(QVarLengthArray<T, Prealloc> &&other)
428
Move-assigns \a other to this array and returns a reference to this array.
429
After the move, \a other is empty.
430
\since 6.0
431
*/
432
433
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator=(std::initializer_list<T> list)
434
\since 5.5
435
436
Assigns the values of \a list to this array, and returns a reference to this array.
437
*/
438
439
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
440
Constructs a copy of \a other.
441
*/
442
443
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::QVarLengthArray(QVarLengthArray<T, Prealloc> &&other)
444
Move-constructs this variable-length array from \a other. After the move, \a other is empty.
445
\since 6.0
446
*/
447
448
/*! \fn template<class T, qsizetype Prealloc> const T &QVarLengthArray<T, Prealloc>::at(qsizetype i) const
449
450
Returns a reference to the item at index position \a i.
451
452
\include qvarlengtharray.qdoc valid-i
453
454
\sa value(), operator[]()
455
*/
456
457
/*! \fn template<class T, qsizetype Prealloc> T QVarLengthArray<T, Prealloc>::value(qsizetype i) const
458
459
Returns the value at index position \a i.
460
461
If the index \a i is out of bounds, the function returns
462
a \l{default-constructed value}. If you are certain that
463
\a i is within bounds, you can use at() instead, which is slightly
464
faster.
465
466
\sa at(), operator[]()
467
*/
468
469
/*! \fn template<class T, qsizetype Prealloc> T QVarLengthArray<T, Prealloc>::value(qsizetype i, const T &defaultValue) const
470
471
\overload
472
473
If the index \a i is out of bounds, the function returns
474
\a defaultValue.
475
*/
476
477
/*
478
\var QVarLengthArray::PreallocatedSize
479
\since 6.8
480
481
The same value as the \c{Prealloc} template argument. Provided for easier
482
access compared to manually extracting the value from the template
483
argument.
484
*/
485
486
/*!
487
\typedef QVarLengthArray::size_type
488
\since 4.7
489
490
Typedef for int. Provided for STL compatibility.
491
*/
492
493
/*!
494
\typedef QVarLengthArray::value_type
495
\since 4.7
496
497
Typedef for T. Provided for STL compatibility.
498
*/
499
500
/*!
501
\typedef QVarLengthArray::difference_type
502
\since 4.7
503
504
Typedef for ptrdiff_t. Provided for STL compatibility.
505
*/
506
507
/*!
508
\typedef QVarLengthArray::pointer
509
\since 4.7
510
511
Typedef for T *. Provided for STL compatibility.
512
*/
513
514
/*!
515
\typedef QVarLengthArray::const_pointer
516
\since 4.7
517
518
Typedef for const T *. Provided for STL compatibility.
519
*/
520
521
/*!
522
\typedef QVarLengthArray::reference
523
\since 4.7
524
525
Typedef for T &. Provided for STL compatibility.
526
*/
527
528
/*!
529
\typedef QVarLengthArray::const_reference
530
\since 4.7
531
532
Typedef for const T &. Provided for STL compatibility.
533
*/
534
535
/*!
536
\typedef QVarLengthArray::const_iterator
537
\since 4.7
538
539
Typedef for const T *. Provided for STL compatibility.
540
*/
541
542
/*!
543
\typedef QVarLengthArray::iterator
544
\since 4.7
545
546
Typedef for T *. Provided for STL compatibility.
547
*/
548
549
/*!
550
\typedef QVarLengthArray::const_reverse_iterator
551
\since 5.6
552
553
Typedef for \c{std::reverse_iterator<const T*>}. Provided for STL compatibility.
554
*/
555
556
/*!
557
\typedef QVarLengthArray::reverse_iterator
558
\since 5.6
559
560
Typedef for \c{std::reverse_iterator<T*>}. Provided for STL compatibility.
561
*/
562
563
/*!
564
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::prepend(const T &value)
565
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::prepend(T &&value)
566
567
\since 4.8
568
\deprecated [6.3] This is slow. If you must, use \c{insert(cbegin(), ~~~)} instead.
569
570
Inserts \a value at the beginning of the array.
571
572
573
This is the same as vector.insert(0, \a value).
574
575
For large arrays, this operation can be slow (\l{linear time}),
576
because it requires moving all the items in the vector by one
577
position further in memory. If you want a container class that
578
provides a fast prepend() function, use std::list instead.
579
580
\sa append(), insert()
581
*/
582
583
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::replace(qsizetype i, const T &value)
584
585
\since 4.8
586
Replaces the item at index position \a i with \a value.
587
588
\include qvarlengtharray.qdoc valid-i
589
590
\sa operator[](), remove()
591
*/
592
593
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::remove(qsizetype i, qsizetype count)
594
595
\overload
596
\since 4.8
597
598
Removes \a count elements from the array, starting at
599
index position \a i.
600
601
\include qvarlengtharray.qdoc valid-i
602
\a count must be <= size() - \a i.
603
\a i == size() is allowed if \a count == 0.
604
605
\sa insert(), replace()
606
*/
607
608
/*!
609
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::removeAt(qsizetype i)
610
\since 6.12
611
612
Removes the element at position \a i from the array.
613
614
\include qvarlengtharray.qdoc valid-i
615
616
This function is provided for compatibility with QList.
617
618
\sa insert(), replace()
619
*/
620
621
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::begin()
622
\since 4.8
623
624
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in
625
the array.
626
627
\sa constBegin(), end()
628
*/
629
630
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::begin() const
631
\since 4.8
632
\overload
633
*/
634
635
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::cbegin() const
636
\since 5.0
637
638
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item
639
in the array.
640
641
\sa begin(), cend()
642
*/
643
644
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::constBegin() const
645
\since 4.8
646
647
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item
648
in the array.
649
650
\sa begin(), constEnd()
651
*/
652
653
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::end()
654
\since 4.8
655
656
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item
657
after the last item in the array.
658
659
\sa begin(), constEnd()
660
*/
661
662
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::end() const
663
\since 4.8
664
665
\overload
666
*/
667
668
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::cend() const
669
\since 5.0
670
671
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
672
item after the last item in the array.
673
674
\sa cbegin(), end()
675
*/
676
677
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_iterator QVarLengthArray<T, Prealloc>::constEnd() const
678
\since 4.8
679
680
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
681
item after the last item in the array.
682
683
\sa constBegin(), end()
684
*/
685
686
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::reverse_iterator QVarLengthArray<T, Prealloc>::rbegin()
687
\since 5.6
688
689
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
690
item in the variable length array, in reverse order.
691
692
\sa begin(), crbegin(), rend()
693
*/
694
695
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray<T, Prealloc>::rbegin() const
696
\since 5.6
697
\overload
698
*/
699
700
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray<T, Prealloc>::crbegin() const
701
\since 5.6
702
703
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
704
item in the variable length array, in reverse order.
705
706
\sa begin(), rbegin(), rend()
707
*/
708
709
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::reverse_iterator QVarLengthArray<T, Prealloc>::rend()
710
\since 5.6
711
712
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
713
the last item in the variable length array, in reverse order.
714
715
\sa end(), crend(), rbegin()
716
*/
717
718
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray<T, Prealloc>::rend() const
719
\since 5.6
720
\overload
721
*/
722
723
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::const_reverse_iterator QVarLengthArray<T, Prealloc>::crend() const
724
\since 5.6
725
726
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one
727
past the last item in the variable length array, in reverse order.
728
729
\sa end(), rend(), rbegin()
730
*/
731
732
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(const_iterator pos)
733
\since 4.8
734
735
Removes the item pointed to by the iterator \a pos from the
736
vector, and returns an iterator to the next item in the vector
737
(which may be end()).
738
739
\sa insert(), remove()
740
*/
741
742
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(const_iterator begin, const_iterator end)
743
744
\overload
745
\since 4.8
746
747
Removes all the items from \a begin up to (but not including) \a
748
end. Returns an iterator to the same item that \a end referred to
749
before the call.
750
*/
751
752
/*!
753
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::insert(qsizetype i, const T &value)
754
\fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::insert(qsizetype i, T &&value)
755
\since 4.8
756
757
Inserts \a value at index position \a i in the array. If \a i is
758
0, the value is prepended to the vector. If \a i is size(), the
759
value is appended to the vector.
760
761
For large arrays, this operation can be slow (\l{linear time}),
762
because it requires moving all the items at indexes \a i and
763
above by one position further in memory. If you want a container
764
class that provides a fast insert() function, use std::list
765
instead.
766
767
\sa remove()
768
*/
769
770
/*! \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::insert(qsizetype i, qsizetype count, const T &value)
771
772
\overload
773
\since 4.8
774
775
Inserts \a count copies of \a value at index position \a i in the
776
vector.
777
*/
778
779
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, const T &value)
780
\fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, T &&value)
781
782
\overload
783
\since 4.8
784
785
Inserts \a value in front of the item pointed to by the iterator
786
\a before. Returns an iterator pointing at the inserted item.
787
*/
788
789
/*!
790
\fn template <class T, qsizetype Prealloc> template <typename...Args> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::emplace(const_iterator pos, Args &&...args)
791
792
\since 6.3
793
794
Inserts an item in front of the item pointed to by the iterator
795
\a pos, passing \a args to its constructor.
796
797
Returns an iterator pointing at the emplaced item.
798
*/
799
800
/*!
801
\fn template <class T, qsizetype Prealloc> template <typename...Args> T &QVarLengthArray<T, Prealloc>::emplace_back(Args &&...args)
802
\since 6.3
803
804
Inserts an item at the back of this QVarLengthArray, passing
805
\a args to its constructor.
806
807
Returns a reference to the emplaced item.
808
*/
809
810
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, qsizetype count, const T &value)
811
812
\since 4.8
813
Inserts \a count copies of \a value in front of the item pointed to
814
by the iterator \a before. Returns an iterator pointing at the
815
first of the inserted items.
816
*/
817
818
819
820
/*! \fn template<class T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator==(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
821
822
\relates QVarLengthArray
823
\since 4.8
824
Returns \c true if the two arrays, specified by \a left and \a right, are
825
equal. The preallocation sizes \a Prealloc1 and \a Prealloc2 may differ.
826
827
Two arrays are considered equal if they contain the same values
828
in the same order.
829
830
This function requires the value type to have an implementation
831
of \c operator==().
832
833
\sa {operator!=(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)}{operator!=()}
834
*/
835
836
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator!=(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
837
838
\relates QVarLengthArray
839
\since 4.8
840
Returns \c true if the two arrays, specified by \a left and \a right, are
841
\e not equal. The preallocation sizes \a Prealloc1 and \a Prealloc2 may
842
differ.
843
844
Two arrays are considered equal if they contain the same values
845
in the same order.
846
847
This function requires the value type to have an implementation
848
of \c operator==().
849
850
\sa {operator==(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)}{operator==()}
851
*/
852
853
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator<(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
854
\since 5.6
855
\relates QVarLengthArray
856
857
Returns \c true if variable length array \a lhs is
858
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
859
{lexicographically less than} \a rhs; otherwise returns \c false. The
860
preallocation sizes \a Prealloc1 and \a Prealloc2 may differ.
861
862
This function requires the value type to have an implementation
863
of \c operator<().
864
*/
865
866
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator<=(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
867
\since 5.6
868
\relates QVarLengthArray
869
870
Returns \c true if variable length array \a lhs is
871
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
872
{lexicographically less than or equal to} \a rhs; otherwise returns
873
\c false. The preallocation sizes \a Prealloc1 and \a Prealloc2 may differ.
874
875
This function requires the value type to have an implementation
876
of \c operator<().
877
*/
878
879
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator>(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
880
\since 5.6
881
\relates QVarLengthArray
882
883
Returns \c true if variable length array \a lhs is
884
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
885
{lexicographically greater than} \a rhs; otherwise returns \c false. The
886
preallocation sizes \a Prealloc1 and \a Prealloc2 may differ.
887
888
This function requires the value type to have an implementation
889
of \c operator<().
890
*/
891
892
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> bool operator>=(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
893
\since 5.6
894
\relates QVarLengthArray
895
896
Returns \c true if variable length array \a lhs is
897
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
898
{lexicographically greater than or equal to} \a rhs; otherwise returns
899
\c false. The preallocation sizes \a Prealloc1 and \a Prealloc2 may differ.
900
901
This function requires the value type to have an implementation
902
of \c operator<().
903
*/
904
905
/*! \fn template<typename T, qsizetype Prealloc1, qsizetype Prealloc2> auto operator<=>(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
906
\since 6.9
907
\relates QVarLengthArray
908
909
Compares the contents of \a lhs and \a rhs
910
\l {https://en.cppreference.com/w/cpp/algorithm/lexicographical_compare_three_way}
911
{lexicographically}. The preallocation sizes \a Prealloc1 and \a Prealloc2 may
912
differ. Returns the result of the strongest applicable category type, that is
913
\c {decltype(lhs[0] <=> rhs[0])} if \c {operator<=>()} is available for type
914
\c {T}; otherwise \c {std::weak_ordering}.
915
916
\note This operator is only available in C++20 mode, and when the underlying
917
type \c T models the \c {std::three_way_comparable} concept
918
or provides \c {operator<()}.
919
*/
920
921
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator<<(const T &value)
922
923
\since 4.8
924
Appends \a value to the array and returns a reference to this
925
vector.
926
927
\sa append(), operator+=()
928
*/
929
930
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator<<(T &&value)
931
\since 5.11
932
933
\overload
934
935
\sa append(), operator+=()
936
*/
937
938
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator+=(const T &value)
939
940
\since 4.8
941
Appends \a value to the array and returns a reference to this vector.
942
943
\sa append(), operator<<()
944
*/
945
946
/*! \fn template<class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc> &QVarLengthArray<T, Prealloc>::operator+=(T &&value)
947
\since 5.11
948
949
\overload
950
951
\sa append(), operator<<()
952
*/
953
954
/*! \fn template<class T, qsizetype Prealloc> template <typename AT = T> qsizetype QVarLengthArray<T, Prealloc>::indexOf(const AT &value, qsizetype from = 0) const
955
956
\since 5.3
957
Returns the index position of the first occurrence of \a value in
958
the array, searching forward from index position \a from.
959
Returns -1 if no item matched.
960
961
This function requires the value type to have an implementation of
962
\c operator==().
963
964
\sa lastIndexOf(), contains()
965
*/
966
967
/*! \fn template<class T, qsizetype Prealloc> template <typename AT = T> qsizetype QVarLengthArray<T, Prealloc>::lastIndexOf(const AT &value, qsizetype from = -1) const
968
969
\since 5.3
970
Returns the index position of the last occurrence of the value \a
971
value in the array, searching backward from index position \a
972
from. If \a from is -1 (the default), the search starts at the
973
last item. Returns -1 if no item matched.
974
975
This function requires the value type to have an implementation of
976
\c operator==().
977
978
\sa indexOf(), contains()
979
*/
980
981
/*! \fn template<class T, qsizetype Prealloc> template <typename AT = T> bool QVarLengthArray<T, Prealloc>::contains(const AT &value) const
982
983
\since 5.3
984
Returns \c true if the array contains an occurrence of \a value;
985
otherwise returns \c false.
986
987
This function requires the value type to have an implementation of
988
\c operator==().
989
990
\sa indexOf(), lastIndexOf()
991
*/
992
993
/*!
994
\fn template <typename T, qsizetype Prealloc> size_t qHash(const QVarLengthArray<T, Prealloc> &key, size_t seed = 0)
995
\qhasholdT{QVarLengthArray}{T}
996
\since 5.14
997
998
Returns the hash value for the \a key, using \a seed to seed the
999
calculation. The preallocation size \a Prealloc does not affect the hash.
1000
*/
1001
1002
/*! \fn template <typename T, qsizetype Prealloc> template <typename AT = T> qsizetype QVarLengthArray<T, Prealloc>::removeAll(const AT &t)
1003
\since 6.1
1004
1005
Removes all elements that compare equal to \a t from the
1006
array. Returns the number of elements removed, if any.
1007
1008
\sa removeOne()
1009
*/
1010
1011
/*! \fn template <typename T, qsizetype Prealloc> template <typename AT = T> bool QVarLengthArray<T, Prealloc>::removeOne(const AT &t)
1012
\since 6.1
1013
1014
Removes the first element that compares equal to \a t from the
1015
array. Returns whether an element was, in fact, removed.
1016
1017
\sa removeAll()
1018
*/
1019
1020
/*! \fn template <typename T, qsizetype Prealloc> template <typename Predicate> qsizetype QVarLengthArray<T, Prealloc>::removeIf(Predicate pred)
1021
\since 6.1
1022
1023
Removes all elements for which the predicate \a pred returns true
1024
from the array. Returns the number of elements removed, if any.
1025
1026
\sa removeAll()
1027
*/
1028
1029
/*! \fn template <typename T, qsizetype Prealloc, typename AT> qsizetype erase(QVarLengthArray<T, Prealloc> &array, const AT &t)
1030
\relates QVarLengthArray
1031
\since 6.1
1032
1033
Removes all elements that compare equal to \a t from the
1034
array \a array. Returns the number of elements removed, if any.
1035
1036
\note \a t is not allowed to be a reference to an element inside \a
1037
array. If you cannot be sure that this is not the case, take a copy
1038
of \a t and call this function with the copy.
1039
1040
\sa erase_if()
1041
*/
1042
1043
/*! \fn template <typename T, qsizetype Prealloc, typename Predicate> qsizetype erase_if(QVarLengthArray<T, Prealloc> &array, Predicate pred)
1044
\relates QVarLengthArray
1045
\since 6.1
1046
1047
Removes all elements for which the predicate \a pred returns true
1048
from the list \a array. Returns the number of elements removed, if
1049
any. The preallocation size \a Prealloc does not affect the operation.
1050
1051
\sa erase()
1052
*/
1053
1054
/*! \fn template <class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>& QVarLengthArray<T, Prealloc>::assign(qsizetype n, const T &t)
1055
\since 6.6
1056
1057
Replaces the contents of this container with \a n copies of \a t.
1058
1059
The size of this container will be equal to \a n. This function will only
1060
allocate memory if \a n exceeds the capacity of the container.
1061
*/
1062
1063
/*! \fn template <class T, qsizetype Prealloc> template <typename InputIterator, QVarLengthArray<T, Prealloc>::if_input_iterator<InputIterator>> QVarLengthArray<T, Prealloc>& QVarLengthArray<T, Prealloc>::assign(InputIterator first, InputIterator last)
1064
\since 6.6
1065
1066
Replaces the contents of this container with a copy of the elements in the
1067
iterator range [\a first, \a last).
1068
1069
The size of this container will be equal to the number of elements in the
1070
range [\a first, \a last). This function will only allocate memory if the
1071
number of elements in the range exceeds the capacity of the container.
1072
1073
The behavior is undefined if either argument is an iterator into *this.
1074
1075
\constraints
1076
\c InputIterator meets the requirements of an
1077
\l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator}.
1078
*/
1079
1080
/*! \fn template <class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>& QVarLengthArray<T, Prealloc>::assign(std::initializer_list<T> list)
1081
\since 6.6
1082
1083
Replaces the contents of this container with a copy of the elements of \a list.
1084
1085
The size of this container will be equal to the number of elements in \a list.
1086
1087
This function only allocates memory if the number of elements in \a list
1088
exceeds the capacity of the container.
1089
*/
qtbase
src
corelib
tools
qvarlengtharray.qdoc
Generated on
for Qt by
1.16.1