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 * Lookup requires logarithmic time.
526 */
527 mapped_type&
528 operator[](const key_type& __k)
529 {
530 // concept requirements
531 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
532
533 iterator __i = lower_bound(__k);
534 // __i->first is greater than or equivalent to __k.
535 if (__i == end() || key_comp()(__k, (*__i).first))
536#if __cplusplus >= 201103L
537 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
539 std::tuple<>());
540#else
541 __i = insert(__i, value_type(__k, mapped_type()));
542#endif
543 return (*__i).second;
544 }
545
546#if __cplusplus >= 201103L
547 mapped_type&
548 operator[](key_type&& __k)
549 {
550 // concept requirements
551 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
552
553 iterator __i = lower_bound(__k);
554 // __i->first is greater than or equivalent to __k.
555 if (__i == end() || key_comp()(__k, (*__i).first))
556 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
558 std::tuple<>());
559 return (*__i).second;
560 }
561#endif
562
563 // _GLIBCXX_RESOLVE_LIB_DEFECTS
564 // DR 464. Suggestion for new member functions in standard containers.
565 /**
566 * @brief Access to %map data.
567 * @param __k The key for which data should be retrieved.
568 * @return A reference to the data whose key is equivalent to @a __k, if
569 * such a data is present in the %map.
570 * @throw std::out_of_range If no such data is present.
571 */
572 mapped_type&
573 at(const key_type& __k)
574 {
575 iterator __i = lower_bound(__k);
576 if (__i == end() || key_comp()(__k, (*__i).first))
577 __throw_out_of_range(__N("map::at"));
578 return (*__i).second;
579 }
580
581 const mapped_type&
582 at(const key_type& __k) const
583 {
584 const_iterator __i = lower_bound(__k);
585 if (__i == end() || key_comp()(__k, (*__i).first))
586 __throw_out_of_range(__N("map::at"));
587 return (*__i).second;
588 }
589
590 // modifiers
591#if __cplusplus >= 201103L
592 /**
593 * @brief Attempts to build and insert a std::pair into the %map.
594 *
595 * @param __args Arguments used to generate a new pair instance (see
596 * std::piecewise_contruct for passing arguments to each
597 * part of the pair constructor).
598 *
599 * @return A pair, of which the first element is an iterator that points
600 * to the possibly inserted pair, and the second is a bool that
601 * is true if the pair was actually inserted.
602 *
603 * This function attempts to build and insert a (key, value) %pair into
604 * the %map.
605 * A %map relies on unique keys and thus a %pair is only inserted if its
606 * first element (the key) is not already present in the %map.
607 *
608 * Insertion requires logarithmic time.
609 */
610 template<typename... _Args>
612 emplace(_Args&&... __args)
613 {
614#if __cplusplus >= 201703L
615 if constexpr (sizeof...(_Args) == 2)
616 if constexpr (is_same_v<allocator_type, allocator<value_type>>)
617 {
618 auto&& [__a, __v] = pair<_Args&...>(__args...);
619 if constexpr (__usable_key<decltype(__a)>)
620 {
621 const key_type& __k = __a;
622 iterator __i = lower_bound(__k);
623 if (__i == end() || key_comp()(__k, (*__i).first))
624 {
625 __i = emplace_hint(__i, std::forward<_Args>(__args)...);
626 return {__i, true};
627 }
628 return {__i, false};
629 }
630 }
631#endif
632 return _M_t._M_emplace_unique(std::forward<_Args>(__args)...);
633 }
634
635 /**
636 * @brief Attempts to build and insert a std::pair into the %map.
637 *
638 * @param __pos An iterator that serves as a hint as to where the pair
639 * should be inserted.
640 * @param __args Arguments used to generate a new pair instance (see
641 * std::piecewise_contruct for passing arguments to each
642 * part of the pair constructor).
643 * @return An iterator that points to the element with key of the
644 * std::pair built from @a __args (may or may not be that
645 * std::pair).
646 *
647 * This function is not concerned about whether the insertion took place,
648 * and thus does not return a boolean like the single-argument emplace()
649 * does.
650 * Note that the first parameter is only a hint and can potentially
651 * improve the performance of the insertion process. A bad hint would
652 * cause no gains in efficiency.
653 *
654 * See
655 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
656 * for more on @a hinting.
657 *
658 * Insertion requires logarithmic time (if the hint is not taken).
659 */
660 template<typename... _Args>
662 emplace_hint(const_iterator __pos, _Args&&... __args)
663 {
664 return _M_t._M_emplace_hint_unique(__pos,
665 std::forward<_Args>(__args)...);
666 }
667#endif
668
669#ifdef __glibcxx_node_extract // >= C++17
670 /// Extract a node.
671 node_type
672 extract(const_iterator __pos)
673 {
674 __glibcxx_assert(__pos != end());
675 return _M_t.extract(__pos);
676 }
677
678 /// Extract a node.
679 node_type
680 extract(const key_type& __x)
681 { return _M_t.extract(__x); }
682
683#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
684 template <__heterogeneous_tree_key<map> _Kt>
685 node_type
686 extract(_Kt&& __key)
687 { return _M_t._M_extract_tr(__key); }
688#endif
689
690 /// Re-insert an extracted node.
691 insert_return_type
692 insert(node_type&& __nh)
693 { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
694
695 /// Re-insert an extracted node.
696 iterator
697 insert(const_iterator __hint, node_type&& __nh)
698 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
699
700 template<typename, typename>
701 friend struct std::_Rb_tree_merge_helper;
702
703 template<typename _Cmp2>
704 void
705 merge(map<_Key, _Tp, _Cmp2, _Alloc>& __source)
706 {
707 using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
708 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
709 }
710
711 template<typename _Cmp2>
712 void
713 merge(map<_Key, _Tp, _Cmp2, _Alloc>&& __source)
714 { merge(__source); }
715
716 template<typename _Cmp2>
717 void
718 merge(multimap<_Key, _Tp, _Cmp2, _Alloc>& __source)
719 {
720 using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
721 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
722 }
723
724 template<typename _Cmp2>
725 void
726 merge(multimap<_Key, _Tp, _Cmp2, _Alloc>&& __source)
727 { merge(__source); }
728#endif // C++17
729
730#ifdef __glibcxx_map_try_emplace // C++ >= 17 && HOSTED
731 /**
732 * @brief Attempts to build and insert a std::pair into the %map.
733 *
734 * @param __k Key to use for finding a possibly existing pair in
735 * the map.
736 * @param __args Arguments used to generate the .second for a new pair
737 * instance.
738 *
739 * @return A pair, of which the first element is an iterator that points
740 * to the possibly inserted pair, and the second is a bool that
741 * is true if the pair was actually inserted.
742 *
743 * This function attempts to build and insert a (key, value) %pair into
744 * the %map.
745 * A %map relies on unique keys and thus a %pair is only inserted if its
746 * first element (the key) is not already present in the %map.
747 * If a %pair is not inserted, this function has no effect.
748 *
749 * Insertion requires logarithmic time.
750 */
751 template <typename... _Args>
753 try_emplace(const key_type& __k, _Args&&... __args)
754 {
755 iterator __i = lower_bound(__k);
756 if (__i == end() || key_comp()(__k, (*__i).first))
757 {
761 std::forward<_Args>(__args)...));
762 return {__i, true};
763 }
764 return {__i, false};
765 }
766
767 // move-capable overload
768 template <typename... _Args>
770 try_emplace(key_type&& __k, _Args&&... __args)
771 {
772 iterator __i = lower_bound(__k);
773 if (__i == end() || key_comp()(__k, (*__i).first))
774 {
778 std::forward<_Args>(__args)...));
779 return {__i, true};
780 }
781 return {__i, false};
782 }
783
784 /**
785 * @brief Attempts to build and insert a std::pair into the %map.
786 *
787 * @param __hint An iterator that serves as a hint as to where the
788 * pair should be inserted.
789 * @param __k Key to use for finding a possibly existing pair in
790 * the map.
791 * @param __args Arguments used to generate the .second for a new pair
792 * instance.
793 * @return An iterator that points to the element with key of the
794 * std::pair built from @a __args (may or may not be that
795 * std::pair).
796 *
797 * This function is not concerned about whether the insertion took place,
798 * and thus does not return a boolean like the single-argument
799 * try_emplace() does. However, if insertion did not take place,
800 * this function has no effect.
801 * Note that the first parameter is only a hint and can potentially
802 * improve the performance of the insertion process. A bad hint would
803 * cause no gains in efficiency.
804 *
805 * See
806 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
807 * for more on @a hinting.
808 *
809 * Insertion requires logarithmic time (if the hint is not taken).
810 */
811 template <typename... _Args>
812 iterator
813 try_emplace(const_iterator __hint, const key_type& __k,
814 _Args&&... __args)
815 {
816 iterator __i;
817 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
818 if (__true_hint.second)
819 __i = emplace_hint(iterator(__true_hint.second),
823 std::forward<_Args>(__args)...));
824 else
825 __i = iterator(__true_hint.first);
826 return __i;
827 }
828
829 // move-capable overload
830 template <typename... _Args>
831 iterator
832 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
833 {
834 iterator __i;
835 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
836 if (__true_hint.second)
837 __i = emplace_hint(iterator(__true_hint.second),
841 std::forward<_Args>(__args)...));
842 else
843 __i = iterator(__true_hint.first);
844 return __i;
845 }
846#endif
847
848 /**
849 * @brief Attempts to insert a std::pair into the %map.
850 * @param __x Pair to be inserted (see std::make_pair for easy
851 * creation of pairs).
852 *
853 * @return A pair, of which the first element is an iterator that
854 * points to the possibly inserted pair, and the second is
855 * a bool that is true if the pair was actually inserted.
856 *
857 * This function attempts to insert a (key, value) %pair into the %map.
858 * A %map relies on unique keys and thus a %pair is only inserted if its
859 * first element (the key) is not already present in the %map.
860 *
861 * Insertion requires logarithmic time.
862 * @{
863 */
865 insert(const value_type& __x)
866 { return _M_t._M_insert_unique(__x); }
867
868#if __cplusplus >= 201103L
869 // _GLIBCXX_RESOLVE_LIB_DEFECTS
870 // 2354. Unnecessary copying when inserting into maps with braced-init
872 insert(value_type&& __x)
873 { return _M_t._M_insert_unique(std::move(__x)); }
874
875 template<typename _Pair>
876 __enable_if_t<is_constructible<value_type, _Pair>::value,
878 insert(_Pair&& __x)
879 {
880#if __cplusplus >= 201703L
881 using _P2 = remove_reference_t<_Pair>;
882 if constexpr (__is_pair<remove_const_t<_P2>>)
883 if constexpr (is_same_v<allocator_type, allocator<value_type>>)
884 if constexpr (__usable_key<typename _P2::first_type>)
885 {
886 const key_type& __k = __x.first;
887 iterator __i = lower_bound(__k);
888 if (__i == end() || key_comp()(__k, (*__i).first))
889 {
890 __i = emplace_hint(__i, std::forward<_Pair>(__x));
891 return {__i, true};
892 }
893 return {__i, false};
894 }
895#endif
896 return _M_t._M_emplace_unique(std::forward<_Pair>(__x));
897 }
898#endif
899 /// @}
900
901#if __cplusplus >= 201103L
902 /**
903 * @brief Attempts to insert a list of std::pairs into the %map.
904 * @param __list A std::initializer_list<value_type> of pairs to be
905 * inserted.
906 *
907 * Complexity similar to that of the range constructor.
908 */
909 void
911 { insert(__list.begin(), __list.end()); }
912#endif
913
914#if __glibcxx_containers_ranges // C++ >= 23
915 /**
916 * @brief Inserts a range of elements.
917 * @since C++23
918 * @param __rg An input range of elements that can be converted to
919 * the map's value type.
920 */
921 template<__detail::__container_compatible_range<value_type> _Rg>
922 void
923 insert_range(_Rg&& __rg)
924 {
925 auto __first = ranges::begin(__rg);
926 const auto __last = ranges::end(__rg);
927 for (; __first != __last; ++__first)
928 insert(*__first);
929 }
930#endif
931
932 /**
933 * @brief Attempts to insert a std::pair into the %map.
934 * @param __position An iterator that serves as a hint as to where the
935 * pair should be inserted.
936 * @param __x Pair to be inserted (see std::make_pair for easy creation
937 * of pairs).
938 * @return An iterator that points to the element with key of
939 * @a __x (may or may not be the %pair passed in).
940 *
941
942 * This function is not concerned about whether the insertion
943 * took place, and thus does not return a boolean like the
944 * single-argument insert() does. Note that the first
945 * parameter is only a hint and can potentially improve the
946 * performance of the insertion process. A bad hint would
947 * cause no gains in efficiency.
948 *
949 * See
950 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
951 * for more on @a hinting.
952 *
953 * Insertion requires logarithmic time (if the hint is not taken).
954 * @{
955 */
956 iterator
957#if __cplusplus >= 201103L
958 insert(const_iterator __position, const value_type& __x)
959#else
960 insert(iterator __position, const value_type& __x)
961#endif
962 { return _M_t._M_insert_unique_(__position, __x); }
963
964#if __cplusplus >= 201103L
965 // _GLIBCXX_RESOLVE_LIB_DEFECTS
966 // 2354. Unnecessary copying when inserting into maps with braced-init
968 insert(const_iterator __position, value_type&& __x)
969 { return _M_t._M_insert_unique_(__position, std::move(__x)); }
970
971 template<typename _Pair>
972 __enable_if_t<is_constructible<value_type, _Pair>::value, iterator>
973 insert(const_iterator __position, _Pair&& __x)
974 {
975 return _M_t._M_emplace_hint_unique(__position,
977 }
978#endif
979 /// @}
980
981 /**
982 * @brief Template function that attempts to insert a range of elements.
983 * @param __first Iterator pointing to the start of the range to be
984 * inserted.
985 * @param __last Iterator pointing to the end of the range.
986 *
987 * Complexity similar to that of the range constructor.
988 */
989 template<typename _InputIterator>
990 void
991 insert(_InputIterator __first, _InputIterator __last)
992 { _M_t._M_insert_range_unique(__first, __last); }
993
994#if __cplusplus > 201402L
995 /**
996 * @brief Attempts to insert or assign a std::pair into the %map.
997 * @param __k Key to use for finding a possibly existing pair in
998 * the map.
999 * @param __obj Argument used to generate the .second for a pair
1000 * instance.
1001 *
1002 * @return A pair, of which the first element is an iterator that
1003 * points to the possibly inserted pair, and the second is
1004 * a bool that is true if the pair was actually inserted.
1005 *
1006 * This function attempts to insert a (key, value) %pair into the %map.
1007 * A %map relies on unique keys and thus a %pair is only inserted if its
1008 * first element (the key) is not already present in the %map.
1009 * If the %pair was already in the %map, the .second of the %pair
1010 * is assigned from __obj.
1011 *
1012 * Insertion requires logarithmic time.
1013 */
1014 template <typename _Obj>
1016 insert_or_assign(const key_type& __k, _Obj&& __obj)
1017 {
1018 iterator __i = lower_bound(__k);
1019 if (__i == end() || key_comp()(__k, (*__i).first))
1020 {
1024 std::forward<_Obj>(__obj)));
1025 return {__i, true};
1026 }
1027 (*__i).second = std::forward<_Obj>(__obj);
1028 return {__i, false};
1029 }
1030
1031 // move-capable overload
1032 template <typename _Obj>
1034 insert_or_assign(key_type&& __k, _Obj&& __obj)
1035 {
1036 iterator __i = lower_bound(__k);
1037 if (__i == end() || key_comp()(__k, (*__i).first))
1038 {
1042 std::forward<_Obj>(__obj)));
1043 return {__i, true};
1044 }
1045 (*__i).second = std::forward<_Obj>(__obj);
1046 return {__i, false};
1047 }
1048
1049 /**
1050 * @brief Attempts to insert or assign a std::pair into the %map.
1051 * @param __hint An iterator that serves as a hint as to where the
1052 * pair should be inserted.
1053 * @param __k Key to use for finding a possibly existing pair in
1054 * the map.
1055 * @param __obj Argument used to generate the .second for a pair
1056 * instance.
1057 *
1058 * @return An iterator that points to the element with key of
1059 * @a __x (may or may not be the %pair passed in).
1060 *
1061 * This function attempts to insert a (key, value) %pair into the %map.
1062 * A %map relies on unique keys and thus a %pair is only inserted if its
1063 * first element (the key) is not already present in the %map.
1064 * If the %pair was already in the %map, the .second of the %pair
1065 * is assigned from __obj.
1066 *
1067 * Insertion requires logarithmic time.
1068 */
1069 template <typename _Obj>
1070 iterator
1071 insert_or_assign(const_iterator __hint,
1072 const key_type& __k, _Obj&& __obj)
1073 {
1074 iterator __i;
1075 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
1076 if (__true_hint.second)
1077 {
1078 return emplace_hint(iterator(__true_hint.second),
1082 std::forward<_Obj>(__obj)));
1083 }
1084 __i = iterator(__true_hint.first);
1085 (*__i).second = std::forward<_Obj>(__obj);
1086 return __i;
1087 }
1088
1089 // move-capable overload
1090 template <typename _Obj>
1091 iterator
1092 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
1093 {
1094 iterator __i;
1095 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
1096 if (__true_hint.second)
1097 {
1098 return emplace_hint(iterator(__true_hint.second),
1102 std::forward<_Obj>(__obj)));
1103 }
1104 __i = iterator(__true_hint.first);
1105 (*__i).second = std::forward<_Obj>(__obj);
1106 return __i;
1107 }
1108#endif
1109
1110#if __cplusplus >= 201103L
1111 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1112 // DR 130. Associative erase should return an iterator.
1113 /**
1114 * @brief Erases an element from a %map.
1115 * @param __position An iterator pointing to the element to be erased.
1116 * @return An iterator pointing to the element immediately following
1117 * @a position prior to the element being erased. If no such
1118 * element exists, end() is returned.
1119 *
1120 * This function erases an element, pointed to by the given
1121 * iterator, from a %map. Note that this function only erases
1122 * the element, and that if the element is itself a pointer,
1123 * the pointed-to memory is not touched in any way. Managing
1124 * the pointer is the user's responsibility.
1125 *
1126 * @{
1127 */
1128 iterator
1129 erase(const_iterator __position)
1130 { return _M_t.erase(__position); }
1131
1132 // LWG 2059
1133 _GLIBCXX_ABI_TAG_CXX11
1134 iterator
1135 erase(iterator __position)
1136 { return _M_t.erase(__position); }
1137 /// @}
1138#else
1139 /**
1140 * @brief Erases an element from a %map.
1141 * @param __position An iterator pointing to the element to be erased.
1142 *
1143 * This function erases an element, pointed to by the given
1144 * iterator, from a %map. Note that this function only erases
1145 * the element, and that if the element is itself a pointer,
1146 * the pointed-to memory is not touched in any way. Managing
1147 * the pointer is the user's responsibility.
1148 */
1149 void
1150 erase(iterator __position)
1151 { _M_t.erase(__position); }
1152#endif
1153
1154 ///@{
1155 /**
1156 * @brief Erases elements according to the provided key.
1157 * @param __x Key of element to be erased.
1158 * @return The number of elements erased.
1159 *
1160 * This function erases all the elements located by the given key from
1161 * a %map.
1162 * Note that this function only erases the element, and that if
1163 * the element is itself a pointer, the pointed-to memory is not touched
1164 * in any way. Managing the pointer is the user's responsibility.
1165 */
1166 size_type
1167 erase(const key_type& __x)
1168 { return _M_t._M_erase_unique(__x); }
1169
1170#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
1171 // Note that for some types _Kt this may erase more than
1172 // one element, such as if _Kt::operator< checks only part
1173 // of the key.
1174 template <__heterogeneous_tree_key<map> _Kt>
1175 size_type
1176 erase(_Kt&& __x)
1177 { return _M_t._M_erase_tr(__x); }
1178#endif
1179 ///@}
1180
1181#if __cplusplus >= 201103L
1182 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1183 // DR 130. Associative erase should return an iterator.
1184 /**
1185 * @brief Erases a [first,last) range of elements from a %map.
1186 * @param __first Iterator pointing to the start of the range to be
1187 * erased.
1188 * @param __last Iterator pointing to the end of the range to
1189 * be erased.
1190 * @return The iterator @a __last.
1191 *
1192 * This function erases a sequence of elements from a %map.
1193 * Note that this function only erases the element, and that if
1194 * the element is itself a pointer, the pointed-to memory is not touched
1195 * in any way. Managing the pointer is the user's responsibility.
1196 */
1197 iterator
1198 erase(const_iterator __first, const_iterator __last)
1199 { return _M_t.erase(__first, __last); }
1200#else
1201 /**
1202 * @brief Erases a [__first,__last) range of elements from a %map.
1203 * @param __first Iterator pointing to the start of the range to be
1204 * erased.
1205 * @param __last Iterator pointing to the end of the range to
1206 * be erased.
1207 *
1208 * This function erases a sequence of elements from a %map.
1209 * Note that this function only erases the element, and that if
1210 * the element is itself a pointer, the pointed-to memory is not touched
1211 * in any way. Managing the pointer is the user's responsibility.
1212 */
1213 void
1214 erase(iterator __first, iterator __last)
1215 { _M_t.erase(__first, __last); }
1216#endif
1217
1218 /**
1219 * @brief Swaps data with another %map.
1220 * @param __x A %map of the same element and allocator types.
1221 *
1222 * This exchanges the elements between two maps in constant
1223 * time. (It is only swapping a pointer, an integer, and an
1224 * instance of the @c Compare type (which itself is often
1225 * stateless and empty), so it should be quite fast.) Note
1226 * that the global std::swap() function is specialized such
1227 * that std::swap(m1,m2) will feed to this function.
1228 *
1229 * Whether the allocators are swapped depends on the allocator traits.
1230 */
1231 void
1232 swap(map& __x)
1233 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
1234 { _M_t.swap(__x._M_t); }
1235
1236 /**
1237 * Erases all elements in a %map. Note that this function only
1238 * erases the elements, and that if the elements themselves are
1239 * pointers, the pointed-to memory is not touched in any way.
1240 * Managing the pointer is the user's responsibility.
1241 */
1242 void
1243 clear() _GLIBCXX_NOEXCEPT
1244 { _M_t.clear(); }
1245
1246 // observers
1247 /**
1248 * Returns the key comparison object out of which the %map was
1249 * constructed.
1250 */
1251 key_compare
1252 key_comp() const
1253 { return _M_t.key_comp(); }
1254
1255 /**
1256 * Returns a value comparison object, built from the key comparison
1257 * object out of which the %map was constructed.
1258 */
1259 value_compare
1261 { return value_compare(_M_t.key_comp()); }
1262
1263 // [23.3.1.3] map operations
1264
1265 ///@{
1266 /**
1267 * @brief Tries to locate an element in a %map.
1268 * @param __x Key of (key, value) %pair to be located.
1269 * @return Iterator pointing to sought-after element, or end() if not
1270 * found.
1271 *
1272 * This function takes a key and tries to locate the element with which
1273 * the key matches. If successful the function returns an iterator
1274 * pointing to the sought after %pair. If unsuccessful it returns the
1275 * past-the-end ( @c end() ) iterator.
1276 */
1277
1278 iterator
1279 find(const key_type& __x)
1280 { return _M_t.find(__x); }
1281
1282#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1283 template<typename _Kt>
1284 auto
1285 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
1286 { return _M_t._M_find_tr(__x); }
1287#endif
1288 ///@}
1289
1290 ///@{
1291 /**
1292 * @brief Tries to locate an element in a %map.
1293 * @param __x Key of (key, value) %pair to be located.
1294 * @return Read-only (constant) iterator pointing to sought-after
1295 * element, or end() if not found.
1296 *
1297 * This function takes a key and tries to locate the element with which
1298 * the key matches. If successful the function returns a constant
1299 * iterator pointing to the sought after %pair. If unsuccessful it
1300 * returns the past-the-end ( @c end() ) iterator.
1301 */
1302
1303 const_iterator
1304 find(const key_type& __x) const
1305 { return _M_t.find(__x); }
1306
1307#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1308 template<typename _Kt>
1309 auto
1310 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
1311 { return _M_t._M_find_tr(__x); }
1312#endif
1313 ///@}
1314
1315 ///@{
1316 /**
1317 * @brief Finds the number of elements with given key.
1318 * @param __x Key of (key, value) pairs to be located.
1319 * @return Number of elements with specified key.
1320 *
1321 * This function only makes sense for multimaps; for map the result will
1322 * either be 0 (not present) or 1 (present).
1323 */
1324 size_type
1325 count(const key_type& __x) const
1326 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
1327
1328#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1329 template<typename _Kt>
1330 auto
1331 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
1332 { return _M_t._M_count_tr(__x); }
1333#endif
1334 ///@}
1335
1336#if __cplusplus > 201703L
1337 ///@{
1338 /**
1339 * @brief Finds whether an element with the given key exists.
1340 * @param __x Key of (key, value) pairs to be located.
1341 * @return True if there is an element with the specified key.
1342 */
1343 bool
1344 contains(const key_type& __x) const
1345 { return _M_t.find(__x) != _M_t.end(); }
1346
1347 template<typename _Kt>
1348 auto
1349 contains(const _Kt& __x) const
1350 -> decltype(_M_t._M_find_tr(__x), void(), true)
1351 { return _M_t._M_find_tr(__x) != _M_t.end(); }
1352 ///@}
1353#endif
1354
1355 ///@{
1356 /**
1357 * @brief Finds the beginning of a subsequence matching given key.
1358 * @param __x Key of (key, value) pair to be located.
1359 * @return Iterator pointing to first element equal to or greater
1360 * than key, or end().
1361 *
1362 * This function returns the first element of a subsequence of elements
1363 * that matches the given key. If unsuccessful it returns an iterator
1364 * pointing to the first element that has a greater value than given key
1365 * or end() if no such element exists.
1366 */
1367 iterator
1368 lower_bound(const key_type& __x)
1369 { return _M_t.lower_bound(__x); }
1370
1371#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1372 template<typename _Kt>
1373 auto
1374 lower_bound(const _Kt& __x)
1375 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
1376 { return iterator(_M_t._M_lower_bound_tr(__x)); }
1377#endif
1378 ///@}
1379
1380 ///@{
1381 /**
1382 * @brief Finds the beginning of a subsequence matching given key.
1383 * @param __x Key of (key, value) pair to be located.
1384 * @return Read-only (constant) iterator pointing to first element
1385 * equal to or greater than key, or end().
1386 *
1387 * This function returns the first element of a subsequence of elements
1388 * that matches the given key. If unsuccessful it returns an iterator
1389 * pointing to the first element that has a greater value than given key
1390 * or end() if no such element exists.
1391 */
1392 const_iterator
1393 lower_bound(const key_type& __x) const
1394 { return _M_t.lower_bound(__x); }
1395
1396#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1397 template<typename _Kt>
1398 auto
1399 lower_bound(const _Kt& __x) const
1400 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
1401 { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
1402#endif
1403 ///@}
1404
1405 ///@{
1406 /**
1407 * @brief Finds the end of a subsequence matching given key.
1408 * @param __x Key of (key, value) pair to be located.
1409 * @return Iterator pointing to the first element
1410 * greater than key, or end().
1411 */
1412 iterator
1413 upper_bound(const key_type& __x)
1414 { return _M_t.upper_bound(__x); }
1415
1416#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1417 template<typename _Kt>
1418 auto
1419 upper_bound(const _Kt& __x)
1420 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
1421 { return iterator(_M_t._M_upper_bound_tr(__x)); }
1422#endif
1423 ///@}
1424
1425 ///@{
1426 /**
1427 * @brief Finds the end of a subsequence matching given key.
1428 * @param __x Key of (key, value) pair to be located.
1429 * @return Read-only (constant) iterator pointing to first iterator
1430 * greater than key, or end().
1431 */
1432 const_iterator
1433 upper_bound(const key_type& __x) const
1434 { return _M_t.upper_bound(__x); }
1435
1436#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1437 template<typename _Kt>
1438 auto
1439 upper_bound(const _Kt& __x) const
1440 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
1441 { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
1442#endif
1443 ///@}
1444
1445 ///@{
1446 /**
1447 * @brief Finds a subsequence matching given key.
1448 * @param __x Key of (key, value) pairs to be located.
1449 * @return Pair of iterators that possibly points to the subsequence
1450 * matching given key.
1451 *
1452 * This function is equivalent to
1453 * @code
1454 * std::make_pair(c.lower_bound(val),
1455 * c.upper_bound(val))
1456 * @endcode
1457 * (but is faster than making the calls separately).
1458 *
1459 * This function probably only makes sense for multimaps.
1460 */
1462 equal_range(const key_type& __x)
1463 { return _M_t.equal_range(__x); }
1464
1465#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1466 template<typename _Kt>
1467 auto
1468 equal_range(const _Kt& __x)
1469 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
1470 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
1471#endif
1472 ///@}
1473
1474 ///@{
1475 /**
1476 * @brief Finds a subsequence matching given key.
1477 * @param __x Key of (key, value) pairs to be located.
1478 * @return Pair of read-only (constant) iterators that possibly points
1479 * to the subsequence matching given key.
1480 *
1481 * This function is equivalent to
1482 * @code
1483 * std::make_pair(c.lower_bound(val),
1484 * c.upper_bound(val))
1485 * @endcode
1486 * (but is faster than making the calls separately).
1487 *
1488 * This function probably only makes sense for multimaps.
1489 */
1491 equal_range(const key_type& __x) const
1492 { return _M_t.equal_range(__x); }
1493
1494#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1495 template<typename _Kt>
1496 auto
1497 equal_range(const _Kt& __x) const
1499 _M_t._M_equal_range_tr(__x)))
1500 {
1502 _M_t._M_equal_range_tr(__x));
1503 }
1504#endif
1505 ///@}
1506
1507 template<typename _K1, typename _T1, typename _C1, typename _A1>
1508 friend bool
1509 operator==(const map<_K1, _T1, _C1, _A1>&,
1510 const map<_K1, _T1, _C1, _A1>&);
1511
1512#if __cpp_lib_three_way_comparison
1513 template<typename _K1, typename _T1, typename _C1, typename _A1>
1514 friend __detail::__synth3way_t<pair<const _K1, _T1>>
1515 operator<=>(const map<_K1, _T1, _C1, _A1>&,
1516 const map<_K1, _T1, _C1, _A1>&);
1517#else
1518 template<typename _K1, typename _T1, typename _C1, typename _A1>
1519 friend bool
1520 operator<(const map<_K1, _T1, _C1, _A1>&,
1521 const map<_K1, _T1, _C1, _A1>&);
1522#endif
1523 };
1524
1525
1526#if __cpp_deduction_guides >= 201606
1527
1528 template<typename _InputIterator,
1529 typename _Compare = less<__iter_key_t<_InputIterator>>,
1530 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
1531 typename = _RequireInputIter<_InputIterator>,
1532 typename = _RequireNotAllocator<_Compare>,
1533 typename = _RequireAllocator<_Allocator>>
1534 map(_InputIterator, _InputIterator,
1535 _Compare = _Compare(), _Allocator = _Allocator())
1536 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
1537 _Compare, _Allocator>;
1538
1539 template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
1540 typename _Allocator = allocator<pair<const _Key, _Tp>>,
1541 typename = _RequireNotAllocator<_Compare>,
1542 typename = _RequireAllocator<_Allocator>>
1544 _Compare = _Compare(), _Allocator = _Allocator())
1546
1547 template <typename _InputIterator, typename _Allocator,
1548 typename = _RequireInputIter<_InputIterator>,
1549 typename = _RequireAllocator<_Allocator>>
1550 map(_InputIterator, _InputIterator, _Allocator)
1551 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
1553
1554 template<typename _Key, typename _Tp, typename _Allocator,
1555 typename = _RequireAllocator<_Allocator>>
1557 -> map<_Key, _Tp, less<_Key>, _Allocator>;
1558
1559#if __glibcxx_containers_ranges // C++ >= 23
1560 template<ranges::input_range _Rg,
1561 __not_allocator_like _Compare = less<__detail::__range_key_type<_Rg>>,
1562 __allocator_like _Alloc =
1563 std::allocator<__detail::__range_to_alloc_type<_Rg>>>
1564 map(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1566 __detail::__range_mapped_type<_Rg>,
1567 _Compare, _Alloc>;
1568
1569 template<ranges::input_range _Rg, __allocator_like _Alloc>
1570 map(from_range_t, _Rg&&, _Alloc)
1572 __detail::__range_mapped_type<_Rg>,
1574 _Alloc>;
1575#endif
1576
1577#endif // deduction guides
1578
1579 /**
1580 * @brief Map equality comparison.
1581 * @param __x A %map.
1582 * @param __y A %map of the same type as @a x.
1583 * @return True iff the size and elements of the maps are equal.
1584 *
1585 * This is an equivalence relation. It is linear in the size of the
1586 * maps. Maps are considered equivalent if their sizes are equal,
1587 * and if corresponding elements compare equal.
1588 */
1589 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1590 inline bool
1593 { return __x._M_t == __y._M_t; }
1594
1595#if __cpp_lib_three_way_comparison
1596 /**
1597 * @brief Map ordering relation.
1598 * @param __x A `map`.
1599 * @param __y A `map` of the same type as `x`.
1600 * @return A value indicating whether `__x` is less than, equal to,
1601 * greater than, or incomparable with `__y`.
1602 *
1603 * This is a total ordering relation. It is linear in the size of the
1604 * maps. The elements must be comparable with @c <.
1605 *
1606 * See `std::lexicographical_compare_three_way()` for how the determination
1607 * is made. This operator is used to synthesize relational operators like
1608 * `<` and `>=` etc.
1609 */
1610 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1611 inline __detail::__synth3way_t<pair<const _Key, _Tp>>
1612 operator<=>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1613 const map<_Key, _Tp, _Compare, _Alloc>& __y)
1614 { return __x._M_t <=> __y._M_t; }
1615#else
1616 /**
1617 * @brief Map ordering relation.
1618 * @param __x A %map.
1619 * @param __y A %map of the same type as @a x.
1620 * @return True iff @a x is lexicographically less than @a y.
1621 *
1622 * This is a total ordering relation. It is linear in the size of the
1623 * maps. The elements must be comparable with @c <.
1624 *
1625 * See std::lexicographical_compare() for how the determination is made.
1626 */
1627 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1628 inline bool
1629 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1631 { return __x._M_t < __y._M_t; }
1632
1633 /// Based on operator==
1634 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1635 inline bool
1636 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1638 { return !(__x == __y); }
1639
1640 /// Based on operator<
1641 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1642 inline bool
1643 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1645 { return __y < __x; }
1646
1647 /// Based on operator<
1648 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1649 inline bool
1650 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1652 { return !(__y < __x); }
1653
1654 /// Based on operator<
1655 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1656 inline bool
1657 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1659 { return !(__x < __y); }
1660#endif // three-way comparison
1661
1662 /// See std::map::swap().
1663 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1664 inline void
1667 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1668 { __x.swap(__y); }
1669
1670_GLIBCXX_END_NAMESPACE_CONTAINER
1671
1672#ifdef __glibcxx_node_extract // >= C++17 && HOSTED
1673 // Allow std::map access to internals of compatible maps.
1674 template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
1675 typename _Cmp2>
1676 struct
1677 _Rb_tree_merge_helper<_GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>,
1678 _Cmp2>
1679 {
1680 private:
1681 friend class _GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>;
1682
1683 static auto&
1684 _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
1685 { return __map._M_t; }
1686
1687 static auto&
1688 _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
1689 { return __map._M_t; }
1690 };
1691#endif // C++17
1692
1693_GLIBCXX_END_NAMESPACE_VERSION
1694} // namespace std
1695
1696#endif /* _STL_MAP_H */
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1880
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:868
is_nothrow_copy_constructible
Definition type_traits:1328
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:662
const_iterator find(const key_type &__x) const
Tries to locate an element in a map.
Definition stl_map.h:1304
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:973
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:1344
~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:1260
void insert(_InputIterator __first, _InputIterator __last)
Template function that attempts to insert a range of elements.
Definition stl_map.h:991
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition stl_map.h:1413
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition stl_map.h:1462
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:865
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:1325
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:1071
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:573
key_compare key_comp() const
Definition stl_map.h:1252
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:1243
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:1016
void swap(map &__x) noexcept(/*conditional */)
Swaps data with another map.
Definition stl_map.h:1232
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition stl_map.h:1167
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:968
iterator insert(const_iterator __position, const value_type &__x)
Attempts to insert a std::pair into the map.
Definition stl_map.h:958
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:1198
void insert(std::initializer_list< value_type > __list)
Attempts to insert a list of std::pairs into the map.
Definition stl_map.h:910
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:1393
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:1491
iterator find(const key_type &__x)
Tries to locate an element in a map.
Definition stl_map.h:1279
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:878
iterator erase(const_iterator __position)
Erases an element from a map.
Definition stl_map.h:1129
mapped_type & operator[](const key_type &__k)
Subscript ( [] ) access to map data.
Definition stl_map.h:528
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:1349
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition stl_map.h:1368
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:612
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:872
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:1433
Uniform interface to C++98 and C++11 allocators.
A range for which ranges::begin returns an input iterator.