libstdc++
stl_map.h
Go to the documentation of this file.
1 // Map implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2014 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 
59 #include <bits/functexcept.h>
60 #include <bits/concept_check.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
63 #include <tuple>
64 #endif
65 
66 namespace std _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 
70  /**
71  * @brief A standard container made up of (key,value) pairs, which can be
72  * retrieved based on a key, in logarithmic time.
73  *
74  * @ingroup associative_containers
75  *
76  * @tparam _Key Type of key objects.
77  * @tparam _Tp Type of mapped objects.
78  * @tparam _Compare Comparison function object type, defaults to less<_Key>.
79  * @tparam _Alloc Allocator type, defaults to
80  * allocator<pair<const _Key, _Tp>.
81  *
82  * Meets the requirements of a <a href="tables.html#65">container</a>, a
83  * <a href="tables.html#66">reversible container</a>, and an
84  * <a href="tables.html#69">associative container</a> (using unique keys).
85  * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
86  * value_type is std::pair<const Key,T>.
87  *
88  * Maps support bidirectional iterators.
89  *
90  * The private tree data is declared exactly the same way for map and
91  * multimap; the distinction is made entirely in how the tree functions are
92  * called (*_unique versus *_equal, same as the standard).
93  */
94  template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
95  typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
96  class map
97  {
98  public:
99  typedef _Key key_type;
100  typedef _Tp mapped_type;
102  typedef _Compare key_compare;
103  typedef _Alloc allocator_type;
104 
105  private:
106  // concept requirements
107  typedef typename _Alloc::value_type _Alloc_value_type;
108  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
109  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
110  _BinaryFunctionConcept)
111  __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
112 
113  public:
114  class value_compare
115  : public std::binary_function<value_type, value_type, bool>
116  {
117  friend class map<_Key, _Tp, _Compare, _Alloc>;
118  protected:
119  _Compare comp;
120 
121  value_compare(_Compare __c)
122  : comp(__c) { }
123 
124  public:
125  bool operator()(const value_type& __x, const value_type& __y) const
126  { return comp(__x.first, __y.first); }
127  };
128 
129  private:
130  /// This turns a red-black tree into a [multi]map.
132  rebind<value_type>::other _Pair_alloc_type;
133 
134  typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
135  key_compare, _Pair_alloc_type> _Rep_type;
136 
137  /// The actual tree structure.
138  _Rep_type _M_t;
139 
141 
142  public:
143  // many of these are specified differently in ISO, but the following are
144  // "functionally equivalent"
145  typedef typename _Alloc_traits::pointer pointer;
146  typedef typename _Alloc_traits::const_pointer const_pointer;
147  typedef typename _Alloc_traits::reference reference;
148  typedef typename _Alloc_traits::const_reference const_reference;
149  typedef typename _Rep_type::iterator iterator;
150  typedef typename _Rep_type::const_iterator const_iterator;
151  typedef typename _Rep_type::size_type size_type;
152  typedef typename _Rep_type::difference_type difference_type;
155 
156  // [23.3.1.1] construct/copy/destroy
157  // (get_allocator() is normally listed in this section, but seems to have
158  // been accidentally omitted in the printed standard)
159  /**
160  * @brief Default constructor creates no elements.
161  */
162  map()
163  : _M_t() { }
164 
165  /**
166  * @brief Creates a %map with no elements.
167  * @param __comp A comparison object.
168  * @param __a An allocator object.
169  */
170  explicit
171  map(const _Compare& __comp,
172  const allocator_type& __a = allocator_type())
173  : _M_t(__comp, _Pair_alloc_type(__a)) { }
174 
175  /**
176  * @brief %Map copy constructor.
177  * @param __x A %map of identical element and allocator types.
178  *
179  * The newly-created %map uses a copy of the allocation object
180  * used by @a __x.
181  */
182  map(const map& __x)
183  : _M_t(__x._M_t) { }
184 
185 #if __cplusplus >= 201103L
186  /**
187  * @brief %Map move constructor.
188  * @param __x A %map of identical element and allocator types.
189  *
190  * The newly-created %map contains the exact contents of @a __x.
191  * The contents of @a __x are a valid, but unspecified %map.
192  */
193  map(map&& __x)
194  noexcept(is_nothrow_copy_constructible<_Compare>::value)
195  : _M_t(std::move(__x._M_t)) { }
196 
197  /**
198  * @brief Builds a %map from an initializer_list.
199  * @param __l An initializer_list.
200  * @param __comp A comparison object.
201  * @param __a An allocator object.
202  *
203  * Create a %map consisting of copies of the elements in the
204  * initializer_list @a __l.
205  * This is linear in N if the range is already sorted, and NlogN
206  * otherwise (where N is @a __l.size()).
207  */
209  const _Compare& __comp = _Compare(),
210  const allocator_type& __a = allocator_type())
211  : _M_t(__comp, _Pair_alloc_type(__a))
212  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
213 
214  /// Allocator-extended default constructor.
215  explicit
216  map(const allocator_type& __a)
217  : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
218 
219  /// Allocator-extended copy constructor.
220  map(const map& __m, const allocator_type& __a)
221  : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
222 
223  /// Allocator-extended move constructor.
224  map(map&& __m, const allocator_type& __a)
225  noexcept(is_nothrow_copy_constructible<_Compare>::value
226  && _Alloc_traits::_S_always_equal())
227  : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
228 
229  /// Allocator-extended initialier-list constructor.
231  : _M_t(_Compare(), _Pair_alloc_type(__a))
232  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
233 
234  /// Allocator-extended range constructor.
235  template<typename _InputIterator>
236  map(_InputIterator __first, _InputIterator __last,
237  const allocator_type& __a)
238  : _M_t(_Compare(), _Pair_alloc_type(__a))
239  { _M_t._M_insert_unique(__first, __last); }
240 #endif
241 
242  /**
243  * @brief Builds a %map from a range.
244  * @param __first An input iterator.
245  * @param __last An input iterator.
246  *
247  * Create a %map consisting of copies of the elements from
248  * [__first,__last). This is linear in N if the range is
249  * already sorted, and NlogN otherwise (where N is
250  * distance(__first,__last)).
251  */
252  template<typename _InputIterator>
253  map(_InputIterator __first, _InputIterator __last)
254  : _M_t()
255  { _M_t._M_insert_unique(__first, __last); }
256 
257  /**
258  * @brief Builds a %map from a range.
259  * @param __first An input iterator.
260  * @param __last An input iterator.
261  * @param __comp A comparison functor.
262  * @param __a An allocator object.
263  *
264  * Create a %map consisting of copies of the elements from
265  * [__first,__last). This is linear in N if the range is
266  * already sorted, and NlogN otherwise (where N is
267  * distance(__first,__last)).
268  */
269  template<typename _InputIterator>
270  map(_InputIterator __first, _InputIterator __last,
271  const _Compare& __comp,
272  const allocator_type& __a = allocator_type())
273  : _M_t(__comp, _Pair_alloc_type(__a))
274  { _M_t._M_insert_unique(__first, __last); }
275 
276  // FIXME There is no dtor declared, but we should have something
277  // generated by Doxygen. I don't know what tags to add to this
278  // paragraph to make that happen:
279  /**
280  * The dtor only erases the elements, and note that if the elements
281  * themselves are pointers, the pointed-to memory is not touched in any
282  * way. Managing the pointer is the user's responsibility.
283  */
284 
285  /**
286  * @brief %Map assignment operator.
287  * @param __x A %map of identical element and allocator types.
288  *
289  * All the elements of @a __x are copied, but unlike the copy
290  * constructor, the allocator object is not copied.
291  */
292  map&
293  operator=(const map& __x)
294  {
295  _M_t = __x._M_t;
296  return *this;
297  }
298 
299 #if __cplusplus >= 201103L
300  /**
301  * @brief %Map move assignment operator.
302  * @param __x A %map of identical element and allocator types.
303  *
304  * The contents of @a __x are moved into this map (without copying).
305  * @a __x is a valid, but unspecified %map.
306  */
307  map&
308  operator=(map&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
309  {
310  if (!_M_t._M_move_assign(__x._M_t))
311  {
312  // The rvalue's allocator cannot be moved and is not equal,
313  // so we need to individually move each element.
314  clear();
315  insert(std::__make_move_if_noexcept_iterator(__x.begin()),
316  std::__make_move_if_noexcept_iterator(__x.end()));
317  __x.clear();
318  }
319  return *this;
320  }
321 
322  /**
323  * @brief %Map list assignment operator.
324  * @param __l An initializer_list.
325  *
326  * This function fills a %map with copies of the elements in the
327  * initializer list @a __l.
328  *
329  * Note that the assignment completely changes the %map and
330  * that the resulting %map's size is the same as the number
331  * of elements assigned. Old data may be lost.
332  */
333  map&
335  {
336  this->clear();
337  this->insert(__l.begin(), __l.end());
338  return *this;
339  }
340 #endif
341 
342  /// Get a copy of the memory allocation object.
343  allocator_type
344  get_allocator() const _GLIBCXX_NOEXCEPT
345  { return allocator_type(_M_t.get_allocator()); }
346 
347  // iterators
348  /**
349  * Returns a read/write iterator that points to the first pair in the
350  * %map.
351  * Iteration is done in ascending order according to the keys.
352  */
353  iterator
354  begin() _GLIBCXX_NOEXCEPT
355  { return _M_t.begin(); }
356 
357  /**
358  * Returns a read-only (constant) iterator that points to the first pair
359  * in the %map. Iteration is done in ascending order according to the
360  * keys.
361  */
362  const_iterator
363  begin() const _GLIBCXX_NOEXCEPT
364  { return _M_t.begin(); }
365 
366  /**
367  * Returns a read/write iterator that points one past the last
368  * pair in the %map. Iteration is done in ascending order
369  * according to the keys.
370  */
371  iterator
372  end() _GLIBCXX_NOEXCEPT
373  { return _M_t.end(); }
374 
375  /**
376  * Returns a read-only (constant) iterator that points one past the last
377  * pair in the %map. Iteration is done in ascending order according to
378  * the keys.
379  */
380  const_iterator
381  end() const _GLIBCXX_NOEXCEPT
382  { return _M_t.end(); }
383 
384  /**
385  * Returns a read/write reverse iterator that points to the last pair in
386  * the %map. Iteration is done in descending order according to the
387  * keys.
388  */
390  rbegin() _GLIBCXX_NOEXCEPT
391  { return _M_t.rbegin(); }
392 
393  /**
394  * Returns a read-only (constant) reverse iterator that points to the
395  * last pair in the %map. Iteration is done in descending order
396  * according to the keys.
397  */
398  const_reverse_iterator
399  rbegin() const _GLIBCXX_NOEXCEPT
400  { return _M_t.rbegin(); }
401 
402  /**
403  * Returns a read/write reverse iterator that points to one before the
404  * first pair in the %map. Iteration is done in descending order
405  * according to the keys.
406  */
408  rend() _GLIBCXX_NOEXCEPT
409  { return _M_t.rend(); }
410 
411  /**
412  * Returns a read-only (constant) reverse iterator that points to one
413  * before the first pair in the %map. Iteration is done in descending
414  * order according to the keys.
415  */
416  const_reverse_iterator
417  rend() const _GLIBCXX_NOEXCEPT
418  { return _M_t.rend(); }
419 
420 #if __cplusplus >= 201103L
421  /**
422  * Returns a read-only (constant) iterator that points to the first pair
423  * in the %map. Iteration is done in ascending order according to the
424  * keys.
425  */
426  const_iterator
427  cbegin() const noexcept
428  { return _M_t.begin(); }
429 
430  /**
431  * Returns a read-only (constant) iterator that points one past the last
432  * pair in the %map. Iteration is done in ascending order according to
433  * the keys.
434  */
435  const_iterator
436  cend() const noexcept
437  { return _M_t.end(); }
438 
439  /**
440  * Returns a read-only (constant) reverse iterator that points to the
441  * last pair in the %map. Iteration is done in descending order
442  * according to the keys.
443  */
444  const_reverse_iterator
446  { return _M_t.rbegin(); }
447 
448  /**
449  * Returns a read-only (constant) reverse iterator that points to one
450  * before the first pair in the %map. Iteration is done in descending
451  * order according to the keys.
452  */
453  const_reverse_iterator
454  crend() const noexcept
455  { return _M_t.rend(); }
456 #endif
457 
458  // capacity
459  /** Returns true if the %map is empty. (Thus begin() would equal
460  * end().)
461  */
462  bool
463  empty() const _GLIBCXX_NOEXCEPT
464  { return _M_t.empty(); }
465 
466  /** Returns the size of the %map. */
467  size_type
468  size() const _GLIBCXX_NOEXCEPT
469  { return _M_t.size(); }
470 
471  /** Returns the maximum size of the %map. */
472  size_type
473  max_size() const _GLIBCXX_NOEXCEPT
474  { return _M_t.max_size(); }
475 
476  // [23.3.1.2] element access
477  /**
478  * @brief Subscript ( @c [] ) access to %map data.
479  * @param __k The key for which data should be retrieved.
480  * @return A reference to the data of the (key,data) %pair.
481  *
482  * Allows for easy lookup with the subscript ( @c [] )
483  * operator. Returns data associated with the key specified in
484  * subscript. If the key does not exist, a pair with that key
485  * is created using default values, which is then returned.
486  *
487  * Lookup requires logarithmic time.
488  */
489  mapped_type&
490  operator[](const key_type& __k)
491  {
492  // concept requirements
493  __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
494 
495  iterator __i = lower_bound(__k);
496  // __i->first is greater than or equivalent to __k.
497  if (__i == end() || key_comp()(__k, (*__i).first))
498 #if __cplusplus >= 201103L
499  __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
501  std::tuple<>());
502 #else
503  __i = insert(__i, value_type(__k, mapped_type()));
504 #endif
505  return (*__i).second;
506  }
507 
508 #if __cplusplus >= 201103L
509  mapped_type&
510  operator[](key_type&& __k)
511  {
512  // concept requirements
513  __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
514 
515  iterator __i = lower_bound(__k);
516  // __i->first is greater than or equivalent to __k.
517  if (__i == end() || key_comp()(__k, (*__i).first))
518  __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
519  std::forward_as_tuple(std::move(__k)),
520  std::tuple<>());
521  return (*__i).second;
522  }
523 #endif
524 
525  // _GLIBCXX_RESOLVE_LIB_DEFECTS
526  // DR 464. Suggestion for new member functions in standard containers.
527  /**
528  * @brief Access to %map data.
529  * @param __k The key for which data should be retrieved.
530  * @return A reference to the data whose key is equivalent to @a __k, if
531  * such a data is present in the %map.
532  * @throw std::out_of_range If no such data is present.
533  */
534  mapped_type&
535  at(const key_type& __k)
536  {
537  iterator __i = lower_bound(__k);
538  if (__i == end() || key_comp()(__k, (*__i).first))
539  __throw_out_of_range(__N("map::at"));
540  return (*__i).second;
541  }
542 
543  const mapped_type&
544  at(const key_type& __k) const
545  {
546  const_iterator __i = lower_bound(__k);
547  if (__i == end() || key_comp()(__k, (*__i).first))
548  __throw_out_of_range(__N("map::at"));
549  return (*__i).second;
550  }
551 
552  // modifiers
553 #if __cplusplus >= 201103L
554  /**
555  * @brief Attempts to build and insert a std::pair into the %map.
556  *
557  * @param __args Arguments used to generate a new pair instance (see
558  * std::piecewise_contruct for passing arguments to each
559  * part of the pair constructor).
560  *
561  * @return A pair, of which the first element is an iterator that points
562  * to the possibly inserted pair, and the second is a bool that
563  * is true if the pair was actually inserted.
564  *
565  * This function attempts to build and insert a (key, value) %pair into
566  * the %map.
567  * A %map relies on unique keys and thus a %pair is only inserted if its
568  * first element (the key) is not already present in the %map.
569  *
570  * Insertion requires logarithmic time.
571  */
572  template<typename... _Args>
574  emplace(_Args&&... __args)
575  { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
576 
577  /**
578  * @brief Attempts to build and insert a std::pair into the %map.
579  *
580  * @param __pos An iterator that serves as a hint as to where the pair
581  * should be inserted.
582  * @param __args Arguments used to generate a new pair instance (see
583  * std::piecewise_contruct for passing arguments to each
584  * part of the pair constructor).
585  * @return An iterator that points to the element with key of the
586  * std::pair built from @a __args (may or may not be that
587  * std::pair).
588  *
589  * This function is not concerned about whether the insertion took place,
590  * and thus does not return a boolean like the single-argument emplace()
591  * does.
592  * Note that the first parameter is only a hint and can potentially
593  * improve the performance of the insertion process. A bad hint would
594  * cause no gains in efficiency.
595  *
596  * See
597  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
598  * for more on @a hinting.
599  *
600  * Insertion requires logarithmic time (if the hint is not taken).
601  */
602  template<typename... _Args>
603  iterator
604  emplace_hint(const_iterator __pos, _Args&&... __args)
605  {
606  return _M_t._M_emplace_hint_unique(__pos,
607  std::forward<_Args>(__args)...);
608  }
609 #endif
610 
611  /**
612  * @brief Attempts to insert a std::pair into the %map.
613 
614  * @param __x Pair to be inserted (see std::make_pair for easy
615  * creation of pairs).
616  *
617  * @return A pair, of which the first element is an iterator that
618  * points to the possibly inserted pair, and the second is
619  * a bool that is true if the pair was actually inserted.
620  *
621  * This function attempts to insert a (key, value) %pair into the %map.
622  * A %map relies on unique keys and thus a %pair is only inserted if its
623  * first element (the key) is not already present in the %map.
624  *
625  * Insertion requires logarithmic time.
626  */
628  insert(const value_type& __x)
629  { return _M_t._M_insert_unique(__x); }
630 
631 #if __cplusplus >= 201103L
632  template<typename _Pair, typename = typename
633  std::enable_if<std::is_constructible<value_type,
634  _Pair&&>::value>::type>
636  insert(_Pair&& __x)
637  { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); }
638 #endif
639 
640 #if __cplusplus >= 201103L
641  /**
642  * @brief Attempts to insert a list of std::pairs into the %map.
643  * @param __list A std::initializer_list<value_type> of pairs to be
644  * inserted.
645  *
646  * Complexity similar to that of the range constructor.
647  */
648  void
650  { insert(__list.begin(), __list.end()); }
651 #endif
652 
653  /**
654  * @brief Attempts to insert a std::pair into the %map.
655  * @param __position An iterator that serves as a hint as to where the
656  * pair should be inserted.
657  * @param __x Pair to be inserted (see std::make_pair for easy creation
658  * of pairs).
659  * @return An iterator that points to the element with key of
660  * @a __x (may or may not be the %pair passed in).
661  *
662 
663  * This function is not concerned about whether the insertion
664  * took place, and thus does not return a boolean like the
665  * single-argument insert() does. Note that the first
666  * parameter is only a hint and can potentially improve the
667  * performance of the insertion process. A bad hint would
668  * cause no gains in efficiency.
669  *
670  * See
671  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
672  * for more on @a hinting.
673  *
674  * Insertion requires logarithmic time (if the hint is not taken).
675  */
676  iterator
677 #if __cplusplus >= 201103L
678  insert(const_iterator __position, const value_type& __x)
679 #else
680  insert(iterator __position, const value_type& __x)
681 #endif
682  { return _M_t._M_insert_unique_(__position, __x); }
683 
684 #if __cplusplus >= 201103L
685  template<typename _Pair, typename = typename
686  std::enable_if<std::is_constructible<value_type,
687  _Pair&&>::value>::type>
688  iterator
689  insert(const_iterator __position, _Pair&& __x)
690  { return _M_t._M_insert_unique_(__position,
691  std::forward<_Pair>(__x)); }
692 #endif
693 
694  /**
695  * @brief Template function that attempts to insert a range of elements.
696  * @param __first Iterator pointing to the start of the range to be
697  * inserted.
698  * @param __last Iterator pointing to the end of the range.
699  *
700  * Complexity similar to that of the range constructor.
701  */
702  template<typename _InputIterator>
703  void
704  insert(_InputIterator __first, _InputIterator __last)
705  { _M_t._M_insert_unique(__first, __last); }
706 
707 #if __cplusplus >= 201103L
708  // _GLIBCXX_RESOLVE_LIB_DEFECTS
709  // DR 130. Associative erase should return an iterator.
710  /**
711  * @brief Erases an element from a %map.
712  * @param __position An iterator pointing to the element to be erased.
713  * @return An iterator pointing to the element immediately following
714  * @a position prior to the element being erased. If no such
715  * element exists, end() is returned.
716  *
717  * This function erases an element, pointed to by the given
718  * iterator, from a %map. Note that this function only erases
719  * the element, and that if the element is itself a pointer,
720  * the pointed-to memory is not touched in any way. Managing
721  * the pointer is the user's responsibility.
722  */
723  iterator
724  erase(const_iterator __position)
725  { return _M_t.erase(__position); }
726 
727  // LWG 2059
728  _GLIBCXX_ABI_TAG_CXX11
729  iterator
730  erase(iterator __position)
731  { return _M_t.erase(__position); }
732 #else
733  /**
734  * @brief Erases an element from a %map.
735  * @param __position An iterator pointing to the element to be erased.
736  *
737  * This function erases an element, pointed to by the given
738  * iterator, from a %map. Note that this function only erases
739  * the element, and that if the element is itself a pointer,
740  * the pointed-to memory is not touched in any way. Managing
741  * the pointer is the user's responsibility.
742  */
743  void
744  erase(iterator __position)
745  { _M_t.erase(__position); }
746 #endif
747 
748  /**
749  * @brief Erases elements according to the provided key.
750  * @param __x Key of element to be erased.
751  * @return The number of elements erased.
752  *
753  * This function erases all the elements located by the given key from
754  * a %map.
755  * Note that this function only erases the element, and that if
756  * the element is itself a pointer, the pointed-to memory is not touched
757  * in any way. Managing the pointer is the user's responsibility.
758  */
759  size_type
760  erase(const key_type& __x)
761  { return _M_t.erase(__x); }
762 
763 #if __cplusplus >= 201103L
764  // _GLIBCXX_RESOLVE_LIB_DEFECTS
765  // DR 130. Associative erase should return an iterator.
766  /**
767  * @brief Erases a [first,last) range of elements from a %map.
768  * @param __first Iterator pointing to the start of the range to be
769  * erased.
770  * @param __last Iterator pointing to the end of the range to
771  * be erased.
772  * @return The iterator @a __last.
773  *
774  * This function erases a sequence of elements from a %map.
775  * Note that this function only erases the element, and that if
776  * the element is itself a pointer, the pointed-to memory is not touched
777  * in any way. Managing the pointer is the user's responsibility.
778  */
779  iterator
780  erase(const_iterator __first, const_iterator __last)
781  { return _M_t.erase(__first, __last); }
782 #else
783  /**
784  * @brief Erases a [__first,__last) range of elements from a %map.
785  * @param __first Iterator pointing to the start of the range to be
786  * erased.
787  * @param __last Iterator pointing to the end of the range to
788  * be erased.
789  *
790  * This function erases a sequence of elements from a %map.
791  * Note that this function only erases the element, and that if
792  * the element is itself a pointer, the pointed-to memory is not touched
793  * in any way. Managing the pointer is the user's responsibility.
794  */
795  void
796  erase(iterator __first, iterator __last)
797  { _M_t.erase(__first, __last); }
798 #endif
799 
800  /**
801  * @brief Swaps data with another %map.
802  * @param __x A %map of the same element and allocator types.
803  *
804  * This exchanges the elements between two maps in constant
805  * time. (It is only swapping a pointer, an integer, and an
806  * instance of the @c Compare type (which itself is often
807  * stateless and empty), so it should be quite fast.) Note
808  * that the global std::swap() function is specialized such
809  * that std::swap(m1,m2) will feed to this function.
810  */
811  void
812  swap(map& __x)
813 #if __cplusplus >= 201103L
814  noexcept(_Alloc_traits::_S_nothrow_swap())
815 #endif
816  { _M_t.swap(__x._M_t); }
817 
818  /**
819  * Erases all elements in a %map. Note that this function only
820  * erases the elements, and that if the elements themselves are
821  * pointers, the pointed-to memory is not touched in any way.
822  * Managing the pointer is the user's responsibility.
823  */
824  void
825  clear() _GLIBCXX_NOEXCEPT
826  { _M_t.clear(); }
827 
828  // observers
829  /**
830  * Returns the key comparison object out of which the %map was
831  * constructed.
832  */
833  key_compare
834  key_comp() const
835  { return _M_t.key_comp(); }
836 
837  /**
838  * Returns a value comparison object, built from the key comparison
839  * object out of which the %map was constructed.
840  */
841  value_compare
842  value_comp() const
843  { return value_compare(_M_t.key_comp()); }
844 
845  // [23.3.1.3] map operations
846  /**
847  * @brief Tries to locate an element in a %map.
848  * @param __x Key of (key, value) %pair to be located.
849  * @return Iterator pointing to sought-after element, or end() if not
850  * found.
851  *
852  * This function takes a key and tries to locate the element with which
853  * the key matches. If successful the function returns an iterator
854  * pointing to the sought after %pair. If unsuccessful it returns the
855  * past-the-end ( @c end() ) iterator.
856  */
857  iterator
858  find(const key_type& __x)
859  { return _M_t.find(__x); }
860 
861  /**
862  * @brief Tries to locate an element in a %map.
863  * @param __x Key of (key, value) %pair to be located.
864  * @return Read-only (constant) iterator pointing to sought-after
865  * element, or end() if not found.
866  *
867  * This function takes a key and tries to locate the element with which
868  * the key matches. If successful the function returns a constant
869  * iterator pointing to the sought after %pair. If unsuccessful it
870  * returns the past-the-end ( @c end() ) iterator.
871  */
872  const_iterator
873  find(const key_type& __x) const
874  { return _M_t.find(__x); }
875 
876  /**
877  * @brief Finds the number of elements with given key.
878  * @param __x Key of (key, value) pairs to be located.
879  * @return Number of elements with specified key.
880  *
881  * This function only makes sense for multimaps; for map the result will
882  * either be 0 (not present) or 1 (present).
883  */
884  size_type
885  count(const key_type& __x) const
886  { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
887 
888  /**
889  * @brief Finds the beginning of a subsequence matching given key.
890  * @param __x Key of (key, value) pair to be located.
891  * @return Iterator pointing to first element equal to or greater
892  * than key, or end().
893  *
894  * This function returns the first element of a subsequence of elements
895  * that matches the given key. If unsuccessful it returns an iterator
896  * pointing to the first element that has a greater value than given key
897  * or end() if no such element exists.
898  */
899  iterator
900  lower_bound(const key_type& __x)
901  { return _M_t.lower_bound(__x); }
902 
903  /**
904  * @brief Finds the beginning of a subsequence matching given key.
905  * @param __x Key of (key, value) pair to be located.
906  * @return Read-only (constant) iterator pointing to first element
907  * equal to or greater than key, or end().
908  *
909  * This function returns the first element of a subsequence of elements
910  * that matches the given key. If unsuccessful it returns an iterator
911  * pointing to the first element that has a greater value than given key
912  * or end() if no such element exists.
913  */
914  const_iterator
915  lower_bound(const key_type& __x) const
916  { return _M_t.lower_bound(__x); }
917 
918  /**
919  * @brief Finds the end of a subsequence matching given key.
920  * @param __x Key of (key, value) pair to be located.
921  * @return Iterator pointing to the first element
922  * greater than key, or end().
923  */
924  iterator
925  upper_bound(const key_type& __x)
926  { return _M_t.upper_bound(__x); }
927 
928  /**
929  * @brief Finds the end of a subsequence matching given key.
930  * @param __x Key of (key, value) pair to be located.
931  * @return Read-only (constant) iterator pointing to first iterator
932  * greater than key, or end().
933  */
934  const_iterator
935  upper_bound(const key_type& __x) const
936  { return _M_t.upper_bound(__x); }
937 
938  /**
939  * @brief Finds a subsequence matching given key.
940  * @param __x Key of (key, value) pairs to be located.
941  * @return Pair of iterators that possibly points to the subsequence
942  * matching given key.
943  *
944  * This function is equivalent to
945  * @code
946  * std::make_pair(c.lower_bound(val),
947  * c.upper_bound(val))
948  * @endcode
949  * (but is faster than making the calls separately).
950  *
951  * This function probably only makes sense for multimaps.
952  */
954  equal_range(const key_type& __x)
955  { return _M_t.equal_range(__x); }
956 
957  /**
958  * @brief Finds a subsequence matching given key.
959  * @param __x Key of (key, value) pairs to be located.
960  * @return Pair of read-only (constant) iterators that possibly points
961  * to the subsequence matching given key.
962  *
963  * This function is equivalent to
964  * @code
965  * std::make_pair(c.lower_bound(val),
966  * c.upper_bound(val))
967  * @endcode
968  * (but is faster than making the calls separately).
969  *
970  * This function probably only makes sense for multimaps.
971  */
973  equal_range(const key_type& __x) const
974  { return _M_t.equal_range(__x); }
975 
976  template<typename _K1, typename _T1, typename _C1, typename _A1>
977  friend bool
978  operator==(const map<_K1, _T1, _C1, _A1>&,
979  const map<_K1, _T1, _C1, _A1>&);
980 
981  template<typename _K1, typename _T1, typename _C1, typename _A1>
982  friend bool
983  operator<(const map<_K1, _T1, _C1, _A1>&,
984  const map<_K1, _T1, _C1, _A1>&);
985  };
986 
987  /**
988  * @brief Map equality comparison.
989  * @param __x A %map.
990  * @param __y A %map of the same type as @a x.
991  * @return True iff the size and elements of the maps are equal.
992  *
993  * This is an equivalence relation. It is linear in the size of the
994  * maps. Maps are considered equivalent if their sizes are equal,
995  * and if corresponding elements compare equal.
996  */
997  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
998  inline bool
999  operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1001  { return __x._M_t == __y._M_t; }
1002 
1003  /**
1004  * @brief Map ordering relation.
1005  * @param __x A %map.
1006  * @param __y A %map of the same type as @a x.
1007  * @return True iff @a x is lexicographically less than @a y.
1008  *
1009  * This is a total ordering relation. It is linear in the size of the
1010  * maps. The elements must be comparable with @c <.
1011  *
1012  * See std::lexicographical_compare() for how the determination is made.
1013  */
1014  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1015  inline bool
1016  operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1018  { return __x._M_t < __y._M_t; }
1019 
1020  /// Based on operator==
1021  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1022  inline bool
1023  operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1025  { return !(__x == __y); }
1026 
1027  /// Based on operator<
1028  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1029  inline bool
1030  operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1032  { return __y < __x; }
1033 
1034  /// Based on operator<
1035  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1036  inline bool
1037  operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1039  { return !(__y < __x); }
1040 
1041  /// Based on operator<
1042  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1043  inline bool
1044  operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
1046  { return !(__x < __y); }
1047 
1048  /// See std::map::swap().
1049  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1050  inline void
1053  { __x.swap(__y); }
1054 
1055 _GLIBCXX_END_NAMESPACE_CONTAINER
1056 } // namespace std
1057 
1058 #endif /* _STL_MAP_H */
Primary class template, tuple.
Definition: tuple:388
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition: stl_map.h:344
map(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition: stl_map.h:236
const_reverse_iterator crend() const noexcept
Definition: stl_map.h:454
constexpr piecewise_construct_t piecewise_construct
piecewise_construct
Definition: stl_pair.h:79
Uniform interface to C++98 and C++0x allocators.
void insert(std::initializer_list< value_type > __list)
Attempts to insert a list of std::pairs into the map.
Definition: stl_map.h:649
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_map.h:954
map & operator=(map &&__x) noexcept(_Alloc_traits::_S_nothrow_move())
Map move assignment operator.
Definition: stl_map.h:308
const_iterator cbegin() const noexcept
Definition: stl_map.h:427
map(const map &__x)
Map copy constructor.
Definition: stl_map.h:182
iterator find(const key_type &__x)
Tries to locate an element in a map.
Definition: stl_map.h:858
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert a std::pair into the map.
Definition: stl_map.h:628
map(_InputIterator __first, _InputIterator __last)
Builds a map from a range.
Definition: stl_map.h:253
_T1 first
second_type is the second bound type
Definition: stl_pair.h:101
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:96
size_type count(const key_type &__x) const
Finds the number of elements with given key.
Definition: stl_map.h:885
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_map.h:935
size_type max_size() const noexcept
Definition: stl_map.h:473
const_reverse_iterator crbegin() const noexcept
Definition: stl_map.h:445
reverse_iterator rbegin() noexcept
Definition: stl_map.h:390
map()
Default constructor creates no elements.
Definition: stl_map.h:162
void clear() noexcept
Definition: stl_map.h:825
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition: stl_map.h:973
map & operator=(const map &__x)
Map assignment operator.
Definition: stl_map.h:293
mapped_type & at(const key_type &__k)
Access to map data.
Definition: stl_map.h:535
const_reverse_iterator rbegin() const noexcept
Definition: stl_map.h:399
const_reverse_iterator rend() const noexcept
Definition: stl_map.h:417
iterator erase(const_iterator __first, const_iterator __last)
Erases a [first,last) range of elements from a map.
Definition: stl_map.h:780
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Definition: stl_map.h:96
key_compare key_comp() const
Definition: stl_map.h:834
map(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition: stl_map.h:230
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:208
map(const map &__m, const allocator_type &__a)
Allocator-extended copy constructor.
Definition: stl_map.h:220
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_map.h:900
map(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a map from a range.
Definition: stl_map.h:270
mapped_type & operator[](const key_type &__k)
Subscript ( [] ) access to map data.
Definition: stl_map.h:490
void insert(_InputIterator __first, _InputIterator __last)
Template function that attempts to insert a range of elements.
Definition: stl_map.h:704
bool empty() const noexcept
Definition: stl_map.h:463
const_iterator end() const noexcept
Definition: stl_map.h:381
map(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a map with no elements.
Definition: stl_map.h:171
iterator emplace_hint(const_iterator __pos, _Args &&...__args)
Attempts to build and insert a std::pair into the map.
Definition: stl_map.h:604
iterator begin() noexcept
Definition: stl_map.h:354
std::pair< iterator, bool > emplace(_Args &&...__args)
Attempts to build and insert a std::pair into the map.
Definition: stl_map.h:574
map & operator=(initializer_list< value_type > __l)
Map list assignment operator.
Definition: stl_map.h:334
map(const allocator_type &__a)
Allocator-extended default constructor.
Definition: stl_map.h:216
value_compare value_comp() const
Definition: stl_map.h:842
The standard allocator, as per [20.4].
Definition: allocator.h:92
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
Definition: functional:2534
Common iterator class.
initializer_list
iterator erase(const_iterator __position)
Erases an element from a map.
Definition: stl_map.h:724
iterator end() noexcept
Definition: stl_map.h:372
const_iterator find(const key_type &__x) const
Tries to locate an element in a map.
Definition: stl_map.h:873
size_type size() const noexcept
Definition: stl_map.h:468
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_map.h:915
noexcept(is_nothrow_copy_constructible< _Compare >::value)
Map move constructor.
Definition: stl_map.h:194
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_map.h:760
const_iterator begin() const noexcept
Definition: stl_map.h:363
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_map.h:925
const_iterator cend() const noexcept
Definition: stl_map.h:436
reverse_iterator rend() noexcept
Definition: stl_map.h:408