Qt 6.x
The Qt SDK
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qmap.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5/*!
6 \class QMap
7 \inmodule QtCore
8 \brief The QMap class is a template class that provides an associative array.
9
10 \ingroup tools
11 \ingroup shared
12
13 \reentrant
14
15 QMap<Key, T> is one of Qt's generic \l{container classes}. It
16 stores (key, value) pairs and provides fast lookup by key.
17
18 QMap and QHash provide very similar functionality. The
19 differences are:
20
21 \list
22 \li QHash provides average faster lookups than QMap. (See \l{Algorithmic
23 Complexity} for details.)
24 \li When iterating over a QHash, the items are arbitrarily ordered.
25 With QMap, the items are always sorted by key.
26 \li The key type of a QHash must provide operator==() and a global
27 qHash(Key) function. The key type of a QMap must provide
28 operator<() specifying a total order. Since Qt 5.8.1 it is also safe
29 to use a pointer type as key, even if the underlying operator<()
30 does not provide a total order.
31 \endlist
32
33 Here's an example QMap with QString keys and \c int values:
34 \snippet code/src_corelib_tools_qmap.cpp 0
35
36 To insert a (key, value) pair into the map, you can use operator[]():
37
38 \snippet code/src_corelib_tools_qmap.cpp 1
39
40 This inserts the following three (key, value) pairs into the
41 QMap: ("one", 1), ("three", 3), and ("seven", 7). Another way to
42 insert items into the map is to use insert():
43
44 \snippet code/src_corelib_tools_qmap.cpp 2
45
46 To look up a value, use operator[]() or value():
47
48 \snippet code/src_corelib_tools_qmap.cpp 3
49
50 If there is no item with the specified key in the map, these
51 functions return a \l{default-constructed value}.
52
53 If you want to check whether the map contains a certain key, use
54 contains():
55
56 \snippet code/src_corelib_tools_qmap.cpp 4
57
58 There is also a value() overload that uses its second argument as
59 a default value if there is no item with the specified key:
60
61 \snippet code/src_corelib_tools_qmap.cpp 5
62
63 In general, we recommend that you use contains() and value()
64 rather than operator[]() for looking up a key in a map. The
65 reason is that operator[]() silently inserts an item into the
66 map if no item exists with the same key (unless the map is
67 const). For example, the following code snippet will create 1000
68 items in memory:
69
70 \snippet code/src_corelib_tools_qmap.cpp 6
71
72 To avoid this problem, replace \c map[i] with \c map.value(i)
73 in the code above.
74
75 If you want to navigate through all the (key, value) pairs stored
76 in a QMap, you can use an iterator. QMap provides both
77 \l{Java-style iterators} (QMapIterator and QMutableMapIterator)
78 and \l{STL-style iterators} (QMap::const_iterator and
79 QMap::iterator). Here's how to iterate over a QMap<QString, int>
80 using a Java-style iterator:
81
82 \snippet code/src_corelib_tools_qmap.cpp 7
83
84 Here's the same code, but using an STL-style iterator this time:
85
86 \snippet code/src_corelib_tools_qmap.cpp 8
87
88 The items are traversed in ascending key order.
89
90 A QMap allows only one value per key. If you call
91 insert() with a key that already exists in the QMap, the
92 previous value will be erased. For example:
93
94 \snippet code/src_corelib_tools_qmap.cpp 9
95
96 However, you can store multiple values per key by using
97 QMultiMap.
98
99 If you only need to extract the values from a map (not the keys),
100 you can also use range-based for:
101
102 \snippet code/src_corelib_tools_qmap.cpp 12
103
104 Items can be removed from the map in several ways. One way is to
105 call remove(); this will remove any item with the given key.
106 Another way is to use QMutableMapIterator::remove(). In addition,
107 you can clear the entire map using clear().
108
109 QMap's key and value data types must be \l{assignable data
110 types}. This covers most data types you are likely to encounter,
111 but the compiler won't let you, for example, store a QWidget as a
112 value; instead, store a QWidget *. In addition, QMap's key type
113 must provide operator<(). QMap uses it to keep its items sorted,
114 and assumes that two keys \c x and \c y are equivalent if neither \c{x
115 < y} nor \c{y < x} is true.
116
117 Example:
118 \snippet code/src_corelib_tools_qmap.cpp 13
119
120 In the example, we start by comparing the employees' names. If
121 they're equal, we compare their dates of birth to break the tie.
122
123 \sa QMapIterator, QMutableMapIterator, QHash, QSet
124*/
125
126/*! \fn template <class Key, class T> QMap<Key, T>::QMap()
127
128 Constructs an empty map.
129
130 \sa clear()
131*/
132
133/*! \fn template <class Key, class T> QMap<Key, T>::QMap(const QMap<Key, T> &other)
134
135 Constructs a copy of \a other.
136
137 This operation occurs in \l{constant time}, because QMap is
138 \l{implicitly shared}. This makes returning a QMap from a
139 function very fast. If a shared instance is modified, it will be
140 copied (copy-on-write), and this takes \l{linear time}.
141
142 \sa operator=
143*/
144
145/*!
146 \fn template <class Key, class T> QMap<Key, T>::QMap(QMap<Key, T> &&other)
147
148 Move-constructs a QMap instance.
149
150 \since 5.2
151*/
152
153/*! \fn template <class Key, class T> QMap<Key, T> &QMap<Key, T>::operator=(const QMap<Key, T> &other)
154
155 Assigns \a other to this map and returns a reference to this map.
156*/
157
158/*!
159 \fn template <class Key, class T> QMap<Key, T> &QMap<Key, T>::operator=(QMap<Key, T> &&other)
160
161 Move-assigns \a other to this QMap instance.
162
163 \since 5.2
164*/
165
166/*! \fn template <class Key, class T> QMap<Key, T>::~QMap()
167
168 Destroys the map. References to the values in the map, and all
169 iterators over this map, become invalid.
170*/
171
172/*! \fn template <class Key, class T> void QMap<Key, T>::swap(QMap<Key, T> &other) noexcept
173 \since 4.8
174
175 Swaps map \a other with this map. This operation is very
176 fast and never fails.
177*/
178
179/*! \fn template <class Key, class T> QMap<Key, T>::QMap(std::initializer_list<std::pair<Key,T> > list)
180 \since 5.1
181
182 Constructs a map with a copy of each of the elements in the
183 initializer list \a list.
184*/
185
186/*! \fn template <class Key, class T> QMap<Key, T>::QMap(const std::map<Key, T> & other)
187
188 Constructs a copy of \a other.
189
190 \sa toStdMap()
191*/
192
193/*! \fn template <class Key, class T> QMap<Key, T>::QMap(std::map<Key, T> && other)
194
195 Constructs a map by moving from \a other.
196
197 \sa toStdMap()
198*/
199
200/*! \fn template <class Key, class T> std::map<Key, T> QMap<Key, T>::toStdMap() const &
201
202 Returns an STL map equivalent to this QMap.
203*/
204
205/*! \fn template <class Key, class T> std::map<Key, T> QMap<Key, T>::toStdMap() &&
206
207 \overload
208 \since 6.0
209
210 \note Calling this function will leave this QMap in the partially-formed state, in which
211 the only valid operations are destruction or assignment of a new value.
212*/
213
214/*! \fn template <class Key, class T> bool QMap<Key, T>::operator==(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
215
216 Returns \c true if \a lhs is equal to \a rhs; otherwise returns
217 false.
218
219 Two maps are considered equal if they contain the same (key,
220 value) pairs.
221
222 This function requires the key and the value types to implement \c
223 operator==().
224
225 \sa operator!=()
226*/
227
228/*! \fn template <class Key, class T> bool QMap<Key, T>::operator!=(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
229
230 Returns \c true if \a lhs is not equal to \a rhs; otherwise returns
231 false.
232
233 Two maps are considered equal if they contain the same (key,
234 value) pairs.
235
236 This function requires the key and the value types to implement \c
237 operator==().
238
239 \sa operator==()
240*/
241
242/*! \fn template <class Key, class T> size_type QMap<Key, T>::size() const
243
244 Returns the number of (key, value) pairs in the map.
245
246 \sa isEmpty(), count()
247*/
248
249/*!
250 \fn template <class Key, class T> bool QMap<Key, T>::isEmpty() const
251
252 Returns \c true if the map contains no items; otherwise returns
253 false.
254
255 \sa size()
256*/
257
258/*! \fn template <class Key, class T> void QMap<Key, T>::detach()
259
260 \internal
261
262 Detaches this map from any other maps with which it may share
263 data.
264
265 \sa isDetached()
266*/
267
268/*! \fn template <class Key, class T> bool QMap<Key, T>::isDetached() const
269
270 \internal
271
272 Returns \c true if the map's internal data isn't shared with any
273 other map object; otherwise returns \c false.
274
275 \sa detach()
276*/
277
278/*! \fn template <class Key, class T> bool QMap<Key, T>::isSharedWith(const QMap<Key, T> &other) const
279
280 \internal
281*/
282
283/*! \fn template <class Key, class T> void QMap<Key, T>::clear()
284
285 Removes all items from the map.
286
287 \sa remove()
288*/
289
290/*! \fn template <class Key, class T> size_type QMap<Key, T>::remove(const Key &key)
291
292 Removes all the items that have the key \a key from the map.
293 Returns the number of items removed which will be 1 if the key
294 exists in the map, and 0 otherwise.
295
296 \sa clear(), take()
297*/
298
299/*! \fn template <class Key, class T> template <typename Predicate> size_type QMap<Key, T>::removeIf(Predicate pred)
300 \since 6.1
301
302 Removes all elements for which the predicate \a pred returns true
303 from the map.
304
305 The function supports predicates which take either an argument of
306 type \c{QMap<Key, T>::iterator}, or an argument of type
307 \c{std::pair<const Key &, T &>}.
308
309 Returns the number of elements removed, if any.
310
311 \sa clear(), take()
312*/
313
314/*! \fn template <class Key, class T> T QMap<Key, T>::take(const Key &key)
315
316 Removes the item with the key \a key from the map and returns
317 the value associated with it.
318
319 If the item does not exist in the map, the function simply
320 returns a \l{default-constructed value}.
321
322 If you don't use the return value, remove() is more efficient.
323
324 \sa remove()
325*/
326
327/*! \fn template <class Key, class T> bool QMap<Key, T>::contains(const Key &key) const
328
329 Returns \c true if the map contains an item with key \a key;
330 otherwise returns \c false.
331
332 \sa count()
333*/
334
335/*!
336 \fn template <class Key, class T> Key QMap<Key, T>::key(const T &value, const Key &defaultKey) const
337 \since 4.3
338 \overload
339
340 Returns the first key with value \a value, or \a defaultKey if
341 the map contains no item with value \a value. If no \a defaultKey
342 is provided the function returns a
343 \l{default-constructed value}{default-constructed key}.
344
345 This function can be slow (\l{linear time}), because QMap's
346 internal data structure is optimized for fast lookup by key, not
347 by value.
348
349 \sa value(), keys()
350*/
351
352/*! \fn template <class Key, class T> T QMap<Key, T>::value(const Key &key, const T &defaultValue) const
353
354 Returns the value associated with the key \a key.
355
356 If the map contains no item with key \a key, the function returns
357 \a defaultValue. If no \a defaultValue is specified, the function
358 returns a \l{default-constructed value}.
359
360 \sa key(), values(), contains(), operator[]()
361*/
362
363/*! \fn template <class Key, class T> T &QMap<Key, T>::operator[](const Key &key)
364
365 Returns the value associated with the key \a key as a modifiable
366 reference.
367
368 If the map contains no item with key \a key, the function inserts
369 a \l{default-constructed value} into the map with key \a key, and
370 returns a reference to it.
371
372 \sa insert(), value()
373*/
374
375/*! \fn template <class Key, class T> T QMap<Key, T>::operator[](const Key &key) const
376
377 \overload
378
379 Same as value().
380*/
381
382/*! \fn template <class Key, class T> QList<Key> QMap<Key, T>::keys() const
383
384 Returns a list containing all the keys in the map in ascending
385 order.
386
387 The order is guaranteed to be the same as that used by values().
388
389 This function creates a new list, in \l {linear time}. The time and memory
390 use that entails can be avoided by iterating from \l keyBegin() to
391 \l keyEnd().
392
393 \sa QMultiMap::uniqueKeys(), values(), key()
394*/
395
396/*! \fn template <class Key, class T> QList<Key> QMap<Key, T>::keys(const T &value) const
397
398 \overload
399
400 Returns a list containing all the keys associated with value \a
401 value in ascending order.
402
403 This function can be slow (\l{linear time}), because QMap's
404 internal data structure is optimized for fast lookup by key, not
405 by value.
406*/
407
408/*! \fn template <class Key, class T> QList<T> QMap<Key, T>::values() const
409
410 Returns a list containing all the values in the map, in ascending
411 order of their keys.
412
413 This function creates a new list, in \l {linear time}. The time and memory
414 use that entails can be avoided by iterating from \l keyValueBegin() to
415 \l keyValueEnd().
416
417 \sa keys(), value()
418*/
419
420/*! \fn template <class Key, class T> size_type QMap<Key, T>::count(const Key &key) const
421
422 Returns the number of items associated with key \a key.
423
424 \sa contains()
425*/
426
427/*! \fn template <class Key, class T> size_type QMap<Key, T>::count() const
428
429 \overload
430
431 Same as size().
432*/
433
434/*! \fn template <class Key, class T> const Key &QMap<Key, T>::firstKey() const
435 \since 5.2
436
437 Returns a reference to the smallest key in the map.
438 This function assumes that the map is not empty.
439
440 This executes in \l{constant time}.
441
442 \sa lastKey(), first(), keyBegin(), isEmpty()
443*/
444
445/*! \fn template <class Key, class T> const Key &QMap<Key, T>::lastKey() const
446 \since 5.2
447
448 Returns a reference to the largest key in the map.
449 This function assumes that the map is not empty.
450
451 This executes in \l{logarithmic time}.
452
453 \sa firstKey(), last(), keyEnd(), isEmpty()
454*/
455
456/*! \fn template <class Key, class T> T &QMap<Key, T>::first()
457 \since 5.2
458
459 Returns a reference to the first value in the map, that is the value mapped
460 to the smallest key. This function assumes that the map is not empty.
461
462 When unshared (or const version is called), this executes in \l{constant time}.
463
464 \sa last(), firstKey(), isEmpty()
465*/
466
467/*! \fn template <class Key, class T> const T &QMap<Key, T>::first() const
468 \since 5.2
469
470 \overload
471*/
472
473/*! \fn template <class Key, class T> T &QMap<Key, T>::last()
474 \since 5.2
475
476 Returns a reference to the last value in the map, that is the value mapped
477 to the largest key. This function assumes that the map is not empty.
478
479 When unshared (or const version is called), this executes in \l{logarithmic time}.
480
481 \sa first(), lastKey(), isEmpty()
482*/
483
484/*! \fn template <class Key, class T> const T &QMap<Key, T>::last() const
485 \since 5.2
486
487 \overload
488*/
489
490/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::begin()
491
492 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in
493 the map.
494
495 \sa constBegin(), end()
496*/
497
498/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::begin() const
499
500 \overload
501*/
502
503/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::cbegin() const
504 \since 5.0
505
506 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item
507 in the map.
508
509 \sa begin(), cend()
510*/
511
512/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::constBegin() const
513
514 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item
515 in the map.
516
517 \sa begin(), constEnd()
518*/
519
520/*! \fn template <class Key, class T> QMap<Key, T>::key_iterator QMap<Key, T>::keyBegin() const
521 \since 5.6
522
523 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first key
524 in the map.
525
526 \sa keyEnd(), firstKey()
527*/
528
529/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::end()
530
531 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item
532 after the last item in the map.
533
534 \sa begin(), constEnd()
535*/
536
537/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::end() const
538
539 \overload
540*/
541
542/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::cend() const
543 \since 5.0
544
545 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
546 item after the last item in the map.
547
548 \sa cbegin(), end()
549*/
550
551/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::constEnd() const
552
553 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
554 item after the last item in the map.
555
556 \sa constBegin(), end()
557*/
558
559/*! \fn template <class Key, class T> QMap<Key, T>::key_iterator QMap<Key, T>::keyEnd() const
560 \since 5.6
561
562 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
563 item after the last key in the map.
564
565 \sa keyBegin(), lastKey()
566*/
567
568
569/*! \fn template <class Key, class T> QMap<Key, T>::key_value_iterator QMap<Key, T>::keyValueBegin()
570 \since 5.10
571
572 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first entry
573 in the map.
574
575 \sa keyValueEnd()
576*/
577
578/*! \fn template <class Key, class T> QMap<Key, T>::key_value_iterator QMap<Key, T>::keyValueEnd()
579 \since 5.10
580
581 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
582 entry after the last entry in the map.
583
584 \sa keyValueBegin()
585*/
586
587/*! \fn template <class Key, class T> QMap<Key, T>::const_key_value_iterator QMap<Key, T>::keyValueBegin() const
588 \since 5.10
589
590 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry
591 in the map.
592
593 \sa keyValueEnd()
594*/
595
596/*! \fn template <class Key, class T> QMap<Key, T>::const_key_value_iterator QMap<Key, T>::constKeyValueBegin() const
597 \since 5.10
598
599 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry
600 in the map.
601
602 \sa keyValueBegin()
603*/
604
605/*! \fn template <class Key, class T> QMap<Key, T>::const_key_value_iterator QMap<Key, T>::keyValueEnd() const
606 \since 5.10
607
608 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
609 entry after the last entry in the map.
610
611 \sa keyValueBegin()
612*/
613
614/*! \fn template <class Key, class T> QMap<Key, T>::const_key_value_iterator QMap<Key, T>::constKeyValueEnd() const
615 \since 5.10
616
617 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
618 entry after the last entry in the map.
619
620 \sa constKeyValueBegin()
621*/
622
623/*! \fn template <class Key, class T> auto QMap<Key, T>::asKeyValueRange() &
624 \fn template <class Key, class T> auto QMap<Key, T>::asKeyValueRange() const &
625 \fn template <class Key, class T> auto QMap<Key, T>::asKeyValueRange() &&
626 \fn template <class Key, class T> auto QMap<Key, T>::asKeyValueRange() const &&
627 \since 6.4
628
629 Returns a range object that allows iteration over this map as
630 key/value pairs. For instance, this range object can be used in a
631 range-based for loop, in combination with a structured binding declaration:
632
633 \snippet code/src_corelib_tools_qmap.cpp 28
634
635 Note that both the key and the value obtained this way are
636 references to the ones in the map. Specifically, mutating the value
637 will modify the map itself.
638
639 \sa QKeyValueIterator
640*/
641
642/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::erase(const_iterator pos)
643
644 Removes the (key, value) pair pointed to by the iterator \a pos
645 from the map, and returns an iterator to the next item in the
646 map.
647
648 \note The iterator \a pos \e must be valid and dereferenceable.
649
650 \sa remove()
651*/
652
653/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::erase(const_iterator first, const_iterator last)
654 \since 6.0
655
656 Removes the (key, value) pairs pointed to by the iterator range
657 [\a first, \a last) from the map.
658 Returns an iterator to the item in the map following the last removed element.
659
660 \note The range \c {[first, last)} \e must be a valid range in \c {*this}.
661
662 \sa remove()
663*/
664
665/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::find(const Key &key)
666
667 Returns an iterator pointing to the item with key \a key in the
668 map.
669
670 If the map contains no item with key \a key, the function
671 returns end().
672
673 \sa constFind(), value(), values(), lowerBound(), upperBound()
674*/
675
676/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::find(const Key &key) const
677
678 \overload
679*/
680
681/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::constFind(const Key &key) const
682 \since 4.1
683
684 Returns an const iterator pointing to the item with key \a key in the
685 map.
686
687 If the map contains no item with key \a key, the function
688 returns constEnd().
689
690 \sa find()
691*/
692
693/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::lowerBound(const Key &key)
694
695 Returns an iterator pointing to the first item with key \a key in
696 the map. If the map contains no item with key \a key, the
697 function returns an iterator to the nearest item with a greater
698 key.
699
700 \sa upperBound(), find()
701*/
702
703/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::lowerBound(const Key &key) const
704
705 \overload
706*/
707
708/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::upperBound(const Key &key)
709
710 Returns an iterator pointing to the item that immediately follows
711 the last item with key \a key in the map. If the map contains no
712 item with key \a key, the function returns an iterator to the
713 nearest item with a greater key.
714
715 Example:
716 \snippet code/src_corelib_tools_qmap.cpp 17
717
718 \sa lowerBound(), find()
719*/
720
721/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::upperBound(const Key &key) const
722
723 \overload
724*/
725
726/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &key, const T &value)
727
728 Inserts a new item with the key \a key and a value of \a value.
729
730 If there is already an item with the key \a key, that item's value
731 is replaced with \a value.
732
733 \sa QMultiMap::insert()
734*/
735
736/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::insert(const_iterator pos, const Key &key, const T &value)
737 \overload
738 \since 5.1
739 Inserts a new item with the key \a key and value \a value and with hint \a pos
740 suggesting where to do the insert.
741
742 If constBegin() is used as hint it indicates that the \a key is less than any key in the map
743 while constEnd() suggests that the \a key is (strictly) larger than any key in the map.
744 Otherwise the hint should meet the condition (\a pos - 1).key() < \a key <= pos.key().
745 If the hint \a pos is wrong it is ignored and a regular insert is done.
746
747 If there is already an item with the key \a key, that item's value
748 is replaced with \a value.
749
750 If the hint is correct and the map is unshared, the insert executes in amortized \l{constant time}.
751
752 When creating a map from sorted data inserting the largest key first with constBegin()
753 is faster than inserting in sorted order with constEnd(), since constEnd() - 1 (which is needed
754 to check if the hint is valid) needs \l{logarithmic time}.
755
756 \b {Note:} Be careful with the hint. Providing an iterator from an older shared instance might
757 crash but there is also a risk that it will silently corrupt both the map and the \a pos map.
758
759 \sa QMultiMap::insert()
760*/
761
762/*! \fn template <class Key, class T> void QMap<Key, T>::insert(const QMap<Key, T> &map)
763 \since 5.15
764
765 Inserts all the items in \a map into this map.
766
767 If a key is common to both maps, its value will be replaced with
768 the value stored in \a map.
769
770 \sa QMultiMap::insert()
771*/
772
773/*! \fn template <class Key, class T> void QMap<Key, T>::insert(QMap<Key, T> &&map)
774 \since 5.15
775
776 Moves all the items from \a map into this map.
777
778 If a key is common to both maps, its value will be replaced with
779 the value stored in \a map.
780
781 If \a map is shared, then the items will be copied instead.
782*/
783
784/*! \typedef QMap::Iterator
785
786 Qt-style synonym for QMap::iterator.
787*/
788
789/*! \typedef QMap::ConstIterator
790
791 Qt-style synonym for QMap::const_iterator.
792*/
793
794/*! \typedef QMap::difference_type
795
796 Typedef for ptrdiff_t. Provided for STL compatibility.
797*/
798
799/*! \typedef QMap::key_type
800
801 Typedef for Key. Provided for STL compatibility.
802*/
803
804/*! \typedef QMap::mapped_type
805
806 Typedef for T. Provided for STL compatibility.
807*/
808
809/*! \typedef QMap::size_type
810
811 Typedef for int. Provided for STL compatibility.
812*/
813
814/*!
815 \fn template <class Key, class T> bool QMap<Key, T>::empty() const
816
817 This function is provided for STL compatibility. It is equivalent
818 to isEmpty(), returning true if the map is empty; otherwise
819 returning false.
820*/
821
822/*!
823 \fn template <class Key, class T> QPair<typename QMap<Key, T>::iterator, typename QMap<Key, T>::iterator> QMap<Key, T>::equal_range(const Key &key)
824
825 Returns a pair of iterators delimiting the range of values \c{[first, second)}, that
826 are stored under \a key.
827*/
828
829/*!
830 \fn template <class Key, class T> QPair<typename QMap<Key, T>::const_iterator, typename QMap<Key, T>::const_iterator> QMap<Key, T>::equal_range(const Key &key) const
831 \overload
832 \since 5.6
833*/
834
835
836/*! \class QMap::iterator
837 \inmodule QtCore
838 \brief The QMap::iterator class provides an STL-style non-const iterator for QMap.
839
840 QMap<Key, T>::iterator allows you to iterate over a QMap
841 and to modify the value (but not the key) stored under
842 a particular key. If you want to iterate over a const QMap, you
843 should use QMap::const_iterator. It is generally good practice to
844 use QMap::const_iterator on a non-const QMap as well, unless you
845 need to change the QMap through the iterator. Const iterators are
846 slightly faster, and can improve code readability.
847
848 The default QMap::iterator constructor creates an uninitialized
849 iterator. You must initialize it using a QMap function like
850 QMap::begin(), QMap::end(), or QMap::find() before you can
851 start iterating. Here's a typical loop that prints all the (key,
852 value) pairs stored in a map:
853
854 \snippet code/src_corelib_tools_qmap.cpp 18
855
856 Unlike QHash, which stores its items in an arbitrary order, QMap
857 stores its items ordered by key.
858
859 Here's an example that increments every value stored in the QMap
860 by 2:
861
862 \snippet code/src_corelib_tools_qmap.cpp 19
863
864 To remove elements from a QMap you can use erase_if(QMap<Key, T> &map, Predicate pred):
865
866 \snippet code/src_corelib_tools_qmap.cpp 21
867
868 Multiple iterators can be used on the same map. If you add items
869 to the map, existing iterators will remain valid. If you remove
870 items from the map, iterators that point to the removed items
871 will become dangling iterators.
872
873 \warning Iterators on implicitly shared containers do not work
874 exactly like STL-iterators. You should avoid copying a container
875 while iterators are active on that container. For more information,
876 read \l{Implicit sharing iterator problem}.
877
878 \sa QMap::const_iterator, QMap::key_iterator, QMap::key_value_iterator
879*/
880
881/*! \typedef QMap::iterator::difference_type
882
883 \internal
884*/
885
886/*! \typedef QMap::iterator::iterator_category
887
888 A synonym for \e {std::bidirectional_iterator_tag} indicating
889 this iterator is a bidirectional iterator.
890*/
891
892/*! \typedef QMap::iterator::pointer
893
894 \internal
895*/
896
897/*! \typedef QMap::iterator::reference
898
899 \internal
900*/
901
902/*! \typedef QMap::iterator::value_type
903
904 \internal
905*/
906
907/*! \fn template <class Key, class T> QMap<Key, T>::iterator::iterator()
908
909 Constructs an uninitialized iterator.
910
911 Functions like key(), value(), and operator++() must not be
912 called on an uninitialized iterator. Use operator=() to assign a
913 value to it before using it.
914
915 \sa QMap::begin(), QMap::end()
916*/
917
918/*! \fn template <class Key, class T> const Key &QMap<Key, T>::iterator::key() const
919
920 Returns the current item's key as a const reference.
921
922 There is no direct way of changing an item's key through an
923 iterator, although it can be done by calling QMap::erase()
924 followed by QMap::insert().
925
926 \sa value()
927*/
928
929/*! \fn template <class Key, class T> T &QMap<Key, T>::iterator::value() const
930
931 Returns a modifiable reference to the current item's value.
932
933 You can change the value of an item by using value() on
934 the left side of an assignment, for example:
935
936 \snippet code/src_corelib_tools_qmap.cpp 23
937
938 \sa key(), operator*()
939*/
940
941/*! \fn template <class Key, class T> T &QMap<Key, T>::iterator::operator*() const
942
943 Returns a modifiable reference to the current item's value.
944
945 Same as value().
946
947 \sa key()
948*/
949
950/*! \fn template <class Key, class T> T *QMap<Key, T>::iterator::operator->() const
951
952 Returns a pointer to the current item's value.
953
954 \sa value()
955*/
956
957/*!
958 \fn template <class Key, class T> bool QMap<Key, T>::iterator::operator==(const iterator &lhs, const iterator &rhs)
959 \fn template <class Key, class T> bool QMap<Key, T>::const_iterator::operator==(const const_iterator &lhs, const const_iterator &rhs)
960
961 Returns \c true if \a lhs points to the same item as the \a rhs iterator;
962 otherwise returns \c false.
963
964 \sa operator!=()
965*/
966
967/*!
968 \fn template <class Key, class T> bool QMap<Key, T>::iterator::operator!=(const iterator &lhs, const iterator &rhs)
969 \fn template <class Key, class T> bool QMap<Key, T>::const_iterator::operator!=(const const_iterator &lhs, const const_iterator &rhs)
970
971 Returns \c true if \a lhs points to a different item than the \a rhs iterator;
972 otherwise returns \c false.
973
974 \sa operator==()
975*/
976
977/*! \fn template <class Key, class T> QMap<Key, T>::iterator &QMap<Key, T>::iterator::operator++()
978
979 The prefix \c{++} operator (\c{++i}) advances the iterator to the
980 next item in the map and returns an iterator to the new current
981 item.
982
983 Calling this function on QMap::end() leads to undefined results.
984
985 \sa operator--()
986*/
987
988/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::iterator::operator++(int)
989
990 \overload
991
992 The postfix \c{++} operator (\c{i++}) advances the iterator to the
993 next item in the map and returns an iterator to the previously
994 current item.
995*/
996
997/*! \fn template <class Key, class T> QMap<Key, T>::iterator &QMap<Key, T>::iterator::operator--()
998
999 The prefix \c{--} operator (\c{--i}) makes the preceding item
1000 current and returns an iterator pointing to the new current item.
1001
1002 Calling this function on QMap::begin() leads to undefined
1003 results.
1004
1005 \sa operator++()
1006*/
1007
1008/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::iterator::operator--(int)
1009
1010 \overload
1011
1012 The postfix \c{--} operator (\c{i--}) makes the preceding item
1013 current and returns an iterator pointing to the previously
1014 current item.
1015*/
1016
1017/*! //! friends
1018 \fn [qmap-op-it-plus-step] template <class Key, class T> typename QMap<Key, T>::iterator QMap<Key, T>::iterator::operator+(QMap<Key, T>::iterator, difference_type n)
1019 \fn [qmap-op-step-plus-it] template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::iterator::operator+(difference_type n, QMap<Key, T>::iterator)
1020 \fn [qmap-op-it-minus-step] template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::iterator::operator-(QMap<Key, T>::iterator, difference_type n)
1021 \fn [qmap-op-step-minus-it] template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::iterator::operator-(difference_type n, QMap<Key, T>::iterator)
1022
1023 //! members
1024 \fn template <class Key, class T> typename QMap<Key, T>::iterator QMap<Key, T>::iterator::operator+=(QMap<Key, T>::iterator::difference_type n)
1025 \fn template <class Key, class T> typename QMap<Key, T>::iterator QMap<Key, T>::iterator::operator-=(QMap<Key, T>::iterator::difference_type n)
1026
1027 \deprecated [6.2] Use \c{std::next}, \c{std::prev} or \c{std::advance} instead.
1028
1029 Moves an iterator by \e{n} positions. These operations can be
1030 expensive for large values of \e{n}; QMap iterators are not
1031 random access.
1032*/
1033
1034/*! \class QMap::const_iterator
1035 \inmodule QtCore
1036 \brief The QMap::const_iterator class provides an STL-style const iterator for QMap.
1037
1038 QMap<Key, T>::const_iterator allows you to iterate over a QMap.
1039 If you want to modify the QMap as you iterate
1040 over it, you must use QMap::iterator instead. It is generally
1041 good practice to use QMap::const_iterator on a non-const QMap as
1042 well, unless you need to change the QMap through the iterator.
1043 Const iterators are slightly faster, and can improve code
1044 readability.
1045
1046 The default QMap::const_iterator constructor creates an
1047 uninitialized iterator. You must initialize it using a QMap
1048 function like QMap::cbegin(), QMap::cend(), or
1049 QMap::constFind() before you can start iterating. Here's a typical
1050 loop that prints all the (key, value) pairs stored in a map:
1051
1052 \snippet code/src_corelib_tools_qmap.cpp 24
1053
1054 Here's an example that removes all the items whose value is greater than 10:
1055
1056 \snippet code/src_corelib_tools_qmap.cpp 20
1057
1058 And here the same behavior with erase_if()
1059
1060 \snippet code/src_corelib_tools_qmap.cpp 21
1061
1062 Unlike QHash, which stores its items in an arbitrary order, QMap
1063 stores its items ordered by key.
1064
1065 Multiple iterators can be used on the same map. If you add items
1066 to the map, existing iterators will remain valid. If you remove
1067 items from the map, iterators that point to the removed items
1068 will become dangling iterators.
1069
1070 \warning Iterators on implicitly shared containers do not work
1071 exactly like STL-iterators. You should avoid copying a container
1072 while iterators are active on that container. For more information,
1073 read \l{Implicit sharing iterator problem}.
1074
1075 \sa QMap::iterator, QMap::key_iterator, QMap::const_key_value_iterator
1076*/
1077
1078/*! \typedef QMap::const_iterator::difference_type
1079
1080 \internal
1081*/
1082
1083/*! \typedef QMap::const_iterator::iterator_category
1084
1085 A synonym for \e {std::bidirectional_iterator_tag} indicating
1086 this iterator is a bidirectional iterator.
1087*/
1088
1089/*! \typedef QMap::const_iterator::pointer
1090
1091 \internal
1092*/
1093
1094/*! \typedef QMap::const_iterator::reference
1095
1096 \internal
1097*/
1098
1099/*! \typedef QMap::const_iterator::value_type
1100
1101 \internal
1102*/
1103
1104/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator::const_iterator()
1105
1106 Constructs an uninitialized iterator.
1107
1108 Functions like key(), value(), and operator++() must not be
1109 called on an uninitialized iterator. Use operator=() to assign a
1110 value to it before using it.
1111
1112 \sa QMap::constBegin(), QMap::constEnd()
1113*/
1114
1115/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator::const_iterator(const iterator &other)
1116
1117 Constructs a copy of \a other.
1118*/
1119
1120/*! \fn template <class Key, class T> const Key &QMap<Key, T>::const_iterator::key() const
1121
1122 Returns the current item's key.
1123
1124 \sa value()
1125*/
1126
1127/*! \fn template <class Key, class T> const T &QMap<Key, T>::const_iterator::value() const
1128
1129 Returns the current item's value.
1130
1131 \sa key(), operator*()
1132*/
1133
1134/*! \fn template <class Key, class T> const T &QMap<Key, T>::const_iterator::operator*() const
1135
1136 Returns the current item's value.
1137
1138 Same as value().
1139
1140 \sa key()
1141*/
1142
1143/*! \fn template <class Key, class T> const T *QMap<Key, T>::const_iterator::operator->() const
1144
1145 Returns a pointer to the current item's value.
1146
1147 \sa value()
1148*/
1149
1150/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator &QMap<Key, T>::const_iterator::operator++()
1151
1152 The prefix \c{++} operator (\c{++i}) advances the iterator to the
1153 next item in the map and returns an iterator to the new current
1154 item.
1155
1156 Calling this function on QMap::end() leads to undefined results.
1157
1158 \sa operator--()
1159*/
1160
1161/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::const_iterator::operator++(int)
1162
1163 \overload
1164
1165 The postfix \c{++} operator (\c{i++}) advances the iterator to the
1166 next item in the map and returns an iterator to the previously
1167 current item.
1168*/
1169
1170/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator &QMap<Key, T>::const_iterator::operator--()
1171
1172 The prefix \c{--} operator (\c{--i}) makes the preceding item
1173 current and returns an iterator pointing to the new current item.
1174
1175 Calling this function on QMap::begin() leads to undefined
1176 results.
1177
1178 \sa operator++()
1179*/
1180
1181/*! \fn template <class Key, class T> QMap<Key, T>::const_iterator QMap<Key, T>::const_iterator::operator--(int)
1182
1183 \overload
1184
1185 The postfix \c{--} operator (\c{i--}) makes the preceding item
1186 current and returns an iterator pointing to the previously
1187 current item.
1188*/
1189
1190/*! //! friends
1191 \fn [qmap-op-it-plus-step-const] template <class Key, class T> QMap<Key, T>::const_iterator::operator+(QMap<Key, T>::const_iterator, difference_type n)
1192 \fn [qmap-op-step-plus-it-const] template <class Key, class T> QMap<Key, T>::operator+(difference_type n, QMap<Key, T>::const_iterator)
1193 \fn [qmap-op-it-minus-step-const] template <class Key, class T> QMap<Key, T>::operator-(QMap<Key, T>::const_iterator, difference_type n)
1194 \fn [qmap-op-step-minus-it-const] template <class Key, class T> QMap<Key, T>::operator-(difference_type n, QMap<Key, T>::const_iterator)
1195
1196 //! members
1197 \fn template <class Key, class T> typename QMap<Key, T>::const_iterator QMap<Key, T>::const_iterator::operator+=(QMap<Key, T>::const_iterator::difference_type n)
1198 \fn template <class Key, class T> typename QMap<Key, T>::const_iterator QMap<Key, T>::const_iterator::operator-=(QMap<Key, T>::const_iterator::difference_type n)
1199
1200 \deprecated [6.2] Use \c{std::next}, \c{std::prev} or \c{std::advance} instead.
1201
1202 Moves an iterator by \e{n} positions. These operations can be
1203 expensive for large values of \e{n}. QMap iterators are not
1204 random access.
1205*/
1206
1207/*! \class QMap::key_iterator
1208 \inmodule QtCore
1209 \since 5.6
1210 \brief The QMap::key_iterator class provides an STL-style const iterator for QMap keys.
1211
1212 QMap::key_iterator is essentially the same as QMap::const_iterator
1213 with the difference that operator*() and operator->() return a key
1214 instead of a value.
1215
1216 For most uses QMap::iterator and QMap::const_iterator should be used,
1217 you can easily access the key by calling QMap::iterator::key():
1218
1219 \snippet code/src_corelib_tools_qmap.cpp keyiterator1
1220
1221 However, to have interoperability between QMap's keys and STL-style
1222 algorithms we need an iterator that dereferences to a key instead
1223 of a value. With QMap::key_iterator we can apply an algorithm to a
1224 range of keys without having to call QMap::keys(), which is inefficient
1225 as it costs one QMap iteration and memory allocation to create a temporary
1226 QList.
1227
1228 \snippet code/src_corelib_tools_qmap.cpp keyiterator2
1229
1230 QMap::key_iterator is const, it's not possible to modify the key.
1231
1232 The default QMap::key_iterator constructor creates an uninitialized
1233 iterator. You must initialize it using a QMap function like
1234 QMap::keyBegin() or QMap::keyEnd().
1235
1236 \warning Iterators on implicitly shared containers do not work
1237 exactly like STL-iterators. You should avoid copying a container
1238 while iterators are active on that container. For more information,
1239 read \l{Implicit sharing iterator problem}.
1240
1241 \sa QMap::const_iterator, QMap::iterator
1242*/
1243
1244/*! \typedef QMap::key_iterator::difference_type
1245 \internal
1246*/
1247
1248/*! \typedef QMap::key_iterator::iterator_category
1249 \internal
1250*/
1251
1252/*! \typedef QMap::key_iterator::pointer
1253 \internal
1254*/
1255
1256/*! \typedef QMap::key_iterator::reference
1257 \internal
1258*/
1259
1260/*! \typedef QMap::key_iterator::value_type
1261 \internal
1262*/
1263
1264/*! \fn template <class Key, class T> const T &QMap<Key, T>::key_iterator::operator*() const
1265
1266 Returns the current item's key.
1267*/
1268
1269/*! \fn template <class Key, class T> const T *QMap<Key, T>::key_iterator::operator->() const
1270
1271 Returns a pointer to the current item's key.
1272*/
1273
1274/*! \fn template <class Key, class T> bool QMap<Key, T>::key_iterator::operator==(key_iterator other) const
1275
1276 Returns \c true if \a other points to the same item as this
1277 iterator; otherwise returns \c false.
1278
1279 \sa operator!=()
1280*/
1281
1282/*! \fn template <class Key, class T> bool QMap<Key, T>::key_iterator::operator!=(key_iterator other) const
1283
1284 Returns \c true if \a other points to a different item than this
1285 iterator; otherwise returns \c false.
1286
1287 \sa operator==()
1288*/
1289
1290/*!
1291 \fn template <class Key, class T> QMap<Key, T>::key_iterator &QMap<Key, T>::key_iterator::operator++()
1292
1293 The prefix \c{++} operator (\c{++i}) advances the iterator to the
1294 next item in the hash and returns an iterator to the new current
1295 item.
1296
1297 Calling this function on QMap::keyEnd() leads to undefined results.
1298
1299 \sa operator--()
1300*/
1301
1302/*! \fn template <class Key, class T> QMap<Key, T>::key_iterator QMap<Key, T>::key_iterator::operator++(int)
1303
1304 \overload
1305
1306 The postfix \c{++} operator (\c{i++}) advances the iterator to the
1307 next item in the hash and returns an iterator to the previous
1308 item.
1309*/
1310
1311/*! \fn template <class Key, class T> QMap<Key, T>::key_iterator &QMap<Key, T>::key_iterator::operator--()
1312
1313 The prefix \c{--} operator (\c{--i}) makes the preceding item
1314 current and returns an iterator pointing to the new current item.
1315
1316 Calling this function on QMap::keyBegin() leads to undefined
1317 results.
1318
1319 \sa operator++()
1320*/
1321
1322/*! \fn template <class Key, class T> QMap<Key, T>::key_iterator QMap<Key, T>::key_iterator::operator--(int)
1323
1324 \overload
1325
1326 The postfix \c{--} operator (\c{i--}) makes the preceding item
1327 current and returns an iterator pointing to the previous
1328 item.
1329*/
1330
1331/*! \fn template <class Key, class T> const_iterator QMap<Key, T>::key_iterator::base() const
1332 Returns the underlying const_iterator this key_iterator is based on.
1333*/
1334
1335/*! \typedef QMap::const_key_value_iterator
1336 \inmodule QtCore
1337 \since 5.10
1338 \brief The QMap::const_key_value_iterator typedef provides an STL-style iterator for QMap.
1339
1340 QMap::const_key_value_iterator is essentially the same as QMap::const_iterator
1341 with the difference that operator*() returns a key/value pair instead of a
1342 value.
1343
1344 \sa QKeyValueIterator
1345*/
1346
1347/*! \typedef QMap::key_value_iterator
1348 \inmodule QtCore
1349 \since 5.10
1350 \brief The QMap::key_value_iterator typedef provides an STL-style iterator for QMap.
1351
1352 QMap::key_value_iterator is essentially the same as QMap::iterator
1353 with the difference that operator*() returns a key/value pair instead of a
1354 value.
1355
1356 \sa QKeyValueIterator
1357*/
1358
1359/*! \fn template <class Key, class T> QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
1360 \relates QMap
1361
1362 Writes the map \a map to stream \a out.
1363
1364 This function requires the key and value types to implement \c
1365 operator<<().
1366
1367 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1368*/
1369
1370/*! \fn template <class Key, class T> QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
1371 \relates QMap
1372
1373 Reads a map from stream \a in into \a map.
1374
1375 This function requires the key and value types to implement \c
1376 operator>>().
1377
1378 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1379*/
1380
1381/*! \fn template <typename Key, typename T, typename Predicate> qsizetype erase_if(QMap<Key, T> &map, Predicate pred)
1382 \relates QMap
1383 \since 6.1
1384
1385 Removes all elements for which the predicate \a pred returns true
1386 from the map \a map.
1387
1388 The function supports predicates which take either an argument of
1389 type \c{QMap<Key, T>::iterator}, or an argument of type
1390 \c{std::pair<const Key &, T &>}.
1391
1392 Returns the number of elements removed, if any.
1393*/