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