libstdc++
stl_map.h
Go to the documentation of this file.
1// Map implementation -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_map.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{map}
54 */
55
56#ifndef _STL_MAP_H
57#define _STL_MAP_H 1
58
60#include <bits/concept_check.h>
61#if __cplusplus >= 201103L
62#include <initializer_list>
63#include <tuple>
64#endif
65#if __glibcxx_containers_ranges // C++ >= 23
66# include <bits/ranges_base.h> // ranges::begin, ranges::distance etc.
67#endif
68// #include <stl_tree.h> // done in std/map
69
70namespace std _GLIBCXX_VISIBILITY(default)
71{
72_GLIBCXX_BEGIN_NAMESPACE_VERSION
73_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
74
75 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
76 class multimap;
77
78 /**
79 * @brief A standard container made up of (key,value) pairs, which can be
80 * retrieved based on a key, in logarithmic time.
81 *
82 * @ingroup associative_containers
83 * @headerfile map
84 * @since C++98
85 *
86 * @tparam _Key Type of key objects.
87 * @tparam _Tp Type of mapped objects.
88 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
89 * @tparam _Alloc Allocator type, defaults to
90 * allocator<pair<const _Key, _Tp>.
91 *
92 * Meets the requirements of a <a href="tables.html#65">container</a>, a
93 * <a href="tables.html#66">reversible container</a>, and an
94 * <a href="tables.html#69">associative container</a> (using unique keys).
95 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
96 * value_type is std::pair<const Key,T>.
97 *
98 * Maps support bidirectional iterators.
99 *
100 * The private tree data is declared exactly the same way for map and
101 * multimap; the distinction is made entirely in how the tree functions are
102 * called (*_unique versus *_equal, same as the standard).
103 */
104 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
105 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
106 class map
107 {
108 public:
109 typedef _Key key_type;
110 typedef _Tp mapped_type;
111 typedef std::pair<const _Key, _Tp> value_type;
112 typedef _Compare key_compare;
113 typedef _Alloc allocator_type;
114
115 private:
116#ifdef _GLIBCXX_CONCEPT_CHECKS
117 // concept requirements
118 typedef typename _Alloc::value_type _Alloc_value_type;
119# if __cplusplus < 201103L
120 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
121# endif
122 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
123 _BinaryFunctionConcept)
124 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
125#endif
126
127#if __cplusplus >= 201103L
128#if __cplusplus > 201703L || defined __STRICT_ANSI__
130 "std::map must have the same value_type as its allocator");
131#endif
132#endif
133
134 public:
135#pragma GCC diagnostic push
136#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
137 class value_compare
138 : public std::binary_function<value_type, value_type, bool>
139 {
140 friend class map<_Key, _Tp, _Compare, _Alloc>;
141 protected:
142 _Compare comp;
143
144 value_compare(_Compare __c)
145 : comp(__c) { }
146
147 public:
148 bool operator()(const value_type& __x, const value_type& __y) const
149 { return comp(__x.first, __y.first); }
150 };
151#pragma GCC diagnostic pop
152
153 private:
154 /// This turns a red-black tree into a [multi]map.
156 rebind<value_type>::other _Pair_alloc_type;
157
158 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
159 key_compare, _Pair_alloc_type> _Rep_type;
160
161 /// The actual tree structure.
162 _Rep_type _M_t;
163
165
166#if __cplusplus >= 201703L
167 template<typename _Up, typename _Vp = remove_reference_t<_Up>>
168 static constexpr bool __usable_key
169 = __or_v<is_same<const _Vp, const _Key>,
170 __and_<is_scalar<_Vp>, is_scalar<_Key>>>;
171#endif
172
173 public:
174 // many of these are specified differently in ISO, but the following are
175 // "functionally equivalent"
176 typedef typename _Alloc_traits::pointer pointer;
177 typedef typename _Alloc_traits::const_pointer const_pointer;
178 typedef typename _Alloc_traits::reference reference;
179 typedef typename _Alloc_traits::const_reference const_reference;
180 typedef typename _Rep_type::iterator iterator;
181 typedef typename _Rep_type::const_iterator const_iterator;
182 typedef typename _Rep_type::size_type size_type;
183 typedef typename _Rep_type::difference_type difference_type;
184 typedef typename _Rep_type::reverse_iterator reverse_iterator;
185 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
186
187#ifdef __glibcxx_node_extract // >= C++17
188 using node_type = typename _Rep_type::node_type;
189 using insert_return_type = typename _Rep_type::insert_return_type;
190#endif
191
192 // [23.3.1.1] construct/copy/destroy
193 // (get_allocator() is also listed in this section)
194
195 /**
196 * @brief Default constructor creates no elements.
197 */
198#if __cplusplus < 201103L
199 map() : _M_t() { }
200#else
201 map() = default;
202#endif
203
204 /**
205 * @brief Creates a %map with no elements.
206 * @param __comp A comparison object.
207 * @param __a An allocator object.
208 */
209 explicit
210 map(const _Compare& __comp,
211 const allocator_type& __a = allocator_type())
212 : _M_t(__comp, _Pair_alloc_type(__a)) { }
213
214 /**
215 * @brief %Map copy constructor.
216 *
217 * Whether the allocator is copied depends on the allocator traits.
218 */
219#if __cplusplus < 201103L
220 map(const map& __x)
221 : _M_t(__x._M_t) { }
222#else
223 map(const map&) = default;
224
225 /**
226 * @brief %Map move constructor.
227 *
228 * The newly-created %map contains the exact contents of the moved
229 * instance. The moved instance is a valid, but unspecified, %map.
230 */
231 map(map&&) = default;
232
233 /**
234 * @brief Builds a %map from an initializer_list.
235 * @param __l An initializer_list.
236 * @param __comp A comparison object.
237 * @param __a An allocator object.
238 *
239 * Create a %map consisting of copies of the elements in the
240 * initializer_list @a __l.
241 * This is linear in N if the range is already sorted, and NlogN
242 * otherwise (where N is @a __l.size()).
243 */
245 const _Compare& __comp = _Compare(),
246 const allocator_type& __a = allocator_type())
247 : _M_t(__comp, _Pair_alloc_type(__a))
248 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
249
250 /// Allocator-extended default constructor.
251 explicit
252 map(const allocator_type& __a)
253 : _M_t(_Pair_alloc_type(__a)) { }
254
255 /// Allocator-extended copy constructor.
256 map(const map& __m, const __type_identity_t<allocator_type>& __a)
257 : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
258
259 /// Allocator-extended move constructor.
260 map(map&& __m, const __type_identity_t<allocator_type>& __a)
262 && _Alloc_traits::_S_always_equal())
263 : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
264
265 /// Allocator-extended initialier-list constructor.
266 map(initializer_list<value_type> __l, const allocator_type& __a)
267 : _M_t(_Pair_alloc_type(__a))
268 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
269
270 /// Allocator-extended range constructor.
271 template<typename _InputIterator>
272 map(_InputIterator __first, _InputIterator __last,
273 const allocator_type& __a)
274 : _M_t(_Pair_alloc_type(__a))
275 { _M_t._M_insert_range_unique(__first, __last); }
276#endif
277
278 /**
279 * @brief Builds a %map from a range.
280 * @param __first An input iterator.
281 * @param __last An input iterator.
282 *
283 * Create a %map consisting of copies of the elements from
284 * [__first,__last). This is linear in N if the range is
285 * already sorted, and NlogN otherwise (where N is
286 * distance(__first,__last)).
287 */
288 template<typename _InputIterator>
289 map(_InputIterator __first, _InputIterator __last)
290 : _M_t()
291 { _M_t._M_insert_range_unique(__first, __last); }
292
293 /**
294 * @brief Builds a %map from a range.
295 * @param __first An input iterator.
296 * @param __last An input iterator.
297 * @param __comp A comparison functor.
298 * @param __a An allocator object.
299 *
300 * Create a %map consisting of copies of the elements from
301 * [__first,__last). This is linear in N if the range is
302 * already sorted, and NlogN otherwise (where N is
303 * distance(__first,__last)).
304 */
305 template<typename _InputIterator>
306 map(_InputIterator __first, _InputIterator __last,
307 const _Compare& __comp,
308 const allocator_type& __a = allocator_type())
309 : _M_t(__comp, _Pair_alloc_type(__a))
310 { _M_t._M_insert_range_unique(__first, __last); }
311
312#if __glibcxx_containers_ranges // C++ >= 23
313 /**
314 * @brief Builds a %map from a range.
315 * @since C++23
316 */
317 template<__detail::__container_compatible_range<value_type> _Rg>
318 map(from_range_t, _Rg&& __rg,
319 const _Compare& __comp,
320 const _Alloc& __a = _Alloc())
321 : _M_t(__comp, _Pair_alloc_type(__a))
322 { insert_range(std::forward<_Rg>(__rg)); }
323
324 /// Allocator-extended range constructor.
325 template<__detail::__container_compatible_range<value_type> _Rg>
326 map(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
327 : _M_t(_Pair_alloc_type(__a))
328 { insert_range(std::forward<_Rg>(__rg)); }
329#endif
330
331
332#if __cplusplus >= 201103L
333 /**
334 * The dtor only erases the elements, and note that if the elements
335 * themselves are pointers, the pointed-to memory is not touched in any
336 * way. Managing the pointer is the user's responsibility.
337 */
338 ~map() = default;
339#endif
340
341 /**
342 * @brief %Map assignment operator.
343 *
344 * Whether the allocator is copied depends on the allocator traits.
345 */
346#if __cplusplus < 201103L
347 map&
348 operator=(const map& __x)
349 {
350 _M_t = __x._M_t;
351 return *this;
352 }
353#else
354 map&
355 operator=(const map&) = default;
356
357 /// Move assignment operator.
358 map&
359 operator=(map&&) = default;
360
361 /**
362 * @brief %Map list assignment operator.
363 * @param __l An initializer_list.
364 *
365 * This function fills a %map with copies of the elements in the
366 * initializer list @a __l.
367 *
368 * Note that the assignment completely changes the %map and
369 * that the resulting %map's size is the same as the number
370 * of elements assigned.
371 */
372 map&
374 {
375 _M_t._M_assign_unique(__l.begin(), __l.end());
376 return *this;
377 }
378#endif
379
380 /// Get a copy of the memory allocation object.
381 allocator_type
382 get_allocator() const _GLIBCXX_NOEXCEPT
383 { return allocator_type(_M_t.get_allocator()); }
384
385 // iterators
386 /**
387 * Returns a read/write iterator that points to the first pair in the
388 * %map.
389 * Iteration is done in ascending order according to the keys.
390 */
392 begin() _GLIBCXX_NOEXCEPT
393 { return _M_t.begin(); }
394
395 /**
396 * Returns a read-only (constant) iterator that points to the first pair
397 * in the %map. Iteration is done in ascending order according to the
398 * keys.
399 */
400 const_iterator
401 begin() const _GLIBCXX_NOEXCEPT
402 { return _M_t.begin(); }
403
404 /**
405 * Returns a read/write iterator that points one past the last
406 * pair in the %map. Iteration is done in ascending order
407 * according to the keys.
408 */
410 end() _GLIBCXX_NOEXCEPT
411 { return _M_t.end(); }
412
413 /**
414 * Returns a read-only (constant) iterator that points one past the last
415 * pair in the %map. Iteration is done in ascending order according to
416 * the keys.
417 */
418 const_iterator
419 end() const _GLIBCXX_NOEXCEPT
420 { return _M_t.end(); }
421
422 /**
423 * Returns a read/write reverse iterator that points to the last pair in
424 * the %map. Iteration is done in descending order according to the
425 * keys.
426 */
428 rbegin() _GLIBCXX_NOEXCEPT
429 { return _M_t.rbegin(); }
430
431 /**
432 * Returns a read-only (constant) reverse iterator that points to the
433 * last pair in the %map. Iteration is done in descending order
434 * according to the keys.
435 */
436 const_reverse_iterator
437 rbegin() const _GLIBCXX_NOEXCEPT
438 { return _M_t.rbegin(); }
439
440 /**
441 * Returns a read/write reverse iterator that points to one before the
442 * first pair in the %map. Iteration is done in descending order
443 * according to the keys.
444 */
446 rend() _GLIBCXX_NOEXCEPT
447 { return _M_t.rend(); }
448
449 /**
450 * Returns a read-only (constant) reverse iterator that points to one
451 * before the first pair in the %map. Iteration is done in descending
452 * order according to the keys.
453 */
454 const_reverse_iterator
455 rend() const _GLIBCXX_NOEXCEPT
456 { return _M_t.rend(); }
457
458#if __cplusplus >= 201103L
459 /**
460 * Returns a read-only (constant) iterator that points to the first pair
461 * in the %map. Iteration is done in ascending order according to the
462 * keys.
463 */
464 const_iterator
465 cbegin() const noexcept
466 { return _M_t.begin(); }
467
468 /**
469 * Returns a read-only (constant) iterator that points one past the last
470 * pair in the %map. Iteration is done in ascending order according to
471 * the keys.
472 */
473 const_iterator
474 cend() const noexcept
475 { return _M_t.end(); }
476
477 /**
478 * Returns a read-only (constant) reverse iterator that points to the
479 * last pair in the %map. Iteration is done in descending order
480 * according to the keys.
481 */
482 const_reverse_iterator
483 crbegin() const noexcept
484 { return _M_t.rbegin(); }
485
486 /**
487 * Returns a read-only (constant) reverse iterator that points to one
488 * before the first pair in the %map. Iteration is done in descending
489 * order according to the keys.
490 */
491 const_reverse_iterator
492 crend() const noexcept
493 { return _M_t.rend(); }
494#endif
495
496 // capacity
497 /** Returns true if the %map is empty. (Thus begin() would equal
498 * end().)
499 */
500 _GLIBCXX_NODISCARD bool
501 empty() const _GLIBCXX_NOEXCEPT
502 { return _M_t.empty(); }
503
504 /** Returns the size of the %map. */
506 size() const _GLIBCXX_NOEXCEPT
507 { return _M_t.size(); }
508
509 /** Returns the maximum size of the %map. */
511 max_size() const _GLIBCXX_NOEXCEPT
512 { return _M_t.max_size(); }
513
514 // [23.3.1.2] element access
515 /**
516 * @brief Subscript ( @c [] ) access to %map data.
517 * @param __k The key for which data should be retrieved.
518 * @return A reference to the data of the (key,data) %pair.
519 *
520 * Allows for easy lookup with the subscript ( @c [] )
521 * operator. Returns data associated with the key specified in
522 * subscript. If the key does not exist, a pair with that key
523 * is created using default values, which is then returned.
524 *
525 * If a heterogeneous key matches a range of elements, the first is
526 * chosen.
527 *
528 * Lookup requires logarithmic time.
529 */
530 mapped_type&
531 operator[](const key_type& __k)
532 {
533 // concept requirements
534 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
535
536 iterator __i = lower_bound(__k);
537 // __i->first is greater than or equivalent to __k.
538 if (__i == end() || key_comp()(__k, (*__i).first))
539#if __cplusplus >= 201103L
540 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
542 std::tuple<>());
543#else
544 __i = insert(__i, value_type(__k, mapped_type()));
545#endif
546 return (*__i).second;
547 }
548
549#if __cplusplus >= 201103L
550 mapped_type&
551 operator[](key_type&& __k)
552 {
553 // concept requirements
554 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
555
556 iterator __i = lower_bound(__k);
557 // __i->first is greater than or equivalent to __k.
558 if (__i == end() || key_comp()(__k, (*__i).first))
559 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
561 std::tuple<>());
562 return (*__i).second;
563 }
564#endif
565
566#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
567 template <__heterogeneous_tree_key<map> _Kt>
568 mapped_type&
569 operator[](_Kt&& __k)
570 { return try_emplace(std::forward<_Kt>(__k)).first->second; }
571#endif
572
573 // _GLIBCXX_RESOLVE_LIB_DEFECTS
574 // DR 464. Suggestion for new member functions in standard containers.
575 /**
576 * @brief Access to %map data.
577 * @param __k The key for which data should be retrieved.
578 * @return A reference to the data whose key is equivalent to @a __k, if
579 * such a data is present in the %map.
580 * @throw std::out_of_range If no such data is present.
581 *
582 * If a heterogeneous key __k matches a range of elements, the
583 * first is chosen.
584 */
585 mapped_type&
586 at(const key_type& __k)
587 {
588 iterator __i = lower_bound(__k);
589 if (__i == end() || key_comp()(__k, (*__i).first))
590 __throw_out_of_range(__N("map::at"));
591 return (*__i).second;
592 }
593
594#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
595 template <__heterogeneous_tree_key<map> _Kt>
596 mapped_type&
597 at(const _Kt& __k)
598 {
599 iterator __i = lower_bound(__k);
600 if (__i == end() || key_comp()(__k, (*__i).first))
601 __throw_out_of_range(__N("map::at"));
602 return (*__i).second;
603 }
604#endif
605
606 const mapped_type&
607 at(const key_type& __k) const
608 {
609 const_iterator __i = lower_bound(__k);
610 if (__i == end() || key_comp()(__k, (*__i).first))
611 __throw_out_of_range(__N("map::at"));
612 return (*__i).second;
613 }
614
615#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
616 template <__heterogeneous_tree_key<map> _Kt>
617 const mapped_type&
618 at(const _Kt& __k) const
619 {
620 const_iterator __i = lower_bound(__k);
621 if (__i == end() || key_comp()(__k, (*__i).first))
622 __throw_out_of_range(__N("map::at"));
623 return (*__i).second;
624 }
625#endif
626
627 // modifiers
628#if __cplusplus >= 201103L
629 /**
630 * @brief Attempts to build and insert a std::pair into the %map.
631 *
632 * @param __args Arguments used to generate a new pair instance (see
633 * std::piecewise_contruct for passing arguments to each
634 * part of the pair constructor).
635 *
636 * @return A pair, of which the first element is an iterator that points
637 * to the possibly inserted pair, and the second is a bool that
638 * is true if the pair was actually inserted.
639 *
640 * This function attempts to build and insert a (key, value) %pair into
641 * the %map.
642 * A %map relies on unique keys and thus a %pair is only inserted if its
643 * first element (the key) is not already present in the %map.
644 *
645 * Insertion requires logarithmic time.
646 */
647 template<typename... _Args>
649 emplace(_Args&&... __args)
650 {
651#if __cplusplus >= 201703L
652 if constexpr (sizeof...(_Args) == 2)
653 if constexpr (is_same_v<allocator_type, allocator<value_type>>)
654 {
655 auto&& [__a, __v] = pair<_Args&...>(__args...);
656 if constexpr (__usable_key<decltype(__a)>)
657 {
658 const key_type& __k = __a;
659 iterator __i = lower_bound(__k);
660 if (__i == end() || key_comp()(__k, (*__i).first))
661 {
662 __i = emplace_hint(__i, std::forward<_Args>(__args)...);
663 return {__i, true};
664 }
665 return {__i, false};
666 }
667 }
668#endif
669 return _M_t._M_emplace_unique(std::forward<_Args>(__args)...);
670 }
671
672 /**
673 * @brief Attempts to build and insert a std::pair into the %map.
674 *
675 * @param __pos An iterator that serves as a hint as to where the pair
676 * should be inserted.
677 * @param __args Arguments used to generate a new pair instance (see
678 * std::piecewise_contruct for passing arguments to each
679 * part of the pair constructor).
680 * @return An iterator that points to the element with key of the
681 * std::pair built from @a __args (may or may not be that
682 * std::pair).
683 *
684 * This function is not concerned about whether the insertion took place,
685 * and thus does not return a boolean like the single-argument emplace()
686 * does.
687 * Note that the first parameter is only a hint and can potentially
688 * improve the performance of the insertion process. A bad hint would
689 * cause no gains in efficiency.
690 *
691 * See
692 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
693 * for more on @a hinting.
694 *
695 * Insertion requires logarithmic time (if the hint is not taken).
696 */
697 template<typename... _Args>
699 emplace_hint(const_iterator __pos, _Args&&... __args)
700 {
701 return _M_t._M_emplace_hint_unique(__pos,
702 std::forward<_Args>(__args)...);
703 }
704#endif
705
706#ifdef __glibcxx_node_extract // >= C++17
707 /// Extract a node.
708 node_type
709 extract(const_iterator __pos)
710 {
711 __glibcxx_assert(__pos != end());
712 return _M_t.extract(__pos);
713 }
714
715 /// Extract a node.
716 node_type
717 extract(const key_type& __x)
718 { return _M_t.extract(__x); }
719
720#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
721 template <__heterogeneous_tree_key<map> _Kt>
722 node_type
723 extract(_Kt&& __key)
724 { return _M_t._M_extract_tr(__key); }
725#endif
726
727 /// Re-insert an extracted node.
728 insert_return_type
729 insert(node_type&& __nh)
730 { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
731
732 /// Re-insert an extracted node.
733 iterator
734 insert(const_iterator __hint, node_type&& __nh)
735 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
736
737 template<typename, typename>
738 friend struct std::_Rb_tree_merge_helper;
739
740 template<typename _Cmp2>
741 void
742 merge(map<_Key, _Tp, _Cmp2, _Alloc>& __source)
743 {
744 using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
745 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
746 }
747
748 template<typename _Cmp2>
749 void
750 merge(map<_Key, _Tp, _Cmp2, _Alloc>&& __source)
751 { merge(__source); }
752
753 template<typename _Cmp2>
754 void
755 merge(multimap<_Key, _Tp, _Cmp2, _Alloc>& __source)
756 {
757 using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
758 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
759 }
760
761 template<typename _Cmp2>
762 void
763 merge(multimap<_Key, _Tp, _Cmp2, _Alloc>&& __source)
764 { merge(__source); }
765#endif // C++17
766
767#ifdef __glibcxx_map_try_emplace // C++ >= 17 && HOSTED
768 /**
769 * @brief Attempts to build and insert a std::pair into the %map.
770 *
771 * @param __k Key to use for finding a possibly existing pair in
772 * the map.
773 * @param __args Arguments used to generate the .second for a new pair
774 * instance.
775 *
776 * @return A pair, of which the first element is an iterator that points
777 * to the possibly inserted pair, and the second is a bool that
778 * is true if the pair was actually inserted.
779 *
780 * This function attempts to build and insert a (key, value) %pair into
781 * the %map.
782 * A %map relies on unique keys and thus a %pair is only inserted if its
783 * first element (the key) is not already present in the %map.
784 * If a %pair is not inserted, this function has no effect.
785 *
786 * If a heterogeneous key __k matches a range of elements, an iterator
787 * to the first is returned.
788 *
789 * Insertion requires logarithmic time.
790 */
791 template <typename... _Args>
793 try_emplace(const key_type& __k, _Args&&... __args)
794 {
795 iterator __i = lower_bound(__k);
796 if (__i == end() || key_comp()(__k, (*__i).first))
797 {
801 std::forward<_Args>(__args)...));
802 return {__i, true};
803 }
804 return {__i, false};
805 }
806
807 // move-capable overload
808 template <typename... _Args>
810 try_emplace(key_type&& __k, _Args&&... __args)
811 {
812 iterator __i = lower_bound(__k);
813 if (__i == end() || key_comp()(__k, (*__i).first))
814 {
818 std::forward<_Args>(__args)...));
819 return {__i, true};
820 }
821 return {__i, false};
822 }
823
824#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
825 template <__heterogeneous_tree_key<map> _Kt, typename ..._Args>
827 try_emplace(_Kt&& __k, _Args&&... __args)
828 {
829 iterator __i;
830 auto [__left, __node] = _M_t._M_get_insert_unique_pos_tr(__k);
831 if (__node)
832 {
833 __i = _M_t._M_emplace_here(__left == __node, __node,
837 return { __i, true };
838 }
839 __i = iterator(__left);
840 return { __i, false };
841 }
842#endif
843
844 /**
845 * @brief Attempts to build and insert a std::pair into the %map.
846 *
847 * @param __hint An iterator that serves as a hint as to where the
848 * pair should be inserted.
849 * @param __k Key to use for finding a possibly existing pair in
850 * the map.
851 * @param __args Arguments used to generate the .second for a new pair
852 * instance.
853 * @return An iterator that points to the element with key of the
854 * std::pair built from @a __args (may or may not be that
855 * std::pair).
856 *
857 * This function is not concerned about whether the insertion took place,
858 * and thus does not return a boolean like the single-argument
859 * try_emplace() does. However, if insertion did not take place,
860 * this function has no effect.
861 * Note that the first parameter is only a hint and can potentially
862 * improve the performance of the insertion process. A bad hint would
863 * cause no gains in efficiency.
864 *
865 * See
866 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
867 * for more on @a hinting.
868 *
869 * If a heterogeneous key __k matches a range of elements, an iterator
870 * to the first is returned.
871 *
872 * Insertion requires logarithmic time (if the hint is not taken).
873 */
874 template <typename... _Args>
875 iterator
876 try_emplace(const_iterator __hint, const key_type& __k,
877 _Args&&... __args)
878 {
879 iterator __i;
880 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
881 if (__true_hint.second)
882 __i = emplace_hint(iterator(__true_hint.second),
886 std::forward<_Args>(__args)...));
887 else
888 __i = iterator(__true_hint.first);
889 return __i;
890 }
891
892 // move-capable overload
893 template <typename... _Args>
894 iterator
895 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
896 {
897 iterator __i;
898 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
899 if (__true_hint.second)
900 __i = emplace_hint(iterator(__true_hint.second),
904 std::forward<_Args>(__args)...));
905 else
906 __i = iterator(__true_hint.first);
907 return __i;
908 }
909#endif
910
911#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
912 template <__heterogeneous_tree_key<map> _Kt, typename ..._Args>
913 iterator
914 try_emplace(const_iterator __hint, _Kt&& __k, _Args&&... __args)
915 {
916 iterator __i;
917 auto [__left, __node] =
918 _M_t._M_get_insert_hint_unique_pos_tr(__hint, __k);
919 if (__node)
920 {
921 __i = _M_t._M_emplace_here(__left == __node, __node,
925 }
926 else __i = iterator(__left);
927 return __i;
928 }
929#endif
930
931 /**
932 * @brief Attempts to insert a std::pair into the %map.
933 * @param __x Pair to be inserted (see std::make_pair for easy
934 * creation of pairs).
935 *
936 * @return A pair, of which the first element is an iterator that
937 * points to the possibly inserted pair, and the second is
938 * a bool that is true if the pair was actually inserted.
939 *
940 * This function attempts to insert a (key, value) %pair into the %map.
941 * A %map relies on unique keys and thus a %pair is only inserted if its
942 * first element (the key) is not already present in the %map.
943 *
944 * Insertion requires logarithmic time.
945 * @{
946 */
948 insert(const value_type& __x)
949 { return _M_t._M_insert_unique(__x); }
950
951#if __cplusplus >= 201103L
952 // _GLIBCXX_RESOLVE_LIB_DEFECTS
953 // 2354. Unnecessary copying when inserting into maps with braced-init
955 insert(value_type&& __x)
956 { return _M_t._M_insert_unique(std::move(__x)); }
957
958 template<typename _Pair>
959 __enable_if_t<is_constructible<value_type, _Pair>::value,
961 insert(_Pair&& __x)
962 {
963#if __cplusplus >= 201703L
964 using _P2 = remove_reference_t<_Pair>;
965 if constexpr (__is_pair<remove_const_t<_P2>>)
966 if constexpr (is_same_v<allocator_type, allocator<value_type>>)
967 if constexpr (__usable_key<typename _P2::first_type>)
968 {
969 const key_type& __k = __x.first;
970 iterator __i = lower_bound(__k);
971 if (__i == end() || key_comp()(__k, (*__i).first))
972 {
973 __i = emplace_hint(__i, std::forward<_Pair>(__x));
974 return {__i, true};
975 }
976 return {__i, false};
977 }
978#endif
979 return _M_t._M_emplace_unique(std::forward<_Pair>(__x));
980 }
981#endif
982 ///@}
983
984#if __cplusplus >= 201103L
985 /**
986 * @brief Attempts to insert a list of std::pairs into the %map.
987 * @param __list A std::initializer_list<value_type> of pairs to be
988 * inserted.
989 *
990 * Complexity similar to that of the range constructor.
991 */
992 void
994 { insert(__list.begin(), __list.end()); }
995#endif
996
997#if __glibcxx_containers_ranges // C++ >= 23
998 /**
999 * @brief Inserts a range of elements.
1000 * @since C++23
1001 * @param __rg An input range of elements that can be converted to
1002 * the map's value type.
1003 */
1004 template<__detail::__container_compatible_range<value_type> _Rg>
1005 void
1006 insert_range(_Rg&& __rg)
1007 {
1008 auto __first = ranges::begin(__rg);
1009 const auto __last = ranges::end(__rg);
1010 for (; __first != __last; ++__first)
1011 insert(*__first);
1012 }
1013#endif
1014
1015 /**
1016 * @brief Attempts to insert a std::pair into the %map.
1017 * @param __position An iterator that serves as a hint as to where the
1018 * pair should be inserted.
1019 * @param __x Pair to be inserted (see std::make_pair for easy creation
1020 * of pairs).
1021 * @return An iterator that points to the element with key of
1022 * @a __x (may or may not be the %pair passed in).
1023 *
1024
1025 * This function is not concerned about whether the insertion
1026 * took place, and thus does not return a boolean like the
1027 * single-argument insert() does. Note that the first
1028 * parameter is only a hint and can potentially improve the
1029 * performance of the insertion process. A bad hint would
1030 * cause no gains in efficiency.
1031 *
1032 * See
1033 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1034 * for more on @a hinting.
1035 *
1036 * Insertion requires logarithmic time (if the hint is not taken).
1037 * @{
1038 */
1039 iterator
1040#if __cplusplus >= 201103L
1041 insert(const_iterator __position, const value_type& __x)
1042#else
1043 insert(iterator __position, const value_type& __x)
1044#endif
1045 { return _M_t._M_insert_unique_(__position, __x); }
1046
1047#if __cplusplus >= 201103L
1048 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1049 // 2354. Unnecessary copying when inserting into maps with braced-init
1050 iterator
1051 insert(const_iterator __position, value_type&& __x)
1052 { return _M_t._M_insert_unique_(__position, std::move(__x)); }
1053
1054 template<typename _Pair>
1055 __enable_if_t<is_constructible<value_type, _Pair>::value, iterator>
1056 insert(const_iterator __position, _Pair&& __x)
1057 {
1058 return _M_t._M_emplace_hint_unique(__position,
1059 std::forward<_Pair>(__x));
1060 }
1061#endif
1062 /// @}
1063
1064 /**
1065 * @brief Template function that attempts to insert a range of elements.
1066 * @param __first Iterator pointing to the start of the range to be
1067 * inserted.
1068 * @param __last Iterator pointing to the end of the range.
1069 *
1070 * Complexity similar to that of the range constructor.
1071 */
1072 template<typename _InputIterator>
1073 void
1074 insert(_InputIterator __first, _InputIterator __last)
1075 { _M_t._M_insert_range_unique(__first, __last); }
1076
1077#if __cplusplus > 201402L
1078 /**
1079 * @brief Attempts to insert or assign a std::pair into the %map.
1080 * @param __k Key to use for finding a possibly existing pair in
1081 * the map.
1082 * @param __obj Argument used to generate the .second for a pair
1083 * instance.
1084 *
1085 * @return A pair, of which the first element is an iterator that
1086 * points to the possibly inserted pair, and the second is
1087 * a bool that is true if the pair was actually inserted.
1088 *
1089 * This function attempts to insert a (key, value) %pair into the %map.
1090 * A %map relies on unique keys and thus a %pair is only inserted if its
1091 * first element (the key) is not already present in the %map.
1092 * If the %pair was already in the %map, the .second of the %pair
1093 * is assigned from __obj.
1094 *
1095 * Insertion requires logarithmic time.
1096 */
1097 template <typename _Obj>
1099 insert_or_assign(const key_type& __k, _Obj&& __obj)
1100 {
1101 iterator __i = lower_bound(__k);
1102 if (__i == end() || key_comp()(__k, (*__i).first))
1103 {
1107 std::forward<_Obj>(__obj)));
1108 return {__i, true};
1109 }
1110 (*__i).second = std::forward<_Obj>(__obj);
1111 return {__i, false};
1112 }
1113
1114 // move-capable overload
1115 template <typename _Obj>
1117 insert_or_assign(key_type&& __k, _Obj&& __obj)
1118 {
1119 iterator __i = lower_bound(__k);
1120 if (__i == end() || key_comp()(__k, (*__i).first))
1121 {
1125 std::forward<_Obj>(__obj)));
1126 return {__i, true};
1127 }
1128 (*__i).second = std::forward<_Obj>(__obj);
1129 return {__i, false};
1130 }
1131
1132#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
1133 template <__heterogeneous_tree_key<map> _Kt, typename _Obj>
1135 insert_or_assign(_Kt&& __k, _Obj&& __obj)
1136 {
1137 iterator __i;
1138 auto [__left, __node] =_M_t._M_get_insert_unique_pos_tr(__k);
1139 if (__node)
1140 {
1141 __i = _M_t._M_emplace_here(__left == __node, __node,
1145 return { __i, true };
1146 }
1147 __i = iterator(__left);
1148 (*__i).second = std::forward<_Obj>(__obj);
1149 return { __i, false };
1150 }
1151#endif
1152 ///@}
1153#endif
1154
1155#if __cplusplus > 201402L
1156 ///@{
1157 /**
1158 * @brief Attempts to insert or assign a std::pair into the %map.
1159 * @param __hint An iterator that serves as a hint as to where the
1160 * pair should be inserted.
1161 * @param __k Key to use for finding a possibly existing pair in
1162 * the map.
1163 * @param __obj Argument used to generate the .second for a pair
1164 * instance.
1165 *
1166 * @return An iterator that points to the element with key of
1167 * @a __x (may or may not be the %pair passed in).
1168 *
1169 * This function attempts to insert a (key, value) %pair into the %map.
1170 * A %map relies on unique keys and thus a %pair is only inserted if its
1171 * first element (the key) is not already present in the %map.
1172 * If the %pair was already in the %map, the .second of the %pair
1173 * is assigned from __obj.
1174 *
1175 * If a heterogeneous key __k matches a range of elements, the first
1176 * is chosen.
1177 *
1178 * Insertion requires logarithmic time.
1179 */
1180 template <typename _Obj>
1181 iterator
1182 insert_or_assign(const_iterator __hint,
1183 const key_type& __k, _Obj&& __obj)
1184 {
1185 iterator __i;
1186 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
1187 if (__true_hint.second)
1188 {
1189 return emplace_hint(iterator(__true_hint.second),
1193 std::forward<_Obj>(__obj)));
1194 }
1195 __i = iterator(__true_hint.first);
1196 (*__i).second = std::forward<_Obj>(__obj);
1197 return __i;
1198 }
1199
1200 // move-capable overload
1201 template <typename _Obj>
1202 iterator
1203 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
1204 {
1205 iterator __i;
1206 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
1207 if (__true_hint.second)
1208 {
1209 return emplace_hint(iterator(__true_hint.second),
1213 std::forward<_Obj>(__obj)));
1214 }
1215 __i = iterator(__true_hint.first);
1216 (*__i).second = std::forward<_Obj>(__obj);
1217 return __i;
1218 }
1219
1220#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
1221 template <__heterogeneous_tree_key<map> _Kt, typename _Obj>
1222 iterator
1223 insert_or_assign(const_iterator __hint, _Kt&& __k, _Obj&& __obj)
1224 {
1225 iterator __i;
1226 auto [__left, __node] =
1227 _M_t._M_get_insert_hint_unique_pos_tr(__hint, __k);
1228 if (__node)
1229 {
1230 return _M_t._M_emplace_here(__left == __node, __node,
1234 }
1235 __i = iterator(__left);
1236 (*__i).second = std::forward<_Obj>(__obj);
1237 return __i;
1238 }
1239#endif
1240 ///@}
1241#endif
1242
1243#if __cplusplus >= 201103L
1244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1245 // DR 130. Associative erase should return an iterator.
1246 /**
1247 * @brief Erases an element from a %map.
1248 * @param __position An iterator pointing to the element to be erased.
1249 * @return An iterator pointing to the element immediately following
1250 * @a position prior to the element being erased. If no such
1251 * element exists, end() is returned.
1252 *
1253 * This function erases an element, pointed to by the given
1254 * iterator, from a %map. Note that this function only erases
1255 * the element, and that if the element is itself a pointer,
1256 * the pointed-to memory is not touched in any way. Managing
1257 * the pointer is the user's responsibility.
1258 *
1259 * @{
1260 */
1261 iterator
1262 erase(const_iterator __position)
1263 { return _M_t.erase(__position); }
1264
1265 // LWG 2059
1266 _GLIBCXX_ABI_TAG_CXX11
1267 iterator
1268 erase(iterator __position)
1269 { return _M_t.erase(__position); }
1270 /// @}
1271#else
1272 /**
1273 * @brief Erases an element from a %map.
1274 * @param __position An iterator pointing to the element to be erased.
1275 *
1276 * This function erases an element, pointed to by the given
1277 * iterator, from a %map. Note that this function only erases
1278 * the element, and that if the element is itself a pointer,
1279 * the pointed-to memory is not touched in any way. Managing
1280 * the pointer is the user's responsibility.
1281 */
1282 void
1283 erase(iterator __position)
1284 { _M_t.erase(__position); }
1285#endif
1286
1287 ///@{
1288 /**
1289 * @brief Erases elements according to the provided key.
1290 * @param __x Key of element to be erased.
1291 * @return The number of elements erased.
1292 *
1293 * This function erases all the elements located by the given key from
1294 * a %map.
1295 * Note that this function only erases the element, and that if
1296 * the element is itself a pointer, the pointed-to memory is not touched
1297 * in any way. Managing the pointer is the user's responsibility.
1298 */
1299 size_type
1300 erase(const key_type& __x)
1301 { return _M_t._M_erase_unique(__x); }
1302
1303#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
1304 // Note that for some types _Kt this may erase more than
1305 // one element, such as if _Kt::operator< checks only part
1306 // of the key.
1307 template <__heterogeneous_tree_key<map> _Kt>
1308 size_type
1309 erase(_Kt&& __x)
1310 { return _M_t._M_erase_tr(__x); }
1311#endif
1312 ///@}
1313
1314#if __cplusplus >= 201103L
1315 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1316 // DR 130. Associative erase should return an iterator.
1317 /**
1318 * @brief Erases a [first,last) range of elements from a %map.
1319 * @param __first Iterator pointing to the start of the range to be
1320 * erased.
1321 * @param __last Iterator pointing to the end of the range to
1322 * be erased.
1323 * @return The iterator @a __last.
1324 *
1325 * This function erases a sequence of elements from a %map.
1326 * Note that this function only erases the element, and that if
1327 * the element is itself a pointer, the pointed-to memory is not touched
1328 * in any way. Managing the pointer is the user's responsibility.
1329 */
1330 iterator
1331 erase(const_iterator __first, const_iterator __last)
1332 { return _M_t.erase(__first, __last); }
1333#else
1334 /**
1335 * @brief Erases a [__first,__last) range of elements from a %map.
1336 * @param __first Iterator pointing to the start of the range to be
1337 * erased.
1338 * @param __last Iterator pointing to the end of the range to
1339 * be erased.
1340 *
1341 * This function erases a sequence of elements from a %map.
1342 * Note that this function only erases the element, and that if
1343 * the element is itself a pointer, the pointed-to memory is not touched
1344 * in any way. Managing the pointer is the user's responsibility.
1345 */
1346 void
1347 erase(iterator __first, iterator __last)
1348 { _M_t.erase(__first, __last); }
1349#endif
1350
1351 /**
1352 * @brief Swaps data with another %map.
1353 * @param __x A %map of the same element and allocator types.
1354 *
1355 * This exchanges the elements between two maps in constant
1356 * time. (It is only swapping a pointer, an integer, and an
1357 * instance of the @c Compare type (which itself is often
1358 * stateless and empty), so it should be quite fast.) Note
1359 * that the global std::swap() function is specialized such
1360 * that std::swap(m1,m2) will feed to this function.
1361 *
1362 * Whether the allocators are swapped depends on the allocator traits.
1363 */
1364 void
1365 swap(map& __x)
1366 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
1367 { _M_t.swap(__x._M_t); }
1368
1369 /**
1370 * Erases all elements in a %map. Note that this function only
1371 * erases the elements, and that if the elements themselves are
1372 * pointers, the pointed-to memory is not touched in any way.
1373 * Managing the pointer is the user's responsibility.
1374 */
1375 void
1376 clear() _GLIBCXX_NOEXCEPT
1377 { _M_t.clear(); }
1378
1379 // observers
1380 /**
1381 * Returns the key comparison object out of which the %map was
1382 * constructed.
1383 */
1384 key_compare
1385 key_comp() const
1386 { return _M_t.key_comp(); }
1387
1388 /**
1389 * Returns a value comparison object, built from the key comparison
1390 * object out of which the %map was constructed.
1391 */
1392 value_compare
1394 { return value_compare(_M_t.key_comp()); }
1395
1396 // [23.3.1.3] map operations
1397
1398 ///@{
1399 /**
1400 * @brief Tries to locate an element in a %map.
1401 * @param __x Key of (key, value) %pair to be located.
1402 * @return Iterator pointing to sought-after element, or end() if not
1403 * found.
1404 *
1405 * This function takes a key and tries to locate the element with which
1406 * the key matches. If successful the function returns an iterator
1407 * pointing to the sought after %pair. If unsuccessful it returns the
1408 * past-the-end ( @c end() ) iterator.
1409 */
1410
1411 iterator
1412 find(const key_type& __x)
1413 { return _M_t.find(__x); }
1414
1415#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1416 template<typename _Kt>
1417 auto
1418 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
1419 { return _M_t._M_find_tr(__x); }
1420#endif
1421 ///@}
1422
1423 ///@{
1424 /**
1425 * @brief Tries to locate an element in a %map.
1426 * @param __x Key of (key, value) %pair to be located.
1427 * @return Read-only (constant) iterator pointing to sought-after
1428 * element, or end() if not found.
1429 *
1430 * This function takes a key and tries to locate the element with which
1431 * the key matches. If successful the function returns a constant
1432 * iterator pointing to the sought after %pair. If unsuccessful it
1433 * returns the past-the-end ( @c end() ) iterator.
1434 */
1435
1436 const_iterator
1437 find(const key_type& __x) const
1438 { return _M_t.find(__x); }
1439
1440#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1441 template<typename _Kt>
1442 auto
1443 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
1444 { return _M_t._M_find_tr(__x); }
1445#endif
1446 ///@}
1447
1448 ///@{
1449 /**
1450 * @brief Finds the number of elements with given key.
1451 * @param __x Key of (key, value) pairs to be located.
1452 * @return Number of elements with specified key.
1453 *
1454 * This function only makes sense for multimaps; for map the result will
1455 * either be 0 (not present) or 1 (present).
1456 */
1457 size_type
1458 count(const key_type& __x) const
1459 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
1460
1461#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1462 template<typename _Kt>
1463 auto
1464 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
1465 { return _M_t._M_count_tr(__x); }
1466#endif
1467 ///@}
1468
1469#if __cplusplus > 201703L
1470 ///@{
1471 /**
1472 * @brief Finds whether an element with the given key exists.
1473 * @param __x Key of (key, value) pairs to be located.
1474 * @return True if there is an element with the specified key.
1475 */
1476 bool
1477 contains(const key_type& __x) const
1478 { return _M_t.find(__x) != _M_t.end(); }
1479
1480 template<typename _Kt>
1481 auto
1482 contains(const _Kt& __x) const
1483 -> decltype(_M_t._M_find_tr(__x), void(), true)
1484 { return _M_t._M_find_tr(__x) != _M_t.end(); }
1485 ///@}
1486#endif
1487
1488 ///@{
1489 /**
1490 * @brief Finds the beginning of a subsequence matching given key.
1491 * @param __x Key of (key, value) pair to be located.
1492 * @return Iterator pointing to first element equal to or greater
1493 * than key, or end().
1494 *
1495 * This function returns the first element of a subsequence of elements
1496 * that matches the given key. If unsuccessful it returns an iterator
1497 * pointing to the first element that has a greater value than given key
1498 * or end() if no such element exists.
1499 */
1500 iterator
1501 lower_bound(const key_type& __x)
1502 { return _M_t.lower_bound(__x); }
1503
1504#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1505 template<typename _Kt>
1506 auto
1507 lower_bound(const _Kt& __x)
1508 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
1509 { return iterator(_M_t._M_lower_bound_tr(__x)); }
1510#endif
1511 ///@}
1512
1513 ///@{
1514 /**
1515 * @brief Finds the beginning of a subsequence matching given key.
1516 * @param __x Key of (key, value) pair to be located.
1517 * @return Read-only (constant) iterator pointing to first element
1518 * equal to or greater than key, or end().
1519 *
1520 * This function returns the first element of a subsequence of elements
1521 * that matches the given key. If unsuccessful it returns an iterator
1522 * pointing to the first element that has a greater value than given key
1523 * or end() if no such element exists.
1524 */
1525 const_iterator
1526 lower_bound(const key_type& __x) const
1527 { return _M_t.lower_bound(__x); }
1528
1529#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1530 template<typename _Kt>
1531 auto
1532 lower_bound(const _Kt& __x) const
1533 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
1534 { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
1535#endif
1536 ///@}
1537
1538 ///@{
1539 /**
1540 * @brief Finds the end of a subsequence matching given key.
1541 * @param __x Key of (key, value) pair to be located.
1542 * @return Iterator pointing to the first element
1543 * greater than key, or end().
1544 */
1545 iterator
1546 upper_bound(const key_type& __x)
1547 { return _M_t.upper_bound(__x); }
1548
1549#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1550 template<typename _Kt>
1551 auto
1552 upper_bound(const _Kt& __x)
1553 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
1554 { return iterator(_M_t._M_upper_bound_tr(__x)); }
1555#endif
1556 ///@}
1557
1558 ///@{
1559 /**
1560 * @brief Finds the end of a subsequence matching given key.
1561 * @param __x Key of (key, value) pair to be located.
1562 * @return Read-only (constant) iterator pointing to first iterator
1563 * greater than key, or end().
1564 */
1565 const_iterator
1566 upper_bound(const key_type& __x) const
1567 { return _M_t.upper_bound(__x); }
1568
1569#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1570 template<typename _Kt>
1571 auto
1572 upper_bound(const _Kt& __x) const
1573 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
1574 { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
1575#endif
1576 ///@}
1577
1578 ///@{
1579 /**
1580 * @brief Finds a subsequence matching given key.
1581 * @param __x Key of (key, value) pairs to be located.
1582 * @return Pair of iterators that possibly points to the subsequence
1583 * matching given key.
1584 *
1585 * This function is equivalent to
1586 * @code
1587 * std::make_pair(c.lower_bound(val),
1588 * c.upper_bound(val))
1589 * @endcode
1590 * (but is faster than making the calls separately).
1591 *
1592 * This function probably only makes sense for multimaps.
1593 */
1595 equal_range(const key_type& __x)
1596 { return _M_t.equal_range(__x); }
1597
1598#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1599 template<typename _Kt>
1600 auto
1601 equal_range(const _Kt& __x)
1602 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
1603 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
1604#endif
1605 ///@}
1606
1607 ///@{
1608 /**
1609 * @brief Finds a subsequence matching given key.
1610 * @param __x Key of (key, value) pairs to be located.
1611 * @return Pair of read-only (constant) iterators that possibly points
1612 * to the subsequence matching given key.
1613 *
1614 * This function is equivalent to
1615 * @code
1616 * std::make_pair(c.lower_bound(val),
1617 * c.upper_bound(val))
1618 * @endcode
1619 * (but is faster than making the calls separately).
1620 *
1621 * This function probably only makes sense for multimaps.
1622 */
1624 equal_range(const key_type& __x) const
1625 { return _M_t.equal_range(__x); }
1626
1627#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1628 template<typename _Kt>
1629 auto
1630 equal_range(const _Kt& __x) const
1632 _M_t._M_equal_range_tr(__x)))
1633 {
1635 _M_t._M_equal_range_tr(__x));
1636 }
1637#endif
1638 ///@}
1639
1640 template<typename _K1, typename _T1, typename _C1, typename _A1>
1641 friend bool
1642 operator==(const map<_K1, _T1, _C1, _A1>&,
1643 const map<_K1, _T1, _C1, _A1>&);
1644
1645#if __cpp_lib_three_way_comparison
1646 template<typename _K1, typename _T1, typename _C1, typename _A1>
1647 friend __detail::__synth3way_t<pair<const _K1, _T1>>
1648 operator<=>(const map<_K1, _T1, _C1, _A1>&,
1649 const map<_K1, _T1, _C1, _A1>&);
1650#else
1651 template<typename _K1, typename _T1, typename _C1, typename _A1>
1652 friend bool
1653 operator<(const map<_K1, _T1, _C1, _A1>&,
1654 const map<_K1, _T1, _C1, _A1>&);
1655#endif
1656 };
1657
1658
1659#if __cpp_deduction_guides >= 201606
1660
1661 template<typename _InputIterator,
1662 typename _Compare = less<__iter_key_t<_InputIterator>>,
1663 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
1664 typename = _RequireInputIter<_InputIterator>,
1665 typename = _RequireNotAllocator<_Compare>,
1666 typename = _RequireAllocator<_Allocator>>
1667 map(_InputIterator, _InputIterator,
1668 _Compare = _Compare(), _Allocator = _Allocator())
1669 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
1670 _Compare, _Allocator>;
1671
1672 template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
1673 typename _Allocator = allocator<pair<const _Key, _Tp>>,
1674 typename = _RequireNotAllocator<_Compare>,
1675 typename = _RequireAllocator<_Allocator>>
1677 _Compare = _Compare(), _Allocator = _Allocator())
1679
1680 template <typename _InputIterator, typename _Allocator,
1681 typename = _RequireInputIter<_InputIterator>,
1682 typename = _RequireAllocator<_Allocator>>
1683 map(_InputIterator, _InputIterator, _Allocator)
1684 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
1686
1687 template<typename _Key, typename _Tp, typename _Allocator,
1688 typename = _RequireAllocator<_Allocator>>
1690 -> map<_Key, _Tp, less<_Key>, _Allocator>;
1691
1692#if __glibcxx_containers_ranges // C++ >= 23
1693 template<ranges::input_range _Rg,
1694 __not_allocator_like _Compare = less<__detail::__range_key_type<_Rg>>,
1695 __allocator_like _Alloc =
1696 std::allocator<__detail::__range_to_alloc_type<_Rg>>>
1697 map(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1699 __detail::__range_mapped_type<_Rg>,
1700 _Compare, _Alloc>;
1701
1702 template<ranges::input_range _Rg, __allocator_like _Alloc>
1703 map(from_range_t, _Rg&&, _Alloc)
1705 __detail::__range_mapped_type<_Rg>,
1707 _Alloc>;
1708#endif
1709
1710#endif // deduction guides
1711
1712 /**
1713 * @brief Map equality comparison.
1714 * @param __x A %map.
1715 * @param __y A %map of the same type as @a x.
1716 * @return True iff the size and elements of the maps are equal.
1717 *
1718 * This is an equivalence relation. It is linear in the size of the
1719 * maps. Maps are considered equivalent if their sizes are equal,
1720 * and if corresponding elements compare equal.
1721 */
1722 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1723 inline bool
1726 { return __x._M_t == __y._M_t; }
1727
1728#if __cpp_lib_three_way_comparison
1729 /**
1730 * @brief Map ordering relation.
1731 * @param __x A `map`.
1732 * @param __y A `map` of the same type as `x`.
1733 * @return A value indicating whether `__x` is less than, equal to,
1734 * greater than, or incomparable with `__y`.
1735 *
1736 * This is a total ordering relation. It is linear in the size of the
1737 * maps. The elements must be comparable with @c <.
1738 *
1739 * See `std::lexicographical_compare_three_way()` for how the determination
1740 * is made. This operator is used to synthesize relational operators like
1741 * `<` and `>=` etc.
1742 */
1743 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1744 inline __detail::__synth3way_t<pair<const _Key, _Tp>>
1745 operator<=>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1746 const map<_Key, _Tp, _Compare, _Alloc>& __y)
1747 { return __x._M_t <=> __y._M_t; }
1748#else
1749 /**
1750 * @brief Map ordering relation.
1751 * @param __x A %map.
1752 * @param __y A %map of the same type as @a x.
1753 * @return True iff @a x is lexicographically less than @a y.
1754 *
1755 * This is a total ordering relation. It is linear in the size of the
1756 * maps. The elements must be comparable with @c <.
1757 *
1758 * See std::lexicographical_compare() for how the determination is made.
1759 */
1760 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1761 inline bool
1762 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1764 { return __x._M_t < __y._M_t; }
1765
1766 /// Based on operator==
1767 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1768 inline bool
1769 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1771 { return !(__x == __y); }
1772
1773 /// Based on operator<
1774 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1775 inline bool
1776 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1778 { return __y < __x; }
1779
1780 /// Based on operator<
1781 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1782 inline bool
1783 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1785 { return !(__y < __x); }
1786
1787 /// Based on operator<
1788 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1789 inline bool
1790 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1792 { return !(__x < __y); }
1793#endif // three-way comparison
1794
1795 /// See std::map::swap().
1796 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1797 inline void
1800 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1801 { __x.swap(__y); }
1802
1803_GLIBCXX_END_NAMESPACE_CONTAINER
1804
1805#ifdef __glibcxx_node_extract // >= C++17 && HOSTED
1806 // Allow std::map access to internals of compatible maps.
1807 template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
1808 typename _Cmp2>
1809 struct
1810 _Rb_tree_merge_helper<_GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>,
1811 _Cmp2>
1812 {
1813 private:
1814 friend class _GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>;
1815
1816 static auto&
1817 _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
1818 { return __map._M_t; }
1819
1820 static auto&
1821 _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
1822 { return __map._M_t; }
1823 };
1824#endif // C++17
1825
1826_GLIBCXX_END_NAMESPACE_VERSION
1827} // namespace std
1828
1829#endif /* _STL_MAP_H */
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1886
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2735
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition stl_pair.h:82
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
initializer_list
Primary class template, tuple.
Definition tuple:834
is_scalar
Definition type_traits:874
is_nothrow_copy_constructible
Definition type_traits:1334
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
One of the comparison functors.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
_T1 first
The first member.
Definition stl_pair.h:308
constexpr void swap(pair &__p) noexcept(__and_< __is_nothrow_swappable< _T1 >, __is_nothrow_swappable< _T2 > >::value)
Swap the first members and then the second members.
Definition stl_pair.h:321
Common iterator class.
A standard container made up of (key,value) pairs, which can be retrieved based on a key,...
A standard container made up of (key,value) pairs, which can be retrieved based on a key,...
Definition stl_map.h:107
iterator emplace_hint(const_iterator __pos, _Args &&... __args)
Attempts to build and insert a std::pair into the map.
Definition stl_map.h:699
const_iterator find(const key_type &__x) const
Tries to locate an element in a map.
Definition stl_map.h:1437
map(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition stl_map.h:272
__enable_if_t< is_constructible< value_type, _Pair >::value, iterator > insert(const_iterator __position, _Pair &&__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:1056
bool empty() const noexcept
Definition stl_map.h:501
const_reverse_iterator rend() const noexcept
Definition stl_map.h:455
bool contains(const key_type &__x) const
Finds whether an element with the given key exists.
Definition stl_map.h:1477
~map()=default
map(const map &__m, const __type_identity_t< allocator_type > &__a)
Allocator-extended copy constructor.
Definition stl_map.h:256
value_compare value_comp() const
Definition stl_map.h:1393
void insert(_InputIterator __first, _InputIterator __last)
Template function that attempts to insert a range of elements.
Definition stl_map.h:1074
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition stl_map.h:1546
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition stl_map.h:1595
map(initializer_list< value_type > __l, const _Compare &__comp=_Compare(), const allocator_type &__a=allocator_type())
Builds a map from an initializer_list.
Definition stl_map.h:244
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:948
map(map &&)=default
Map move constructor.
size_type count(const key_type &__x) const
Finds the number of elements with given key.
Definition stl_map.h:1458
const_reverse_iterator rbegin() const noexcept
Definition stl_map.h:437
reverse_iterator rbegin() noexcept
Definition stl_map.h:428
iterator insert_or_assign(const_iterator __hint, const key_type &__k, _Obj &&__obj)
Attempts to insert or assign a std::pair into the map.
Definition stl_map.h:1182
const_iterator end() const noexcept
Definition stl_map.h:419
const_iterator cend() const noexcept
Definition stl_map.h:474
mapped_type & at(const key_type &__k)
Access to map data.
Definition stl_map.h:586
iterator insert_or_assign(const_iterator __hint, key_type &&__k, _Obj &&__obj)
Attempts to insert or assign a std::pair into the map.
Definition stl_map.h:1203
key_compare key_comp() const
Definition stl_map.h:1385
map(map &&__m, const __type_identity_t< allocator_type > &__a) noexcept(is_nothrow_copy_constructible< _Compare >::value &&_Alloc_traits::_S_always_equal())
Allocator-extended move constructor.
Definition stl_map.h:260
void clear() noexcept
Definition stl_map.h:1376
iterator end() noexcept
Definition stl_map.h:410
map(_InputIterator __first, _InputIterator __last)
Builds a map from a range.
Definition stl_map.h:289
const_reverse_iterator crbegin() const noexcept
Definition stl_map.h:483
pair< iterator, bool > insert_or_assign(const key_type &__k, _Obj &&__obj)
Attempts to insert or assign a std::pair into the map.
Definition stl_map.h:1099
void swap(map &__x) noexcept(/*conditional */)
Swaps data with another map.
Definition stl_map.h:1365
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition stl_map.h:1300
map(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition stl_map.h:266
map(const map &)=default
Map copy constructor.
map(const allocator_type &__a)
Allocator-extended default constructor.
Definition stl_map.h:252
iterator insert(const_iterator __position, value_type &&__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:1051
iterator insert(const_iterator __position, const value_type &__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:1041
map(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a map with no elements.
Definition stl_map.h:210
reverse_iterator rend() noexcept
Definition stl_map.h:446
iterator erase(const_iterator __first, const_iterator __last)
Erases a [first,last) range of elements from a map.
Definition stl_map.h:1331
void insert(std::initializer_list< value_type > __list)
Attempts to insert a list of std::pairs into the map.
Definition stl_map.h:993
map & operator=(const map &)=default
Map assignment operator.
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition stl_map.h:1526
size_type size() const noexcept
Definition stl_map.h:506
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition stl_map.h:1624
iterator find(const key_type &__x)
Tries to locate an element in a map.
Definition stl_map.h:1412
map(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a map from a range.
Definition stl_map.h:306
__enable_if_t< is_constructible< value_type, _Pair >::value, pair< iterator, bool > > insert(_Pair &&__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:961
iterator erase(const_iterator __position)
Erases an element from a map.
Definition stl_map.h:1262
mapped_type & operator[](const key_type &__k)
Subscript ( [] ) access to map data.
Definition stl_map.h:531
auto contains(const _Kt &__x) const -> decltype(_M_t._M_find_tr(__x), void(), true)
Finds whether an element with the given key exists.
Definition stl_map.h:1482
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition stl_map.h:1501
const_reverse_iterator crend() const noexcept
Definition stl_map.h:492
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_map.h:382
map & operator=(initializer_list< value_type > __l)
Map list assignment operator.
Definition stl_map.h:373
std::pair< iterator, bool > emplace(_Args &&... __args)
Attempts to build and insert a std::pair into the map.
Definition stl_map.h:649
const_iterator cbegin() const noexcept
Definition stl_map.h:465
size_type max_size() const noexcept
Definition stl_map.h:511
const_iterator begin() const noexcept
Definition stl_map.h:401
iterator begin() noexcept
Definition stl_map.h:392
map & operator=(map &&)=default
Move assignment operator.
std::pair< iterator, bool > insert(value_type &&__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:955
map()=default
Default constructor creates no elements.
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition stl_map.h:1566
Uniform interface to C++98 and C++11 allocators.
A range for which ranges::begin returns an input iterator.