libstdc++
|
00001 // Map implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2019 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996,1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_map.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{map} 00054 */ 00055 00056 #ifndef _STL_MAP_H 00057 #define _STL_MAP_H 1 00058 00059 #include <bits/functexcept.h> 00060 #include <bits/concept_check.h> 00061 #if __cplusplus >= 201103L 00062 #include <initializer_list> 00063 #include <tuple> 00064 #endif 00065 00066 namespace std _GLIBCXX_VISIBILITY(default) 00067 { 00068 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00069 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00070 00071 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00072 class multimap; 00073 00074 /** 00075 * @brief A standard container made up of (key,value) pairs, which can be 00076 * retrieved based on a key, in logarithmic time. 00077 * 00078 * @ingroup associative_containers 00079 * 00080 * @tparam _Key Type of key objects. 00081 * @tparam _Tp Type of mapped objects. 00082 * @tparam _Compare Comparison function object type, defaults to less<_Key>. 00083 * @tparam _Alloc Allocator type, defaults to 00084 * allocator<pair<const _Key, _Tp>. 00085 * 00086 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00087 * <a href="tables.html#66">reversible container</a>, and an 00088 * <a href="tables.html#69">associative container</a> (using unique keys). 00089 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the 00090 * value_type is std::pair<const Key,T>. 00091 * 00092 * Maps support bidirectional iterators. 00093 * 00094 * The private tree data is declared exactly the same way for map and 00095 * multimap; the distinction is made entirely in how the tree functions are 00096 * called (*_unique versus *_equal, same as the standard). 00097 */ 00098 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 00099 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > > 00100 class map 00101 { 00102 public: 00103 typedef _Key key_type; 00104 typedef _Tp mapped_type; 00105 typedef std::pair<const _Key, _Tp> value_type; 00106 typedef _Compare key_compare; 00107 typedef _Alloc allocator_type; 00108 00109 private: 00110 #ifdef _GLIBCXX_CONCEPT_CHECKS 00111 // concept requirements 00112 typedef typename _Alloc::value_type _Alloc_value_type; 00113 # if __cplusplus < 201103L 00114 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00115 # endif 00116 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00117 _BinaryFunctionConcept) 00118 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept) 00119 #endif 00120 00121 #if __cplusplus >= 201103L && defined(__STRICT_ANSI__) 00122 static_assert(is_same<typename _Alloc::value_type, value_type>::value, 00123 "std::map must have the same value_type as its allocator"); 00124 #endif 00125 00126 public: 00127 class value_compare 00128 : public std::binary_function<value_type, value_type, bool> 00129 { 00130 friend class map<_Key, _Tp, _Compare, _Alloc>; 00131 protected: 00132 _Compare comp; 00133 00134 value_compare(_Compare __c) 00135 : comp(__c) { } 00136 00137 public: 00138 bool operator()(const value_type& __x, const value_type& __y) const 00139 { return comp(__x.first, __y.first); } 00140 }; 00141 00142 private: 00143 /// This turns a red-black tree into a [multi]map. 00144 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 00145 rebind<value_type>::other _Pair_alloc_type; 00146 00147 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>, 00148 key_compare, _Pair_alloc_type> _Rep_type; 00149 00150 /// The actual tree structure. 00151 _Rep_type _M_t; 00152 00153 typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits; 00154 00155 public: 00156 // many of these are specified differently in ISO, but the following are 00157 // "functionally equivalent" 00158 typedef typename _Alloc_traits::pointer pointer; 00159 typedef typename _Alloc_traits::const_pointer const_pointer; 00160 typedef typename _Alloc_traits::reference reference; 00161 typedef typename _Alloc_traits::const_reference const_reference; 00162 typedef typename _Rep_type::iterator iterator; 00163 typedef typename _Rep_type::const_iterator const_iterator; 00164 typedef typename _Rep_type::size_type size_type; 00165 typedef typename _Rep_type::difference_type difference_type; 00166 typedef typename _Rep_type::reverse_iterator reverse_iterator; 00167 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00168 00169 #if __cplusplus > 201402L 00170 using node_type = typename _Rep_type::node_type; 00171 using insert_return_type = typename _Rep_type::insert_return_type; 00172 #endif 00173 00174 // [23.3.1.1] construct/copy/destroy 00175 // (get_allocator() is also listed in this section) 00176 00177 /** 00178 * @brief Default constructor creates no elements. 00179 */ 00180 #if __cplusplus < 201103L 00181 map() : _M_t() { } 00182 #else 00183 map() = default; 00184 #endif 00185 00186 /** 00187 * @brief Creates a %map with no elements. 00188 * @param __comp A comparison object. 00189 * @param __a An allocator object. 00190 */ 00191 explicit 00192 map(const _Compare& __comp, 00193 const allocator_type& __a = allocator_type()) 00194 : _M_t(__comp, _Pair_alloc_type(__a)) { } 00195 00196 /** 00197 * @brief %Map copy constructor. 00198 * 00199 * Whether the allocator is copied depends on the allocator traits. 00200 */ 00201 #if __cplusplus < 201103L 00202 map(const map& __x) 00203 : _M_t(__x._M_t) { } 00204 #else 00205 map(const map&) = default; 00206 00207 /** 00208 * @brief %Map move constructor. 00209 * 00210 * The newly-created %map contains the exact contents of the moved 00211 * instance. The moved instance is a valid, but unspecified, %map. 00212 */ 00213 map(map&&) = default; 00214 00215 /** 00216 * @brief Builds a %map from an initializer_list. 00217 * @param __l An initializer_list. 00218 * @param __comp A comparison object. 00219 * @param __a An allocator object. 00220 * 00221 * Create a %map consisting of copies of the elements in the 00222 * initializer_list @a __l. 00223 * This is linear in N if the range is already sorted, and NlogN 00224 * otherwise (where N is @a __l.size()). 00225 */ 00226 map(initializer_list<value_type> __l, 00227 const _Compare& __comp = _Compare(), 00228 const allocator_type& __a = allocator_type()) 00229 : _M_t(__comp, _Pair_alloc_type(__a)) 00230 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); } 00231 00232 /// Allocator-extended default constructor. 00233 explicit 00234 map(const allocator_type& __a) 00235 : _M_t(_Pair_alloc_type(__a)) { } 00236 00237 /// Allocator-extended copy constructor. 00238 map(const map& __m, const allocator_type& __a) 00239 : _M_t(__m._M_t, _Pair_alloc_type(__a)) { } 00240 00241 /// Allocator-extended move constructor. 00242 map(map&& __m, const allocator_type& __a) 00243 noexcept(is_nothrow_copy_constructible<_Compare>::value 00244 && _Alloc_traits::_S_always_equal()) 00245 : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { } 00246 00247 /// Allocator-extended initialier-list constructor. 00248 map(initializer_list<value_type> __l, const allocator_type& __a) 00249 : _M_t(_Pair_alloc_type(__a)) 00250 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); } 00251 00252 /// Allocator-extended range constructor. 00253 template<typename _InputIterator> 00254 map(_InputIterator __first, _InputIterator __last, 00255 const allocator_type& __a) 00256 : _M_t(_Pair_alloc_type(__a)) 00257 { _M_t._M_insert_range_unique(__first, __last); } 00258 #endif 00259 00260 /** 00261 * @brief Builds a %map from a range. 00262 * @param __first An input iterator. 00263 * @param __last An input iterator. 00264 * 00265 * Create a %map consisting of copies of the elements from 00266 * [__first,__last). This is linear in N if the range is 00267 * already sorted, and NlogN otherwise (where N is 00268 * distance(__first,__last)). 00269 */ 00270 template<typename _InputIterator> 00271 map(_InputIterator __first, _InputIterator __last) 00272 : _M_t() 00273 { _M_t._M_insert_range_unique(__first, __last); } 00274 00275 /** 00276 * @brief Builds a %map from a range. 00277 * @param __first An input iterator. 00278 * @param __last An input iterator. 00279 * @param __comp A comparison functor. 00280 * @param __a An allocator object. 00281 * 00282 * Create a %map consisting of copies of the elements from 00283 * [__first,__last). This is linear in N if the range is 00284 * already sorted, and NlogN otherwise (where N is 00285 * distance(__first,__last)). 00286 */ 00287 template<typename _InputIterator> 00288 map(_InputIterator __first, _InputIterator __last, 00289 const _Compare& __comp, 00290 const allocator_type& __a = allocator_type()) 00291 : _M_t(__comp, _Pair_alloc_type(__a)) 00292 { _M_t._M_insert_range_unique(__first, __last); } 00293 00294 #if __cplusplus >= 201103L 00295 /** 00296 * The dtor only erases the elements, and note that if the elements 00297 * themselves are pointers, the pointed-to memory is not touched in any 00298 * way. Managing the pointer is the user's responsibility. 00299 */ 00300 ~map() = default; 00301 #endif 00302 00303 /** 00304 * @brief %Map assignment operator. 00305 * 00306 * Whether the allocator is copied depends on the allocator traits. 00307 */ 00308 #if __cplusplus < 201103L 00309 map& 00310 operator=(const map& __x) 00311 { 00312 _M_t = __x._M_t; 00313 return *this; 00314 } 00315 #else 00316 map& 00317 operator=(const map&) = default; 00318 00319 /// Move assignment operator. 00320 map& 00321 operator=(map&&) = default; 00322 00323 /** 00324 * @brief %Map list assignment operator. 00325 * @param __l An initializer_list. 00326 * 00327 * This function fills a %map with copies of the elements in the 00328 * initializer list @a __l. 00329 * 00330 * Note that the assignment completely changes the %map and 00331 * that the resulting %map's size is the same as the number 00332 * of elements assigned. 00333 */ 00334 map& 00335 operator=(initializer_list<value_type> __l) 00336 { 00337 _M_t._M_assign_unique(__l.begin(), __l.end()); 00338 return *this; 00339 } 00340 #endif 00341 00342 /// Get a copy of the memory allocation object. 00343 allocator_type 00344 get_allocator() const _GLIBCXX_NOEXCEPT 00345 { return allocator_type(_M_t.get_allocator()); } 00346 00347 // iterators 00348 /** 00349 * Returns a read/write iterator that points to the first pair in the 00350 * %map. 00351 * Iteration is done in ascending order according to the keys. 00352 */ 00353 iterator 00354 begin() _GLIBCXX_NOEXCEPT 00355 { return _M_t.begin(); } 00356 00357 /** 00358 * Returns a read-only (constant) iterator that points to the first pair 00359 * in the %map. Iteration is done in ascending order according to the 00360 * keys. 00361 */ 00362 const_iterator 00363 begin() const _GLIBCXX_NOEXCEPT 00364 { return _M_t.begin(); } 00365 00366 /** 00367 * Returns a read/write iterator that points one past the last 00368 * pair in the %map. Iteration is done in ascending order 00369 * according to the keys. 00370 */ 00371 iterator 00372 end() _GLIBCXX_NOEXCEPT 00373 { return _M_t.end(); } 00374 00375 /** 00376 * Returns a read-only (constant) iterator that points one past the last 00377 * pair in the %map. Iteration is done in ascending order according to 00378 * the keys. 00379 */ 00380 const_iterator 00381 end() const _GLIBCXX_NOEXCEPT 00382 { return _M_t.end(); } 00383 00384 /** 00385 * Returns a read/write reverse iterator that points to the last pair in 00386 * the %map. Iteration is done in descending order according to the 00387 * keys. 00388 */ 00389 reverse_iterator 00390 rbegin() _GLIBCXX_NOEXCEPT 00391 { return _M_t.rbegin(); } 00392 00393 /** 00394 * Returns a read-only (constant) reverse iterator that points to the 00395 * last pair in the %map. Iteration is done in descending order 00396 * according to the keys. 00397 */ 00398 const_reverse_iterator 00399 rbegin() const _GLIBCXX_NOEXCEPT 00400 { return _M_t.rbegin(); } 00401 00402 /** 00403 * Returns a read/write reverse iterator that points to one before the 00404 * first pair in the %map. Iteration is done in descending order 00405 * according to the keys. 00406 */ 00407 reverse_iterator 00408 rend() _GLIBCXX_NOEXCEPT 00409 { return _M_t.rend(); } 00410 00411 /** 00412 * Returns a read-only (constant) reverse iterator that points to one 00413 * before the first pair in the %map. Iteration is done in descending 00414 * order according to the keys. 00415 */ 00416 const_reverse_iterator 00417 rend() const _GLIBCXX_NOEXCEPT 00418 { return _M_t.rend(); } 00419 00420 #if __cplusplus >= 201103L 00421 /** 00422 * Returns a read-only (constant) iterator that points to the first pair 00423 * in the %map. Iteration is done in ascending order according to the 00424 * keys. 00425 */ 00426 const_iterator 00427 cbegin() const noexcept 00428 { return _M_t.begin(); } 00429 00430 /** 00431 * Returns a read-only (constant) iterator that points one past the last 00432 * pair in the %map. Iteration is done in ascending order according to 00433 * the keys. 00434 */ 00435 const_iterator 00436 cend() const noexcept 00437 { return _M_t.end(); } 00438 00439 /** 00440 * Returns a read-only (constant) reverse iterator that points to the 00441 * last pair in the %map. Iteration is done in descending order 00442 * according to the keys. 00443 */ 00444 const_reverse_iterator 00445 crbegin() const noexcept 00446 { return _M_t.rbegin(); } 00447 00448 /** 00449 * Returns a read-only (constant) reverse iterator that points to one 00450 * before the first pair in the %map. Iteration is done in descending 00451 * order according to the keys. 00452 */ 00453 const_reverse_iterator 00454 crend() const noexcept 00455 { return _M_t.rend(); } 00456 #endif 00457 00458 // capacity 00459 /** Returns true if the %map is empty. (Thus begin() would equal 00460 * end().) 00461 */ 00462 _GLIBCXX_NODISCARD bool 00463 empty() const _GLIBCXX_NOEXCEPT 00464 { return _M_t.empty(); } 00465 00466 /** Returns the size of the %map. */ 00467 size_type 00468 size() const _GLIBCXX_NOEXCEPT 00469 { return _M_t.size(); } 00470 00471 /** Returns the maximum size of the %map. */ 00472 size_type 00473 max_size() const _GLIBCXX_NOEXCEPT 00474 { return _M_t.max_size(); } 00475 00476 // [23.3.1.2] element access 00477 /** 00478 * @brief Subscript ( @c [] ) access to %map data. 00479 * @param __k The key for which data should be retrieved. 00480 * @return A reference to the data of the (key,data) %pair. 00481 * 00482 * Allows for easy lookup with the subscript ( @c [] ) 00483 * operator. Returns data associated with the key specified in 00484 * subscript. If the key does not exist, a pair with that key 00485 * is created using default values, which is then returned. 00486 * 00487 * Lookup requires logarithmic time. 00488 */ 00489 mapped_type& 00490 operator[](const key_type& __k) 00491 { 00492 // concept requirements 00493 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00494 00495 iterator __i = lower_bound(__k); 00496 // __i->first is greater than or equivalent to __k. 00497 if (__i == end() || key_comp()(__k, (*__i).first)) 00498 #if __cplusplus >= 201103L 00499 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, 00500 std::tuple<const key_type&>(__k), 00501 std::tuple<>()); 00502 #else 00503 __i = insert(__i, value_type(__k, mapped_type())); 00504 #endif 00505 return (*__i).second; 00506 } 00507 00508 #if __cplusplus >= 201103L 00509 mapped_type& 00510 operator[](key_type&& __k) 00511 { 00512 // concept requirements 00513 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00514 00515 iterator __i = lower_bound(__k); 00516 // __i->first is greater than or equivalent to __k. 00517 if (__i == end() || key_comp()(__k, (*__i).first)) 00518 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, 00519 std::forward_as_tuple(std::move(__k)), 00520 std::tuple<>()); 00521 return (*__i).second; 00522 } 00523 #endif 00524 00525 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00526 // DR 464. Suggestion for new member functions in standard containers. 00527 /** 00528 * @brief Access to %map data. 00529 * @param __k The key for which data should be retrieved. 00530 * @return A reference to the data whose key is equivalent to @a __k, if 00531 * such a data is present in the %map. 00532 * @throw std::out_of_range If no such data is present. 00533 */ 00534 mapped_type& 00535 at(const key_type& __k) 00536 { 00537 iterator __i = lower_bound(__k); 00538 if (__i == end() || key_comp()(__k, (*__i).first)) 00539 __throw_out_of_range(__N("map::at")); 00540 return (*__i).second; 00541 } 00542 00543 const mapped_type& 00544 at(const key_type& __k) const 00545 { 00546 const_iterator __i = lower_bound(__k); 00547 if (__i == end() || key_comp()(__k, (*__i).first)) 00548 __throw_out_of_range(__N("map::at")); 00549 return (*__i).second; 00550 } 00551 00552 // modifiers 00553 #if __cplusplus >= 201103L 00554 /** 00555 * @brief Attempts to build and insert a std::pair into the %map. 00556 * 00557 * @param __args Arguments used to generate a new pair instance (see 00558 * std::piecewise_contruct for passing arguments to each 00559 * part of the pair constructor). 00560 * 00561 * @return A pair, of which the first element is an iterator that points 00562 * to the possibly inserted pair, and the second is a bool that 00563 * is true if the pair was actually inserted. 00564 * 00565 * This function attempts to build and insert a (key, value) %pair into 00566 * the %map. 00567 * A %map relies on unique keys and thus a %pair is only inserted if its 00568 * first element (the key) is not already present in the %map. 00569 * 00570 * Insertion requires logarithmic time. 00571 */ 00572 template<typename... _Args> 00573 std::pair<iterator, bool> 00574 emplace(_Args&&... __args) 00575 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } 00576 00577 /** 00578 * @brief Attempts to build and insert a std::pair into the %map. 00579 * 00580 * @param __pos An iterator that serves as a hint as to where the pair 00581 * should be inserted. 00582 * @param __args Arguments used to generate a new pair instance (see 00583 * std::piecewise_contruct for passing arguments to each 00584 * part of the pair constructor). 00585 * @return An iterator that points to the element with key of the 00586 * std::pair built from @a __args (may or may not be that 00587 * std::pair). 00588 * 00589 * This function is not concerned about whether the insertion took place, 00590 * and thus does not return a boolean like the single-argument emplace() 00591 * does. 00592 * Note that the first parameter is only a hint and can potentially 00593 * improve the performance of the insertion process. A bad hint would 00594 * cause no gains in efficiency. 00595 * 00596 * See 00597 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00598 * for more on @a hinting. 00599 * 00600 * Insertion requires logarithmic time (if the hint is not taken). 00601 */ 00602 template<typename... _Args> 00603 iterator 00604 emplace_hint(const_iterator __pos, _Args&&... __args) 00605 { 00606 return _M_t._M_emplace_hint_unique(__pos, 00607 std::forward<_Args>(__args)...); 00608 } 00609 #endif 00610 00611 #if __cplusplus > 201402L 00612 /// Extract a node. 00613 node_type 00614 extract(const_iterator __pos) 00615 { 00616 __glibcxx_assert(__pos != end()); 00617 return _M_t.extract(__pos); 00618 } 00619 00620 /// Extract a node. 00621 node_type 00622 extract(const key_type& __x) 00623 { return _M_t.extract(__x); } 00624 00625 /// Re-insert an extracted node. 00626 insert_return_type 00627 insert(node_type&& __nh) 00628 { return _M_t._M_reinsert_node_unique(std::move(__nh)); } 00629 00630 /// Re-insert an extracted node. 00631 iterator 00632 insert(const_iterator __hint, node_type&& __nh) 00633 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); } 00634 00635 template<typename, typename> 00636 friend class std::_Rb_tree_merge_helper; 00637 00638 template<typename _C2> 00639 void 00640 merge(map<_Key, _Tp, _C2, _Alloc>& __source) 00641 { 00642 using _Merge_helper = _Rb_tree_merge_helper<map, _C2>; 00643 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source)); 00644 } 00645 00646 template<typename _C2> 00647 void 00648 merge(map<_Key, _Tp, _C2, _Alloc>&& __source) 00649 { merge(__source); } 00650 00651 template<typename _C2> 00652 void 00653 merge(multimap<_Key, _Tp, _C2, _Alloc>& __source) 00654 { 00655 using _Merge_helper = _Rb_tree_merge_helper<map, _C2>; 00656 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source)); 00657 } 00658 00659 template<typename _C2> 00660 void 00661 merge(multimap<_Key, _Tp, _C2, _Alloc>&& __source) 00662 { merge(__source); } 00663 #endif // C++17 00664 00665 #if __cplusplus > 201402L 00666 #define __cpp_lib_map_try_emplace 201411 00667 /** 00668 * @brief Attempts to build and insert a std::pair into the %map. 00669 * 00670 * @param __k Key to use for finding a possibly existing pair in 00671 * the map. 00672 * @param __args Arguments used to generate the .second for a new pair 00673 * instance. 00674 * 00675 * @return A pair, of which the first element is an iterator that points 00676 * to the possibly inserted pair, and the second is a bool that 00677 * is true if the pair was actually inserted. 00678 * 00679 * This function attempts to build and insert a (key, value) %pair into 00680 * the %map. 00681 * A %map relies on unique keys and thus a %pair is only inserted if its 00682 * first element (the key) is not already present in the %map. 00683 * If a %pair is not inserted, this function has no effect. 00684 * 00685 * Insertion requires logarithmic time. 00686 */ 00687 template <typename... _Args> 00688 pair<iterator, bool> 00689 try_emplace(const key_type& __k, _Args&&... __args) 00690 { 00691 iterator __i = lower_bound(__k); 00692 if (__i == end() || key_comp()(__k, (*__i).first)) 00693 { 00694 __i = emplace_hint(__i, std::piecewise_construct, 00695 std::forward_as_tuple(__k), 00696 std::forward_as_tuple( 00697 std::forward<_Args>(__args)...)); 00698 return {__i, true}; 00699 } 00700 return {__i, false}; 00701 } 00702 00703 // move-capable overload 00704 template <typename... _Args> 00705 pair<iterator, bool> 00706 try_emplace(key_type&& __k, _Args&&... __args) 00707 { 00708 iterator __i = lower_bound(__k); 00709 if (__i == end() || key_comp()(__k, (*__i).first)) 00710 { 00711 __i = emplace_hint(__i, std::piecewise_construct, 00712 std::forward_as_tuple(std::move(__k)), 00713 std::forward_as_tuple( 00714 std::forward<_Args>(__args)...)); 00715 return {__i, true}; 00716 } 00717 return {__i, false}; 00718 } 00719 00720 /** 00721 * @brief Attempts to build and insert a std::pair into the %map. 00722 * 00723 * @param __hint An iterator that serves as a hint as to where the 00724 * pair should be inserted. 00725 * @param __k Key to use for finding a possibly existing pair in 00726 * the map. 00727 * @param __args Arguments used to generate the .second for a new pair 00728 * instance. 00729 * @return An iterator that points to the element with key of the 00730 * std::pair built from @a __args (may or may not be that 00731 * std::pair). 00732 * 00733 * This function is not concerned about whether the insertion took place, 00734 * and thus does not return a boolean like the single-argument 00735 * try_emplace() does. However, if insertion did not take place, 00736 * this function has no effect. 00737 * Note that the first parameter is only a hint and can potentially 00738 * improve the performance of the insertion process. A bad hint would 00739 * cause no gains in efficiency. 00740 * 00741 * See 00742 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00743 * for more on @a hinting. 00744 * 00745 * Insertion requires logarithmic time (if the hint is not taken). 00746 */ 00747 template <typename... _Args> 00748 iterator 00749 try_emplace(const_iterator __hint, const key_type& __k, 00750 _Args&&... __args) 00751 { 00752 iterator __i; 00753 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); 00754 if (__true_hint.second) 00755 __i = emplace_hint(iterator(__true_hint.second), 00756 std::piecewise_construct, 00757 std::forward_as_tuple(__k), 00758 std::forward_as_tuple( 00759 std::forward<_Args>(__args)...)); 00760 else 00761 __i = iterator(__true_hint.first); 00762 return __i; 00763 } 00764 00765 // move-capable overload 00766 template <typename... _Args> 00767 iterator 00768 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args) 00769 { 00770 iterator __i; 00771 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); 00772 if (__true_hint.second) 00773 __i = emplace_hint(iterator(__true_hint.second), 00774 std::piecewise_construct, 00775 std::forward_as_tuple(std::move(__k)), 00776 std::forward_as_tuple( 00777 std::forward<_Args>(__args)...)); 00778 else 00779 __i = iterator(__true_hint.first); 00780 return __i; 00781 } 00782 #endif 00783 00784 /** 00785 * @brief Attempts to insert a std::pair into the %map. 00786 * @param __x Pair to be inserted (see std::make_pair for easy 00787 * creation of pairs). 00788 * 00789 * @return A pair, of which the first element is an iterator that 00790 * points to the possibly inserted pair, and the second is 00791 * a bool that is true if the pair was actually inserted. 00792 * 00793 * This function attempts to insert a (key, value) %pair into the %map. 00794 * A %map relies on unique keys and thus a %pair is only inserted if its 00795 * first element (the key) is not already present in the %map. 00796 * 00797 * Insertion requires logarithmic time. 00798 * @{ 00799 */ 00800 std::pair<iterator, bool> 00801 insert(const value_type& __x) 00802 { return _M_t._M_insert_unique(__x); } 00803 00804 #if __cplusplus >= 201103L 00805 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00806 // 2354. Unnecessary copying when inserting into maps with braced-init 00807 std::pair<iterator, bool> 00808 insert(value_type&& __x) 00809 { return _M_t._M_insert_unique(std::move(__x)); } 00810 00811 template<typename _Pair> 00812 __enable_if_t<is_constructible<value_type, _Pair>::value, 00813 pair<iterator, bool>> 00814 insert(_Pair&& __x) 00815 { return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); } 00816 #endif 00817 // @} 00818 00819 #if __cplusplus >= 201103L 00820 /** 00821 * @brief Attempts to insert a list of std::pairs into the %map. 00822 * @param __list A std::initializer_list<value_type> of pairs to be 00823 * inserted. 00824 * 00825 * Complexity similar to that of the range constructor. 00826 */ 00827 void 00828 insert(std::initializer_list<value_type> __list) 00829 { insert(__list.begin(), __list.end()); } 00830 #endif 00831 00832 /** 00833 * @brief Attempts to insert a std::pair into the %map. 00834 * @param __position An iterator that serves as a hint as to where the 00835 * pair should be inserted. 00836 * @param __x Pair to be inserted (see std::make_pair for easy creation 00837 * of pairs). 00838 * @return An iterator that points to the element with key of 00839 * @a __x (may or may not be the %pair passed in). 00840 * 00841 00842 * This function is not concerned about whether the insertion 00843 * took place, and thus does not return a boolean like the 00844 * single-argument insert() does. Note that the first 00845 * parameter is only a hint and can potentially improve the 00846 * performance of the insertion process. A bad hint would 00847 * cause no gains in efficiency. 00848 * 00849 * See 00850 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00851 * for more on @a hinting. 00852 * 00853 * Insertion requires logarithmic time (if the hint is not taken). 00854 * @{ 00855 */ 00856 iterator 00857 #if __cplusplus >= 201103L 00858 insert(const_iterator __position, const value_type& __x) 00859 #else 00860 insert(iterator __position, const value_type& __x) 00861 #endif 00862 { return _M_t._M_insert_unique_(__position, __x); } 00863 00864 #if __cplusplus >= 201103L 00865 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00866 // 2354. Unnecessary copying when inserting into maps with braced-init 00867 iterator 00868 insert(const_iterator __position, value_type&& __x) 00869 { return _M_t._M_insert_unique_(__position, std::move(__x)); } 00870 00871 template<typename _Pair> 00872 __enable_if_t<is_constructible<value_type, _Pair>::value, iterator> 00873 insert(const_iterator __position, _Pair&& __x) 00874 { 00875 return _M_t._M_emplace_hint_unique(__position, 00876 std::forward<_Pair>(__x)); 00877 } 00878 #endif 00879 // @} 00880 00881 /** 00882 * @brief Template function that attempts to insert a range of elements. 00883 * @param __first Iterator pointing to the start of the range to be 00884 * inserted. 00885 * @param __last Iterator pointing to the end of the range. 00886 * 00887 * Complexity similar to that of the range constructor. 00888 */ 00889 template<typename _InputIterator> 00890 void 00891 insert(_InputIterator __first, _InputIterator __last) 00892 { _M_t._M_insert_range_unique(__first, __last); } 00893 00894 #if __cplusplus > 201402L 00895 #define __cpp_lib_map_insertion 201411 00896 /** 00897 * @brief Attempts to insert or assign a std::pair into the %map. 00898 * @param __k Key to use for finding a possibly existing pair in 00899 * the map. 00900 * @param __obj Argument used to generate the .second for a pair 00901 * instance. 00902 * 00903 * @return A pair, of which the first element is an iterator that 00904 * points to the possibly inserted pair, and the second is 00905 * a bool that is true if the pair was actually inserted. 00906 * 00907 * This function attempts to insert a (key, value) %pair into the %map. 00908 * A %map relies on unique keys and thus a %pair is only inserted if its 00909 * first element (the key) is not already present in the %map. 00910 * If the %pair was already in the %map, the .second of the %pair 00911 * is assigned from __obj. 00912 * 00913 * Insertion requires logarithmic time. 00914 */ 00915 template <typename _Obj> 00916 pair<iterator, bool> 00917 insert_or_assign(const key_type& __k, _Obj&& __obj) 00918 { 00919 iterator __i = lower_bound(__k); 00920 if (__i == end() || key_comp()(__k, (*__i).first)) 00921 { 00922 __i = emplace_hint(__i, std::piecewise_construct, 00923 std::forward_as_tuple(__k), 00924 std::forward_as_tuple( 00925 std::forward<_Obj>(__obj))); 00926 return {__i, true}; 00927 } 00928 (*__i).second = std::forward<_Obj>(__obj); 00929 return {__i, false}; 00930 } 00931 00932 // move-capable overload 00933 template <typename _Obj> 00934 pair<iterator, bool> 00935 insert_or_assign(key_type&& __k, _Obj&& __obj) 00936 { 00937 iterator __i = lower_bound(__k); 00938 if (__i == end() || key_comp()(__k, (*__i).first)) 00939 { 00940 __i = emplace_hint(__i, std::piecewise_construct, 00941 std::forward_as_tuple(std::move(__k)), 00942 std::forward_as_tuple( 00943 std::forward<_Obj>(__obj))); 00944 return {__i, true}; 00945 } 00946 (*__i).second = std::forward<_Obj>(__obj); 00947 return {__i, false}; 00948 } 00949 00950 /** 00951 * @brief Attempts to insert or assign a std::pair into the %map. 00952 * @param __hint An iterator that serves as a hint as to where the 00953 * pair should be inserted. 00954 * @param __k Key to use for finding a possibly existing pair in 00955 * the map. 00956 * @param __obj Argument used to generate the .second for a pair 00957 * instance. 00958 * 00959 * @return An iterator that points to the element with key of 00960 * @a __x (may or may not be the %pair passed in). 00961 * 00962 * This function attempts to insert a (key, value) %pair into the %map. 00963 * A %map relies on unique keys and thus a %pair is only inserted if its 00964 * first element (the key) is not already present in the %map. 00965 * If the %pair was already in the %map, the .second of the %pair 00966 * is assigned from __obj. 00967 * 00968 * Insertion requires logarithmic time. 00969 */ 00970 template <typename _Obj> 00971 iterator 00972 insert_or_assign(const_iterator __hint, 00973 const key_type& __k, _Obj&& __obj) 00974 { 00975 iterator __i; 00976 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); 00977 if (__true_hint.second) 00978 { 00979 return emplace_hint(iterator(__true_hint.second), 00980 std::piecewise_construct, 00981 std::forward_as_tuple(__k), 00982 std::forward_as_tuple( 00983 std::forward<_Obj>(__obj))); 00984 } 00985 __i = iterator(__true_hint.first); 00986 (*__i).second = std::forward<_Obj>(__obj); 00987 return __i; 00988 } 00989 00990 // move-capable overload 00991 template <typename _Obj> 00992 iterator 00993 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj) 00994 { 00995 iterator __i; 00996 auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); 00997 if (__true_hint.second) 00998 { 00999 return emplace_hint(iterator(__true_hint.second), 01000 std::piecewise_construct, 01001 std::forward_as_tuple(std::move(__k)), 01002 std::forward_as_tuple( 01003 std::forward<_Obj>(__obj))); 01004 } 01005 __i = iterator(__true_hint.first); 01006 (*__i).second = std::forward<_Obj>(__obj); 01007 return __i; 01008 } 01009 #endif 01010 01011 #if __cplusplus >= 201103L 01012 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01013 // DR 130. Associative erase should return an iterator. 01014 /** 01015 * @brief Erases an element from a %map. 01016 * @param __position An iterator pointing to the element to be erased. 01017 * @return An iterator pointing to the element immediately following 01018 * @a position prior to the element being erased. If no such 01019 * element exists, end() is returned. 01020 * 01021 * This function erases an element, pointed to by the given 01022 * iterator, from a %map. Note that this function only erases 01023 * the element, and that if the element is itself a pointer, 01024 * the pointed-to memory is not touched in any way. Managing 01025 * the pointer is the user's responsibility. 01026 * 01027 * @{ 01028 */ 01029 iterator 01030 erase(const_iterator __position) 01031 { return _M_t.erase(__position); } 01032 01033 // LWG 2059 01034 _GLIBCXX_ABI_TAG_CXX11 01035 iterator 01036 erase(iterator __position) 01037 { return _M_t.erase(__position); } 01038 // @} 01039 #else 01040 /** 01041 * @brief Erases an element from a %map. 01042 * @param __position An iterator pointing to the element to be erased. 01043 * 01044 * This function erases an element, pointed to by the given 01045 * iterator, from a %map. Note that this function only erases 01046 * the element, and that if the element is itself a pointer, 01047 * the pointed-to memory is not touched in any way. Managing 01048 * the pointer is the user's responsibility. 01049 */ 01050 void 01051 erase(iterator __position) 01052 { _M_t.erase(__position); } 01053 #endif 01054 01055 /** 01056 * @brief Erases elements according to the provided key. 01057 * @param __x Key of element to be erased. 01058 * @return The number of elements erased. 01059 * 01060 * This function erases all the elements located by the given key from 01061 * a %map. 01062 * Note that this function only erases the element, and that if 01063 * the element is itself a pointer, the pointed-to memory is not touched 01064 * in any way. Managing the pointer is the user's responsibility. 01065 */ 01066 size_type 01067 erase(const key_type& __x) 01068 { return _M_t.erase(__x); } 01069 01070 #if __cplusplus >= 201103L 01071 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01072 // DR 130. Associative erase should return an iterator. 01073 /** 01074 * @brief Erases a [first,last) range of elements from a %map. 01075 * @param __first Iterator pointing to the start of the range to be 01076 * erased. 01077 * @param __last Iterator pointing to the end of the range to 01078 * be erased. 01079 * @return The iterator @a __last. 01080 * 01081 * This function erases a sequence of elements from a %map. 01082 * Note that this function only erases the element, and that if 01083 * the element is itself a pointer, the pointed-to memory is not touched 01084 * in any way. Managing the pointer is the user's responsibility. 01085 */ 01086 iterator 01087 erase(const_iterator __first, const_iterator __last) 01088 { return _M_t.erase(__first, __last); } 01089 #else 01090 /** 01091 * @brief Erases a [__first,__last) range of elements from a %map. 01092 * @param __first Iterator pointing to the start of the range to be 01093 * erased. 01094 * @param __last Iterator pointing to the end of the range to 01095 * be erased. 01096 * 01097 * This function erases a sequence of elements from a %map. 01098 * Note that this function only erases the element, and that if 01099 * the element is itself a pointer, the pointed-to memory is not touched 01100 * in any way. Managing the pointer is the user's responsibility. 01101 */ 01102 void 01103 erase(iterator __first, iterator __last) 01104 { _M_t.erase(__first, __last); } 01105 #endif 01106 01107 /** 01108 * @brief Swaps data with another %map. 01109 * @param __x A %map of the same element and allocator types. 01110 * 01111 * This exchanges the elements between two maps in constant 01112 * time. (It is only swapping a pointer, an integer, and an 01113 * instance of the @c Compare type (which itself is often 01114 * stateless and empty), so it should be quite fast.) Note 01115 * that the global std::swap() function is specialized such 01116 * that std::swap(m1,m2) will feed to this function. 01117 * 01118 * Whether the allocators are swapped depends on the allocator traits. 01119 */ 01120 void 01121 swap(map& __x) 01122 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value) 01123 { _M_t.swap(__x._M_t); } 01124 01125 /** 01126 * Erases all elements in a %map. Note that this function only 01127 * erases the elements, and that if the elements themselves are 01128 * pointers, the pointed-to memory is not touched in any way. 01129 * Managing the pointer is the user's responsibility. 01130 */ 01131 void 01132 clear() _GLIBCXX_NOEXCEPT 01133 { _M_t.clear(); } 01134 01135 // observers 01136 /** 01137 * Returns the key comparison object out of which the %map was 01138 * constructed. 01139 */ 01140 key_compare 01141 key_comp() const 01142 { return _M_t.key_comp(); } 01143 01144 /** 01145 * Returns a value comparison object, built from the key comparison 01146 * object out of which the %map was constructed. 01147 */ 01148 value_compare 01149 value_comp() const 01150 { return value_compare(_M_t.key_comp()); } 01151 01152 // [23.3.1.3] map operations 01153 01154 //@{ 01155 /** 01156 * @brief Tries to locate an element in a %map. 01157 * @param __x Key of (key, value) %pair to be located. 01158 * @return Iterator pointing to sought-after element, or end() if not 01159 * found. 01160 * 01161 * This function takes a key and tries to locate the element with which 01162 * the key matches. If successful the function returns an iterator 01163 * pointing to the sought after %pair. If unsuccessful it returns the 01164 * past-the-end ( @c end() ) iterator. 01165 */ 01166 01167 iterator 01168 find(const key_type& __x) 01169 { return _M_t.find(__x); } 01170 01171 #if __cplusplus > 201103L 01172 template<typename _Kt> 01173 auto 01174 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x)) 01175 { return _M_t._M_find_tr(__x); } 01176 #endif 01177 //@} 01178 01179 //@{ 01180 /** 01181 * @brief Tries to locate an element in a %map. 01182 * @param __x Key of (key, value) %pair to be located. 01183 * @return Read-only (constant) iterator pointing to sought-after 01184 * element, or end() if not found. 01185 * 01186 * This function takes a key and tries to locate the element with which 01187 * the key matches. If successful the function returns a constant 01188 * iterator pointing to the sought after %pair. If unsuccessful it 01189 * returns the past-the-end ( @c end() ) iterator. 01190 */ 01191 01192 const_iterator 01193 find(const key_type& __x) const 01194 { return _M_t.find(__x); } 01195 01196 #if __cplusplus > 201103L 01197 template<typename _Kt> 01198 auto 01199 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x)) 01200 { return _M_t._M_find_tr(__x); } 01201 #endif 01202 //@} 01203 01204 //@{ 01205 /** 01206 * @brief Finds the number of elements with given key. 01207 * @param __x Key of (key, value) pairs to be located. 01208 * @return Number of elements with specified key. 01209 * 01210 * This function only makes sense for multimaps; for map the result will 01211 * either be 0 (not present) or 1 (present). 01212 */ 01213 size_type 01214 count(const key_type& __x) const 01215 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 01216 01217 #if __cplusplus > 201103L 01218 template<typename _Kt> 01219 auto 01220 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x)) 01221 { return _M_t._M_count_tr(__x); } 01222 #endif 01223 //@} 01224 01225 #if __cplusplus > 201703L 01226 //@{ 01227 /** 01228 * @brief Finds whether an element with the given key exists. 01229 * @param __x Key of (key, value) pairs to be located. 01230 * @return True if there is an element with the specified key. 01231 */ 01232 bool 01233 contains(const key_type& __x) const 01234 { return _M_t.find(__x) != _M_t.end(); } 01235 01236 template<typename _Kt> 01237 auto 01238 contains(const _Kt& __x) const 01239 -> decltype(_M_t._M_find_tr(__x), void(), true) 01240 { return _M_t._M_find_tr(__x) != _M_t.end(); } 01241 //@} 01242 #endif 01243 01244 //@{ 01245 /** 01246 * @brief Finds the beginning of a subsequence matching given key. 01247 * @param __x Key of (key, value) pair to be located. 01248 * @return Iterator pointing to first element equal to or greater 01249 * than key, or end(). 01250 * 01251 * This function returns the first element of a subsequence of elements 01252 * that matches the given key. If unsuccessful it returns an iterator 01253 * pointing to the first element that has a greater value than given key 01254 * or end() if no such element exists. 01255 */ 01256 iterator 01257 lower_bound(const key_type& __x) 01258 { return _M_t.lower_bound(__x); } 01259 01260 #if __cplusplus > 201103L 01261 template<typename _Kt> 01262 auto 01263 lower_bound(const _Kt& __x) 01264 -> decltype(iterator(_M_t._M_lower_bound_tr(__x))) 01265 { return iterator(_M_t._M_lower_bound_tr(__x)); } 01266 #endif 01267 //@} 01268 01269 //@{ 01270 /** 01271 * @brief Finds the beginning of a subsequence matching given key. 01272 * @param __x Key of (key, value) pair to be located. 01273 * @return Read-only (constant) iterator pointing to first element 01274 * equal to or greater than key, or end(). 01275 * 01276 * This function returns the first element of a subsequence of elements 01277 * that matches the given key. If unsuccessful it returns an iterator 01278 * pointing to the first element that has a greater value than given key 01279 * or end() if no such element exists. 01280 */ 01281 const_iterator 01282 lower_bound(const key_type& __x) const 01283 { return _M_t.lower_bound(__x); } 01284 01285 #if __cplusplus > 201103L 01286 template<typename _Kt> 01287 auto 01288 lower_bound(const _Kt& __x) const 01289 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x))) 01290 { return const_iterator(_M_t._M_lower_bound_tr(__x)); } 01291 #endif 01292 //@} 01293 01294 //@{ 01295 /** 01296 * @brief Finds the end of a subsequence matching given key. 01297 * @param __x Key of (key, value) pair to be located. 01298 * @return Iterator pointing to the first element 01299 * greater than key, or end(). 01300 */ 01301 iterator 01302 upper_bound(const key_type& __x) 01303 { return _M_t.upper_bound(__x); } 01304 01305 #if __cplusplus > 201103L 01306 template<typename _Kt> 01307 auto 01308 upper_bound(const _Kt& __x) 01309 -> decltype(iterator(_M_t._M_upper_bound_tr(__x))) 01310 { return iterator(_M_t._M_upper_bound_tr(__x)); } 01311 #endif 01312 //@} 01313 01314 //@{ 01315 /** 01316 * @brief Finds the end of a subsequence matching given key. 01317 * @param __x Key of (key, value) pair to be located. 01318 * @return Read-only (constant) iterator pointing to first iterator 01319 * greater than key, or end(). 01320 */ 01321 const_iterator 01322 upper_bound(const key_type& __x) const 01323 { return _M_t.upper_bound(__x); } 01324 01325 #if __cplusplus > 201103L 01326 template<typename _Kt> 01327 auto 01328 upper_bound(const _Kt& __x) const 01329 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x))) 01330 { return const_iterator(_M_t._M_upper_bound_tr(__x)); } 01331 #endif 01332 //@} 01333 01334 //@{ 01335 /** 01336 * @brief Finds a subsequence matching given key. 01337 * @param __x Key of (key, value) pairs to be located. 01338 * @return Pair of iterators that possibly points to the subsequence 01339 * matching given key. 01340 * 01341 * This function is equivalent to 01342 * @code 01343 * std::make_pair(c.lower_bound(val), 01344 * c.upper_bound(val)) 01345 * @endcode 01346 * (but is faster than making the calls separately). 01347 * 01348 * This function probably only makes sense for multimaps. 01349 */ 01350 std::pair<iterator, iterator> 01351 equal_range(const key_type& __x) 01352 { return _M_t.equal_range(__x); } 01353 01354 #if __cplusplus > 201103L 01355 template<typename _Kt> 01356 auto 01357 equal_range(const _Kt& __x) 01358 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x))) 01359 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); } 01360 #endif 01361 //@} 01362 01363 //@{ 01364 /** 01365 * @brief Finds a subsequence matching given key. 01366 * @param __x Key of (key, value) pairs to be located. 01367 * @return Pair of read-only (constant) iterators that possibly points 01368 * to the subsequence matching given key. 01369 * 01370 * This function is equivalent to 01371 * @code 01372 * std::make_pair(c.lower_bound(val), 01373 * c.upper_bound(val)) 01374 * @endcode 01375 * (but is faster than making the calls separately). 01376 * 01377 * This function probably only makes sense for multimaps. 01378 */ 01379 std::pair<const_iterator, const_iterator> 01380 equal_range(const key_type& __x) const 01381 { return _M_t.equal_range(__x); } 01382 01383 #if __cplusplus > 201103L 01384 template<typename _Kt> 01385 auto 01386 equal_range(const _Kt& __x) const 01387 -> decltype(pair<const_iterator, const_iterator>( 01388 _M_t._M_equal_range_tr(__x))) 01389 { 01390 return pair<const_iterator, const_iterator>( 01391 _M_t._M_equal_range_tr(__x)); 01392 } 01393 #endif 01394 //@} 01395 01396 template<typename _K1, typename _T1, typename _C1, typename _A1> 01397 friend bool 01398 operator==(const map<_K1, _T1, _C1, _A1>&, 01399 const map<_K1, _T1, _C1, _A1>&); 01400 01401 template<typename _K1, typename _T1, typename _C1, typename _A1> 01402 friend bool 01403 operator<(const map<_K1, _T1, _C1, _A1>&, 01404 const map<_K1, _T1, _C1, _A1>&); 01405 }; 01406 01407 01408 #if __cpp_deduction_guides >= 201606 01409 01410 template<typename _InputIterator, 01411 typename _Compare = less<__iter_key_t<_InputIterator>>, 01412 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>, 01413 typename = _RequireInputIter<_InputIterator>, 01414 typename = _RequireNotAllocator<_Compare>, 01415 typename = _RequireAllocator<_Allocator>> 01416 map(_InputIterator, _InputIterator, 01417 _Compare = _Compare(), _Allocator = _Allocator()) 01418 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, 01419 _Compare, _Allocator>; 01420 01421 template<typename _Key, typename _Tp, typename _Compare = less<_Key>, 01422 typename _Allocator = allocator<pair<const _Key, _Tp>>, 01423 typename = _RequireNotAllocator<_Compare>, 01424 typename = _RequireAllocator<_Allocator>> 01425 map(initializer_list<pair<_Key, _Tp>>, 01426 _Compare = _Compare(), _Allocator = _Allocator()) 01427 -> map<_Key, _Tp, _Compare, _Allocator>; 01428 01429 template <typename _InputIterator, typename _Allocator, 01430 typename = _RequireInputIter<_InputIterator>, 01431 typename = _RequireAllocator<_Allocator>> 01432 map(_InputIterator, _InputIterator, _Allocator) 01433 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, 01434 less<__iter_key_t<_InputIterator>>, _Allocator>; 01435 01436 template<typename _Key, typename _Tp, typename _Allocator, 01437 typename = _RequireAllocator<_Allocator>> 01438 map(initializer_list<pair<_Key, _Tp>>, _Allocator) 01439 -> map<_Key, _Tp, less<_Key>, _Allocator>; 01440 01441 #endif 01442 01443 /** 01444 * @brief Map equality comparison. 01445 * @param __x A %map. 01446 * @param __y A %map of the same type as @a x. 01447 * @return True iff the size and elements of the maps are equal. 01448 * 01449 * This is an equivalence relation. It is linear in the size of the 01450 * maps. Maps are considered equivalent if their sizes are equal, 01451 * and if corresponding elements compare equal. 01452 */ 01453 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01454 inline bool 01455 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01456 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01457 { return __x._M_t == __y._M_t; } 01458 01459 /** 01460 * @brief Map ordering relation. 01461 * @param __x A %map. 01462 * @param __y A %map of the same type as @a x. 01463 * @return True iff @a x is lexicographically less than @a y. 01464 * 01465 * This is a total ordering relation. It is linear in the size of the 01466 * maps. The elements must be comparable with @c <. 01467 * 01468 * See std::lexicographical_compare() for how the determination is made. 01469 */ 01470 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01471 inline bool 01472 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01473 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01474 { return __x._M_t < __y._M_t; } 01475 01476 /// Based on operator== 01477 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01478 inline bool 01479 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01480 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01481 { return !(__x == __y); } 01482 01483 /// Based on operator< 01484 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01485 inline bool 01486 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01487 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01488 { return __y < __x; } 01489 01490 /// Based on operator< 01491 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01492 inline bool 01493 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01494 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01495 { return !(__y < __x); } 01496 01497 /// Based on operator< 01498 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01499 inline bool 01500 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01501 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01502 { return !(__x < __y); } 01503 01504 /// See std::map::swap(). 01505 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01506 inline void 01507 swap(map<_Key, _Tp, _Compare, _Alloc>& __x, 01508 map<_Key, _Tp, _Compare, _Alloc>& __y) 01509 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) 01510 { __x.swap(__y); } 01511 01512 _GLIBCXX_END_NAMESPACE_CONTAINER 01513 01514 #if __cplusplus > 201402L 01515 // Allow std::map access to internals of compatible maps. 01516 template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc, 01517 typename _Cmp2> 01518 struct 01519 _Rb_tree_merge_helper<_GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>, 01520 _Cmp2> 01521 { 01522 private: 01523 friend class _GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>; 01524 01525 static auto& 01526 _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map) 01527 { return __map._M_t; } 01528 01529 static auto& 01530 _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map) 01531 { return __map._M_t; } 01532 }; 01533 #endif // C++17 01534 01535 _GLIBCXX_END_NAMESPACE_VERSION 01536 } // namespace std 01537 01538 #endif /* _STL_MAP_H */