1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_vector.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{vector}
57 #define _STL_VECTOR_H 1
59 #include <bits/stl_iterator_base_funcs.h>
60 #include <bits/functexcept.h>
61 #include <bits/concept_check.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
66 #include <debug/assertions.h>
68 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
70 __sanitizer_annotate_contiguous_container(const void*, const void*,
71 const void*, const void*);
74 namespace std
_GLIBCXX_VISIBILITY(default)
76 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
79 /// See bits/stl_deque.h's _Deque_base for an explanation.
80 template<typename _Tp
, typename _Alloc
>
83 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
84 rebind
<_Tp
>::other _Tp_alloc_type
;
85 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
88 struct _Vector_impl_data
92 pointer _M_end_of_storage
;
94 _Vector_impl_data() _GLIBCXX_NOEXCEPT
95 : _M_start(), _M_finish(), _M_end_of_storage()
98 #if __cplusplus >= 201103L
99 _Vector_impl_data(_Vector_impl_data
&& __x
) noexcept
100 : _M_start(__x
._M_start
), _M_finish(__x
._M_finish
),
101 _M_end_of_storage(__x
._M_end_of_storage
)
102 { __x
._M_start
= __x
._M_finish
= __x
._M_end_of_storage
= pointer(); }
106 _M_copy_data(_Vector_impl_data
const& __x
) _GLIBCXX_NOEXCEPT
108 _M_start
= __x
._M_start
;
109 _M_finish
= __x
._M_finish
;
110 _M_end_of_storage
= __x
._M_end_of_storage
;
114 _M_swap_data(_Vector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
116 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
117 // information used by TBAA.
118 _Vector_impl_data __tmp
;
119 __tmp
._M_copy_data(*this);
121 __x
._M_copy_data(__tmp
);
126 : public _Tp_alloc_type
, public _Vector_impl_data
128 _Vector_impl() _GLIBCXX_NOEXCEPT_IF( noexcept(_Tp_alloc_type()) )
132 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
133 : _Tp_alloc_type(__a
)
136 #if __cplusplus >= 201103L
137 // Not defaulted, to enforce noexcept(true) even when
138 // !is_nothrow_move_constructible<_Tp_alloc_type>.
139 _Vector_impl(_Vector_impl
&& __x
) noexcept
140 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
143 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
144 : _Tp_alloc_type(std::move(__a
))
147 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
148 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
152 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
153 template<typename
= _Tp_alloc_type
>
156 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
157 ::size_type size_type
;
159 static void _S_shrink(_Vector_impl
&, size_type
) { }
160 static void _S_on_dealloc(_Vector_impl
&) { }
162 typedef _Vector_impl
& _Reinit
;
166 _Grow(_Vector_impl
&, size_type
) { }
167 void _M_grew(size_type
) { }
171 // Enable ASan annotations for memory obtained from std::allocator.
172 template<typename _Up
>
173 struct _Asan
<allocator
<_Up
> >
175 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
176 ::size_type size_type
;
178 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
179 // mark end of valid region as __curr instead of __prev.
181 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
183 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
184 __impl
._M_end_of_storage
, __prev
, __curr
);
188 _S_grow(_Vector_impl
& __impl
, size_type __n
)
189 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
192 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
193 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
196 _S_on_dealloc(_Vector_impl
& __impl
)
199 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
202 // Used on reallocation to tell ASan unused capacity is invalid.
205 explicit _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
207 // Mark unused capacity as valid again before deallocating it.
208 _S_on_dealloc(_M_impl
);
213 // Mark unused capacity as invalid after reallocation.
214 if (_M_impl
._M_start
)
215 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
219 _Vector_impl
& _M_impl
;
221 #if __cplusplus >= 201103L
222 _Reinit(const _Reinit
&) = delete;
223 _Reinit
& operator=(const _Reinit
&) = delete;
227 // Tell ASan when unused capacity is initialized to be valid.
230 _Grow(_Vector_impl
& __impl
, size_type __n
)
231 : _M_impl(__impl
), _M_n(__n
)
232 { _S_grow(_M_impl
, __n
); }
234 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
236 void _M_grew(size_type __n
) { _M_n
-= __n
; }
238 #if __cplusplus >= 201103L
239 _Grow(const _Grow
&) = delete;
240 _Grow
& operator=(const _Grow
&) = delete;
243 _Vector_impl
& _M_impl
;
248 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
249 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
250 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
251 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
252 typename _Base::_Vector_impl::template _Asan<>::_Grow \
253 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
254 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
255 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
256 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
257 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
258 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
259 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
260 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
261 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
262 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
263 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
264 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
265 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
269 typedef _Alloc allocator_type
;
272 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
273 { return this->_M_impl
; }
275 const _Tp_alloc_type
&
276 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
277 { return this->_M_impl
; }
280 get_allocator() const _GLIBCXX_NOEXCEPT
281 { return allocator_type(_M_get_Tp_allocator()); }
283 #if __cplusplus >= 201103L
284 _Vector_base() = default;
289 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
292 // Kept for ABI compatibility.
293 #if !_GLIBCXX_INLINE_VERSION
294 _Vector_base(size_t __n
)
296 { _M_create_storage(__n
); }
299 _Vector_base(size_t __n
, const allocator_type
& __a
)
301 { _M_create_storage(__n
); }
303 #if __cplusplus >= 201103L
304 _Vector_base(_Vector_base
&&) = default;
306 // Kept for ABI compatibility.
307 # if !_GLIBCXX_INLINE_VERSION
308 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
309 : _M_impl(std::move(__a
)) { }
311 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
314 if (__x
.get_allocator() == __a
)
315 this->_M_impl
._M_swap_data(__x
._M_impl
);
318 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
319 _M_create_storage(__n
);
324 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
325 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
329 ~_Vector_base() _GLIBCXX_NOEXCEPT
331 _M_deallocate(_M_impl
._M_start
,
332 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
336 _Vector_impl _M_impl
;
339 _M_allocate(size_t __n
)
341 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
342 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
346 _M_deallocate(pointer __p
, size_t __n
)
348 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
350 _Tr::deallocate(_M_impl
, __p
, __n
);
355 _M_create_storage(size_t __n
)
357 this->_M_impl
._M_start
= this->_M_allocate(__n
);
358 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
359 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
364 * @brief A standard container which offers fixed time access to
365 * individual elements in any order.
369 * @tparam _Tp Type of element.
370 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
372 * Meets the requirements of a <a href="tables.html#65">container</a>, a
373 * <a href="tables.html#66">reversible container</a>, and a
374 * <a href="tables.html#67">sequence</a>, including the
375 * <a href="tables.html#68">optional sequence requirements</a> with the
376 * %exception of @c push_front and @c pop_front.
378 * In some terminology a %vector can be described as a dynamic
379 * C-style array, it offers fast and efficient access to individual
380 * elements in any order and saves the user from worrying about
381 * memory and size allocation. Subscripting ( @c [] ) access is
382 * also provided as with C-style arrays.
384 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
385 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
387 #ifdef _GLIBCXX_CONCEPT_CHECKS
388 // Concept requirements.
389 typedef typename
_Alloc::value_type _Alloc_value_type
;
390 # if __cplusplus < 201103L
391 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
393 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
396 #if __cplusplus >= 201103L
397 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
398 "std::vector must have a non-const, non-volatile value_type");
399 # ifdef __STRICT_ANSI__
400 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
401 "std::vector must have the same value_type as its allocator");
405 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
406 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
407 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
410 typedef _Tp value_type
;
411 typedef typename
_Base::pointer pointer
;
412 typedef typename
_Alloc_traits::const_pointer const_pointer
;
413 typedef typename
_Alloc_traits::reference reference
;
414 typedef typename
_Alloc_traits::const_reference const_reference
;
415 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
416 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
418 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
419 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
420 typedef size_t size_type
;
421 typedef ptrdiff_t difference_type
;
422 typedef _Alloc allocator_type
;
425 using _Base::_M_allocate
;
426 using _Base::_M_deallocate
;
427 using _Base::_M_impl
;
428 using _Base::_M_get_Tp_allocator
;
431 // [23.2.4.1] construct/copy/destroy
432 // (assign() and get_allocator() are also listed in this section)
435 * @brief Creates a %vector with no elements.
437 #if __cplusplus >= 201103L
444 * @brief Creates a %vector with no elements.
445 * @param __a An allocator object.
448 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
451 #if __cplusplus >= 201103L
453 * @brief Creates a %vector with default constructed elements.
454 * @param __n The number of elements to initially create.
455 * @param __a An allocator.
457 * This constructor fills the %vector with @a __n default
458 * constructed elements.
461 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
463 { _M_default_initialize(__n
); }
466 * @brief Creates a %vector with copies of an exemplar element.
467 * @param __n The number of elements to initially create.
468 * @param __value An element to copy.
469 * @param __a An allocator.
471 * This constructor fills the %vector with @a __n copies of @a __value.
473 vector(size_type __n
, const value_type
& __value
,
474 const allocator_type
& __a
= allocator_type())
476 { _M_fill_initialize(__n
, __value
); }
479 * @brief Creates a %vector with copies of an exemplar element.
480 * @param __n The number of elements to initially create.
481 * @param __value An element to copy.
482 * @param __a An allocator.
484 * This constructor fills the %vector with @a __n copies of @a __value.
487 vector(size_type __n
, const value_type
& __value
= value_type(),
488 const allocator_type
& __a
= allocator_type())
490 { _M_fill_initialize(__n
, __value
); }
494 * @brief %Vector copy constructor.
495 * @param __x A %vector of identical element and allocator types.
497 * All the elements of @a __x are copied, but any unused capacity in
498 * @a __x will not be copied
499 * (i.e. capacity() == size() in the new %vector).
501 * The newly-created %vector uses a copy of the allocator object used
502 * by @a __x (unless the allocator traits dictate a different object).
504 vector(const vector
& __x
)
506 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
508 this->_M_impl
._M_finish
=
509 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
510 this->_M_impl
._M_start
,
511 _M_get_Tp_allocator());
514 #if __cplusplus >= 201103L
516 * @brief %Vector move constructor.
518 * The newly-created %vector contains the exact contents of the
520 * The contents of the moved instance are a valid, but unspecified
523 vector(vector
&&) noexcept
= default;
525 /// Copy constructor with alternative allocator
526 vector(const vector
& __x
, const allocator_type
& __a
)
527 : _Base(__x
.size(), __a
)
529 this->_M_impl
._M_finish
=
530 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
531 this->_M_impl
._M_start
,
532 _M_get_Tp_allocator());
536 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
537 : _Base(__m
, std::move(__rv
))
540 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
543 if (__rv
.get_allocator() == __m
)
544 this->_M_impl
._M_swap_data(__rv
._M_impl
);
545 else if (!__rv
.empty())
547 this->_M_create_storage(__rv
.size());
548 this->_M_impl
._M_finish
=
549 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
550 this->_M_impl
._M_start
,
551 _M_get_Tp_allocator());
557 /// Move constructor with alternative allocator
558 vector(vector
&& __rv
, const allocator_type
& __m
)
560 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
561 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
562 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
566 * @brief Builds a %vector from an initializer list.
567 * @param __l An initializer_list.
568 * @param __a An allocator.
570 * Create a %vector consisting of copies of the elements in the
571 * initializer_list @a __l.
573 * This will call the element type's copy constructor N times
574 * (where N is @a __l.size()) and do no memory reallocation.
576 vector(initializer_list
<value_type
> __l
,
577 const allocator_type
& __a
= allocator_type())
580 _M_range_initialize(__l
.begin(), __l
.end(),
581 random_access_iterator_tag());
586 * @brief Builds a %vector from a range.
587 * @param __first An input iterator.
588 * @param __last An input iterator.
589 * @param __a An allocator.
591 * Create a %vector consisting of copies of the elements from
594 * If the iterators are forward, bidirectional, or
595 * random-access, then this will call the elements' copy
596 * constructor N times (where N is distance(first,last)) and do
597 * no memory reallocation. But if only input iterators are
598 * used, then this will do at most 2N calls to the copy
599 * constructor, and logN memory reallocations.
601 #if __cplusplus >= 201103L
602 template<typename _InputIterator
,
603 typename
= std::_RequireInputIter
<_InputIterator
>>
604 vector(_InputIterator __first
, _InputIterator __last
,
605 const allocator_type
& __a
= allocator_type())
608 _M_range_initialize(__first
, __last
,
609 std::__iterator_category(__first
));
612 template<typename _InputIterator
>
613 vector(_InputIterator __first
, _InputIterator __last
,
614 const allocator_type
& __a
= allocator_type())
617 // Check whether it's an integral type. If so, it's not an iterator.
618 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
619 _M_initialize_dispatch(__first
, __last
, _Integral());
624 * The dtor only erases the elements, and note that if the
625 * elements themselves are pointers, the pointed-to memory is
626 * not touched in any way. Managing the pointer is the user's
629 ~vector() _GLIBCXX_NOEXCEPT
631 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
632 _M_get_Tp_allocator());
633 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
637 * @brief %Vector assignment operator.
638 * @param __x A %vector of identical element and allocator types.
640 * All the elements of @a __x are copied, but any unused capacity in
641 * @a __x will not be copied.
643 * Whether the allocator is copied depends on the allocator traits.
646 operator=(const vector
& __x
);
648 #if __cplusplus >= 201103L
650 * @brief %Vector move assignment operator.
651 * @param __x A %vector of identical element and allocator types.
653 * The contents of @a __x are moved into this %vector (without copying,
654 * if the allocators permit it).
655 * Afterwards @a __x is a valid, but unspecified %vector.
657 * Whether the allocator is moved depends on the allocator traits.
660 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
662 constexpr bool __move_storage
=
663 _Alloc_traits::_S_propagate_on_move_assign()
664 || _Alloc_traits::_S_always_equal();
665 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
670 * @brief %Vector list assignment operator.
671 * @param __l An initializer_list.
673 * This function fills a %vector with copies of the elements in the
674 * initializer list @a __l.
676 * Note that the assignment completely changes the %vector and
677 * that the resulting %vector's size is the same as the number
678 * of elements assigned.
681 operator=(initializer_list
<value_type
> __l
)
683 this->_M_assign_aux(__l
.begin(), __l
.end(),
684 random_access_iterator_tag());
690 * @brief Assigns a given value to a %vector.
691 * @param __n Number of elements to be assigned.
692 * @param __val Value to be assigned.
694 * This function fills a %vector with @a __n copies of the given
695 * value. Note that the assignment completely changes the
696 * %vector and that the resulting %vector's size is the same as
697 * the number of elements assigned.
700 assign(size_type __n
, const value_type
& __val
)
701 { _M_fill_assign(__n
, __val
); }
704 * @brief Assigns a range to a %vector.
705 * @param __first An input iterator.
706 * @param __last An input iterator.
708 * This function fills a %vector with copies of the elements in the
709 * range [__first,__last).
711 * Note that the assignment completely changes the %vector and
712 * that the resulting %vector's size is the same as the number
713 * of elements assigned.
715 #if __cplusplus >= 201103L
716 template<typename _InputIterator
,
717 typename
= std::_RequireInputIter
<_InputIterator
>>
719 assign(_InputIterator __first
, _InputIterator __last
)
720 { _M_assign_dispatch(__first
, __last
, __false_type()); }
722 template<typename _InputIterator
>
724 assign(_InputIterator __first
, _InputIterator __last
)
726 // Check whether it's an integral type. If so, it's not an iterator.
727 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
728 _M_assign_dispatch(__first
, __last
, _Integral());
732 #if __cplusplus >= 201103L
734 * @brief Assigns an initializer list to a %vector.
735 * @param __l An initializer_list.
737 * This function fills a %vector with copies of the elements in the
738 * initializer list @a __l.
740 * Note that the assignment completely changes the %vector and
741 * that the resulting %vector's size is the same as the number
742 * of elements assigned.
745 assign(initializer_list
<value_type
> __l
)
747 this->_M_assign_aux(__l
.begin(), __l
.end(),
748 random_access_iterator_tag());
752 /// Get a copy of the memory allocation object.
753 using _Base::get_allocator
;
757 * Returns a read/write iterator that points to the first
758 * element in the %vector. Iteration is done in ordinary
762 begin() _GLIBCXX_NOEXCEPT
763 { return iterator(this->_M_impl
._M_start
); }
766 * Returns a read-only (constant) iterator that points to the
767 * first element in the %vector. Iteration is done in ordinary
771 begin() const _GLIBCXX_NOEXCEPT
772 { return const_iterator(this->_M_impl
._M_start
); }
775 * Returns a read/write iterator that points one past the last
776 * element in the %vector. Iteration is done in ordinary
780 end() _GLIBCXX_NOEXCEPT
781 { return iterator(this->_M_impl
._M_finish
); }
784 * Returns a read-only (constant) iterator that points one past
785 * the last element in the %vector. Iteration is done in
786 * ordinary element order.
789 end() const _GLIBCXX_NOEXCEPT
790 { return const_iterator(this->_M_impl
._M_finish
); }
793 * Returns a read/write reverse iterator that points to the
794 * last element in the %vector. Iteration is done in reverse
798 rbegin() _GLIBCXX_NOEXCEPT
799 { return reverse_iterator(end()); }
802 * Returns a read-only (constant) reverse iterator that points
803 * to the last element in the %vector. Iteration is done in
804 * reverse element order.
806 const_reverse_iterator
807 rbegin() const _GLIBCXX_NOEXCEPT
808 { return const_reverse_iterator(end()); }
811 * Returns a read/write reverse iterator that points to one
812 * before the first element in the %vector. Iteration is done
813 * in reverse element order.
816 rend() _GLIBCXX_NOEXCEPT
817 { return reverse_iterator(begin()); }
820 * Returns a read-only (constant) reverse iterator that points
821 * to one before the first element in the %vector. Iteration
822 * is done in reverse element order.
824 const_reverse_iterator
825 rend() const _GLIBCXX_NOEXCEPT
826 { return const_reverse_iterator(begin()); }
828 #if __cplusplus >= 201103L
830 * Returns a read-only (constant) iterator that points to the
831 * first element in the %vector. Iteration is done in ordinary
835 cbegin() const noexcept
836 { return const_iterator(this->_M_impl
._M_start
); }
839 * Returns a read-only (constant) iterator that points one past
840 * the last element in the %vector. Iteration is done in
841 * ordinary element order.
844 cend() const noexcept
845 { return const_iterator(this->_M_impl
._M_finish
); }
848 * Returns a read-only (constant) reverse iterator that points
849 * to the last element in the %vector. Iteration is done in
850 * reverse element order.
852 const_reverse_iterator
853 crbegin() const noexcept
854 { return const_reverse_iterator(end()); }
857 * Returns a read-only (constant) reverse iterator that points
858 * to one before the first element in the %vector. Iteration
859 * is done in reverse element order.
861 const_reverse_iterator
862 crend() const noexcept
863 { return const_reverse_iterator(begin()); }
866 // [23.2.4.2] capacity
867 /** Returns the number of elements in the %vector. */
869 size() const _GLIBCXX_NOEXCEPT
870 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
872 /** Returns the size() of the largest possible %vector. */
874 max_size() const _GLIBCXX_NOEXCEPT
875 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
877 #if __cplusplus >= 201103L
879 * @brief Resizes the %vector to the specified number of elements.
880 * @param __new_size Number of elements the %vector should contain.
882 * This function will %resize the %vector to the specified
883 * number of elements. If the number is smaller than the
884 * %vector's current size the %vector is truncated, otherwise
885 * default constructed elements are appended.
888 resize(size_type __new_size
)
890 if (__new_size
> size())
891 _M_default_append(__new_size
- size());
892 else if (__new_size
< size())
893 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
897 * @brief Resizes the %vector to the specified number of elements.
898 * @param __new_size Number of elements the %vector should contain.
899 * @param __x Data with which new elements should be populated.
901 * This function will %resize the %vector to the specified
902 * number of elements. If the number is smaller than the
903 * %vector's current size the %vector is truncated, otherwise
904 * the %vector is extended and new elements are populated with
908 resize(size_type __new_size
, const value_type
& __x
)
910 if (__new_size
> size())
911 _M_fill_insert(end(), __new_size
- size(), __x
);
912 else if (__new_size
< size())
913 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
917 * @brief Resizes the %vector to the specified number of elements.
918 * @param __new_size Number of elements the %vector should contain.
919 * @param __x Data with which new elements should be populated.
921 * This function will %resize the %vector to the specified
922 * number of elements. If the number is smaller than the
923 * %vector's current size the %vector is truncated, otherwise
924 * the %vector is extended and new elements are populated with
928 resize(size_type __new_size
, value_type __x
= value_type())
930 if (__new_size
> size())
931 _M_fill_insert(end(), __new_size
- size(), __x
);
932 else if (__new_size
< size())
933 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
937 #if __cplusplus >= 201103L
938 /** A non-binding request to reduce capacity() to size(). */
941 { _M_shrink_to_fit(); }
945 * Returns the total number of elements that the %vector can
946 * hold before needing to allocate more memory.
949 capacity() const _GLIBCXX_NOEXCEPT
950 { return size_type(this->_M_impl
._M_end_of_storage
951 - this->_M_impl
._M_start
); }
954 * Returns true if the %vector is empty. (Thus begin() would
958 empty() const _GLIBCXX_NOEXCEPT
959 { return begin() == end(); }
962 * @brief Attempt to preallocate enough memory for specified number of
964 * @param __n Number of elements required.
965 * @throw std::length_error If @a n exceeds @c max_size().
967 * This function attempts to reserve enough memory for the
968 * %vector to hold the specified number of elements. If the
969 * number requested is more than max_size(), length_error is
972 * The advantage of this function is that if optimal code is a
973 * necessity and the user can determine the number of elements
974 * that will be required, the user can reserve the memory in
975 * %advance, and thus prevent a possible reallocation of memory
976 * and copying of %vector data.
979 reserve(size_type __n
);
983 * @brief Subscript access to the data contained in the %vector.
984 * @param __n The index of the element for which data should be
986 * @return Read/write reference to data.
988 * This operator allows for easy, array-style, data access.
989 * Note that data access with this operator is unchecked and
990 * out_of_range lookups are not defined. (For checked lookups
994 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
996 __glibcxx_requires_subscript(__n
);
997 return *(this->_M_impl
._M_start
+ __n
);
1001 * @brief Subscript access to the data contained in the %vector.
1002 * @param __n The index of the element for which data should be
1004 * @return Read-only (constant) reference to data.
1006 * This operator allows for easy, array-style, data access.
1007 * Note that data access with this operator is unchecked and
1008 * out_of_range lookups are not defined. (For checked lookups
1012 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1014 __glibcxx_requires_subscript(__n
);
1015 return *(this->_M_impl
._M_start
+ __n
);
1019 /// Safety check used only from at().
1021 _M_range_check(size_type __n
) const
1023 if (__n
>= this->size())
1024 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1025 "(which is %zu) >= this->size() "
1032 * @brief Provides access to the data contained in the %vector.
1033 * @param __n The index of the element for which data should be
1035 * @return Read/write reference to data.
1036 * @throw std::out_of_range If @a __n is an invalid index.
1038 * This function provides for safer data access. The parameter
1039 * is first checked that it is in the range of the vector. The
1040 * function throws out_of_range if the check fails.
1045 _M_range_check(__n
);
1046 return (*this)[__n
];
1050 * @brief Provides access to the data contained in the %vector.
1051 * @param __n The index of the element for which data should be
1053 * @return Read-only (constant) reference to data.
1054 * @throw std::out_of_range If @a __n is an invalid index.
1056 * This function provides for safer data access. The parameter
1057 * is first checked that it is in the range of the vector. The
1058 * function throws out_of_range if the check fails.
1061 at(size_type __n
) const
1063 _M_range_check(__n
);
1064 return (*this)[__n
];
1068 * Returns a read/write reference to the data at the first
1069 * element of the %vector.
1072 front() _GLIBCXX_NOEXCEPT
1074 __glibcxx_requires_nonempty();
1079 * Returns a read-only (constant) reference to the data at the first
1080 * element of the %vector.
1083 front() const _GLIBCXX_NOEXCEPT
1085 __glibcxx_requires_nonempty();
1090 * Returns a read/write reference to the data at the last
1091 * element of the %vector.
1094 back() _GLIBCXX_NOEXCEPT
1096 __glibcxx_requires_nonempty();
1097 return *(end() - 1);
1101 * Returns a read-only (constant) reference to the data at the
1102 * last element of the %vector.
1105 back() const _GLIBCXX_NOEXCEPT
1107 __glibcxx_requires_nonempty();
1108 return *(end() - 1);
1111 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1112 // DR 464. Suggestion for new member functions in standard containers.
1115 * Returns a pointer such that [data(), data() + size()) is a valid
1116 * range. For a non-empty %vector, data() == &front().
1119 data() _GLIBCXX_NOEXCEPT
1120 { return _M_data_ptr(this->_M_impl
._M_start
); }
1123 data() const _GLIBCXX_NOEXCEPT
1124 { return _M_data_ptr(this->_M_impl
._M_start
); }
1126 // [23.2.4.3] modifiers
1128 * @brief Add data to the end of the %vector.
1129 * @param __x Data to be added.
1131 * This is a typical stack operation. The function creates an
1132 * element at the end of the %vector and assigns the given data
1133 * to it. Due to the nature of a %vector this operation can be
1134 * done in constant time if the %vector has preallocated space
1138 push_back(const value_type
& __x
)
1140 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1142 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1143 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1145 ++this->_M_impl
._M_finish
;
1146 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1149 _M_realloc_insert(end(), __x
);
1152 #if __cplusplus >= 201103L
1154 push_back(value_type
&& __x
)
1155 { emplace_back(std::move(__x
)); }
1157 template<typename
... _Args
>
1158 #if __cplusplus > 201402L
1163 emplace_back(_Args
&&... __args
);
1167 * @brief Removes last element.
1169 * This is a typical stack operation. It shrinks the %vector by one.
1171 * Note that no data is returned, and if the last element's
1172 * data is needed, it should be retrieved before pop_back() is
1176 pop_back() _GLIBCXX_NOEXCEPT
1178 __glibcxx_requires_nonempty();
1179 --this->_M_impl
._M_finish
;
1180 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1181 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1184 #if __cplusplus >= 201103L
1186 * @brief Inserts an object in %vector before specified iterator.
1187 * @param __position A const_iterator into the %vector.
1188 * @param __args Arguments.
1189 * @return An iterator that points to the inserted data.
1191 * This function will insert an object of type T constructed
1192 * with T(std::forward<Args>(args)...) before the specified location.
1193 * Note that this kind of operation could be expensive for a %vector
1194 * and if it is frequently used the user should consider using
1197 template<typename
... _Args
>
1199 emplace(const_iterator __position
, _Args
&&... __args
)
1200 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1203 * @brief Inserts given value into %vector before specified iterator.
1204 * @param __position A const_iterator into the %vector.
1205 * @param __x Data to be inserted.
1206 * @return An iterator that points to the inserted data.
1208 * This function will insert a copy of the given value before
1209 * the specified location. Note that this kind of operation
1210 * could be expensive for a %vector and if it is frequently
1211 * used the user should consider using std::list.
1214 insert(const_iterator __position
, const value_type
& __x
);
1217 * @brief Inserts given value into %vector before specified iterator.
1218 * @param __position An iterator into the %vector.
1219 * @param __x Data to be inserted.
1220 * @return An iterator that points to the inserted data.
1222 * This function will insert a copy of the given value before
1223 * the specified location. Note that this kind of operation
1224 * could be expensive for a %vector and if it is frequently
1225 * used the user should consider using std::list.
1228 insert(iterator __position
, const value_type
& __x
);
1231 #if __cplusplus >= 201103L
1233 * @brief Inserts given rvalue into %vector before specified iterator.
1234 * @param __position A const_iterator into the %vector.
1235 * @param __x Data to be inserted.
1236 * @return An iterator that points to the inserted data.
1238 * This function will insert a copy of the given rvalue before
1239 * the specified location. Note that this kind of operation
1240 * could be expensive for a %vector and if it is frequently
1241 * used the user should consider using std::list.
1244 insert(const_iterator __position
, value_type
&& __x
)
1245 { return _M_insert_rval(__position
, std::move(__x
)); }
1248 * @brief Inserts an initializer_list into the %vector.
1249 * @param __position An iterator into the %vector.
1250 * @param __l An initializer_list.
1252 * This function will insert copies of the data in the
1253 * initializer_list @a l into the %vector before the location
1254 * specified by @a position.
1256 * Note that this kind of operation could be expensive for a
1257 * %vector and if it is frequently used the user should
1258 * consider using std::list.
1261 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1263 auto __offset
= __position
- cbegin();
1264 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1265 std::random_access_iterator_tag());
1266 return begin() + __offset
;
1270 #if __cplusplus >= 201103L
1272 * @brief Inserts a number of copies of given data into the %vector.
1273 * @param __position A const_iterator into the %vector.
1274 * @param __n Number of elements to be inserted.
1275 * @param __x Data to be inserted.
1276 * @return An iterator that points to the inserted data.
1278 * This function will insert a specified number of copies of
1279 * the given data before the location specified by @a position.
1281 * Note that this kind of operation could be expensive for a
1282 * %vector and if it is frequently used the user should
1283 * consider using std::list.
1286 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1288 difference_type __offset
= __position
- cbegin();
1289 _M_fill_insert(begin() + __offset
, __n
, __x
);
1290 return begin() + __offset
;
1294 * @brief Inserts a number of copies of given data into the %vector.
1295 * @param __position An iterator into the %vector.
1296 * @param __n Number of elements to be inserted.
1297 * @param __x Data to be inserted.
1299 * This function will insert a specified number of copies of
1300 * the given data before the location specified by @a position.
1302 * Note that this kind of operation could be expensive for a
1303 * %vector and if it is frequently used the user should
1304 * consider using std::list.
1307 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1308 { _M_fill_insert(__position
, __n
, __x
); }
1311 #if __cplusplus >= 201103L
1313 * @brief Inserts a range into the %vector.
1314 * @param __position A const_iterator into the %vector.
1315 * @param __first An input iterator.
1316 * @param __last An input iterator.
1317 * @return An iterator that points to the inserted data.
1319 * This function will insert copies of the data in the range
1320 * [__first,__last) into the %vector before the location specified
1323 * Note that this kind of operation could be expensive for a
1324 * %vector and if it is frequently used the user should
1325 * consider using std::list.
1327 template<typename _InputIterator
,
1328 typename
= std::_RequireInputIter
<_InputIterator
>>
1330 insert(const_iterator __position
, _InputIterator __first
,
1331 _InputIterator __last
)
1333 difference_type __offset
= __position
- cbegin();
1334 _M_insert_dispatch(begin() + __offset
,
1335 __first
, __last
, __false_type());
1336 return begin() + __offset
;
1340 * @brief Inserts a range into the %vector.
1341 * @param __position An iterator into the %vector.
1342 * @param __first An input iterator.
1343 * @param __last An input iterator.
1345 * This function will insert copies of the data in the range
1346 * [__first,__last) into the %vector before the location specified
1349 * Note that this kind of operation could be expensive for a
1350 * %vector and if it is frequently used the user should
1351 * consider using std::list.
1353 template<typename _InputIterator
>
1355 insert(iterator __position
, _InputIterator __first
,
1356 _InputIterator __last
)
1358 // Check whether it's an integral type. If so, it's not an iterator.
1359 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1360 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1365 * @brief Remove element at given position.
1366 * @param __position Iterator pointing to element to be erased.
1367 * @return An iterator pointing to the next element (or end()).
1369 * This function will erase the element at the given position and thus
1370 * shorten the %vector by one.
1372 * Note This operation could be expensive and if it is
1373 * frequently used the user should consider using std::list.
1374 * The user is also cautioned that this function only erases
1375 * the element, and that if the element is itself a pointer,
1376 * the pointed-to memory is not touched in any way. Managing
1377 * the pointer is the user's responsibility.
1380 #if __cplusplus >= 201103L
1381 erase(const_iterator __position
)
1382 { return _M_erase(begin() + (__position
- cbegin())); }
1384 erase(iterator __position
)
1385 { return _M_erase(__position
); }
1389 * @brief Remove a range of elements.
1390 * @param __first Iterator pointing to the first element to be erased.
1391 * @param __last Iterator pointing to one past the last element to be
1393 * @return An iterator pointing to the element pointed to by @a __last
1394 * prior to erasing (or end()).
1396 * This function will erase the elements in the range
1397 * [__first,__last) and shorten the %vector accordingly.
1399 * Note This operation could be expensive and if it is
1400 * frequently used the user should consider using std::list.
1401 * The user is also cautioned that this function only erases
1402 * the elements, and that if the elements themselves are
1403 * pointers, the pointed-to memory is not touched in any way.
1404 * Managing the pointer is the user's responsibility.
1407 #if __cplusplus >= 201103L
1408 erase(const_iterator __first
, const_iterator __last
)
1410 const auto __beg
= begin();
1411 const auto __cbeg
= cbegin();
1412 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1415 erase(iterator __first
, iterator __last
)
1416 { return _M_erase(__first
, __last
); }
1420 * @brief Swaps data with another %vector.
1421 * @param __x A %vector of the same element and allocator types.
1423 * This exchanges the elements between two vectors in constant time.
1424 * (Three pointers, so it should be quite fast.)
1425 * Note that the global std::swap() function is specialized such that
1426 * std::swap(v1,v2) will feed to this function.
1428 * Whether the allocators are swapped depends on the allocator traits.
1431 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1433 #if __cplusplus >= 201103L
1434 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1435 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1437 this->_M_impl
._M_swap_data(__x
._M_impl
);
1438 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1439 __x
._M_get_Tp_allocator());
1443 * Erases all the elements. Note that this function only erases the
1444 * elements, and that if the elements themselves are pointers, the
1445 * pointed-to memory is not touched in any way. Managing the pointer is
1446 * the user's responsibility.
1449 clear() _GLIBCXX_NOEXCEPT
1450 { _M_erase_at_end(this->_M_impl
._M_start
); }
1454 * Memory expansion handler. Uses the member allocation function to
1455 * obtain @a n bytes of memory, and then copies [first,last) into it.
1457 template<typename _ForwardIterator
>
1459 _M_allocate_and_copy(size_type __n
,
1460 _ForwardIterator __first
, _ForwardIterator __last
)
1462 pointer __result
= this->_M_allocate(__n
);
1465 std::__uninitialized_copy_a(__first
, __last
, __result
,
1466 _M_get_Tp_allocator());
1471 _M_deallocate(__result
, __n
);
1472 __throw_exception_again
;
1477 // Internal constructor functions follow.
1479 // Called by the range constructor to implement [23.1.1]/9
1481 #if __cplusplus < 201103L
1482 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1483 // 438. Ambiguity in the "do the right thing" clause
1484 template<typename _Integer
>
1486 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1488 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1489 this->_M_impl
._M_end_of_storage
=
1490 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1491 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1494 // Called by the range constructor to implement [23.1.1]/9
1495 template<typename _InputIterator
>
1497 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1500 _M_range_initialize(__first
, __last
,
1501 std::__iterator_category(__first
));
1505 // Called by the second initialize_dispatch above
1506 template<typename _InputIterator
>
1508 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1509 std::input_iterator_tag
)
1512 for (; __first
!= __last
; ++__first
)
1513 #if __cplusplus >= 201103L
1514 emplace_back(*__first
);
1516 push_back(*__first
);
1520 __throw_exception_again
;
1524 // Called by the second initialize_dispatch above
1525 template<typename _ForwardIterator
>
1527 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1528 std::forward_iterator_tag
)
1530 const size_type __n
= std::distance(__first
, __last
);
1531 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1532 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1533 this->_M_impl
._M_finish
=
1534 std::__uninitialized_copy_a(__first
, __last
,
1535 this->_M_impl
._M_start
,
1536 _M_get_Tp_allocator());
1539 // Called by the first initialize_dispatch above and by the
1540 // vector(n,value,a) constructor.
1542 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1544 this->_M_impl
._M_finish
=
1545 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1546 _M_get_Tp_allocator());
1549 #if __cplusplus >= 201103L
1550 // Called by the vector(n) constructor.
1552 _M_default_initialize(size_type __n
)
1554 this->_M_impl
._M_finish
=
1555 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1556 _M_get_Tp_allocator());
1560 // Internal assign functions follow. The *_aux functions do the actual
1561 // assignment work for the range versions.
1563 // Called by the range assign to implement [23.1.1]/9
1565 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1566 // 438. Ambiguity in the "do the right thing" clause
1567 template<typename _Integer
>
1569 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1570 { _M_fill_assign(__n
, __val
); }
1572 // Called by the range assign to implement [23.1.1]/9
1573 template<typename _InputIterator
>
1575 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1577 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1579 // Called by the second assign_dispatch above
1580 template<typename _InputIterator
>
1582 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1583 std::input_iterator_tag
);
1585 // Called by the second assign_dispatch above
1586 template<typename _ForwardIterator
>
1588 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1589 std::forward_iterator_tag
);
1591 // Called by assign(n,t), and the range assign when it turns out
1592 // to be the same thing.
1594 _M_fill_assign(size_type __n
, const value_type
& __val
);
1596 // Internal insert functions follow.
1598 // Called by the range insert to implement [23.1.1]/9
1600 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1601 // 438. Ambiguity in the "do the right thing" clause
1602 template<typename _Integer
>
1604 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1606 { _M_fill_insert(__pos
, __n
, __val
); }
1608 // Called by the range insert to implement [23.1.1]/9
1609 template<typename _InputIterator
>
1611 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1612 _InputIterator __last
, __false_type
)
1614 _M_range_insert(__pos
, __first
, __last
,
1615 std::__iterator_category(__first
));
1618 // Called by the second insert_dispatch above
1619 template<typename _InputIterator
>
1621 _M_range_insert(iterator __pos
, _InputIterator __first
,
1622 _InputIterator __last
, std::input_iterator_tag
);
1624 // Called by the second insert_dispatch above
1625 template<typename _ForwardIterator
>
1627 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1628 _ForwardIterator __last
, std::forward_iterator_tag
);
1630 // Called by insert(p,n,x), and the range insert when it turns out to be
1633 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1635 #if __cplusplus >= 201103L
1636 // Called by resize(n).
1638 _M_default_append(size_type __n
);
1644 #if __cplusplus < 201103L
1645 // Called by insert(p,x)
1647 _M_insert_aux(iterator __position
, const value_type
& __x
);
1650 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1652 // A value_type object constructed with _Alloc_traits::construct()
1653 // and destroyed with _Alloc_traits::destroy().
1654 struct _Temporary_value
1656 template<typename
... _Args
>
1658 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1660 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1661 std::forward
<_Args
>(__args
)...);
1665 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1668 _M_val() { return *reinterpret_cast<_Tp
*>(&__buf
); }
1672 _M_ptr() { return pointer_traits
<pointer
>::pointer_to(_M_val()); }
1675 typename aligned_storage
<sizeof(_Tp
), alignof(_Tp
)>::type __buf
;
1678 // Called by insert(p,x) and other functions when insertion needs to
1679 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1680 template<typename _Arg
>
1682 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1684 template<typename
... _Args
>
1686 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1688 // Either move-construct at the end, or forward to _M_insert_aux.
1690 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1692 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1693 template<typename
... _Args
>
1695 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1697 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1699 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1700 { return _M_insert_rval(__position
, std::move(__v
)); }
1703 // Called by _M_fill_insert, _M_insert_aux etc.
1705 _M_check_len(size_type __n
, const char* __s
) const
1707 if (max_size() - size() < __n
)
1708 __throw_length_error(__N(__s
));
1710 const size_type __len
= size() + std::max(size(), __n
);
1711 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1714 // Internal erase functions follow.
1716 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1719 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1721 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1723 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1724 _M_get_Tp_allocator());
1725 this->_M_impl
._M_finish
= __pos
;
1726 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1731 _M_erase(iterator __position
);
1734 _M_erase(iterator __first
, iterator __last
);
1736 #if __cplusplus >= 201103L
1738 // Constant-time move assignment when source object's memory can be
1739 // moved, either because the source's allocator will move too
1740 // or because the allocators are equal.
1742 _M_move_assign(vector
&& __x
, true_type
) noexcept
1744 vector
__tmp(get_allocator());
1745 this->_M_impl
._M_swap_data(__x
._M_impl
);
1746 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
1747 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1750 // Do move assignment when it might not be possible to move source
1751 // object's memory, resulting in a linear-time operation.
1753 _M_move_assign(vector
&& __x
, false_type
)
1755 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1756 _M_move_assign(std::move(__x
), true_type());
1759 // The rvalue's allocator cannot be moved and is not equal,
1760 // so we need to individually move each element.
1761 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1762 std::__make_move_if_noexcept_iterator(__x
.end()));
1768 template<typename _Up
>
1770 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
1773 #if __cplusplus >= 201103L
1774 template<typename _Ptr
>
1775 typename
std::pointer_traits
<_Ptr
>::element_type
*
1776 _M_data_ptr(_Ptr __ptr
) const
1777 { return empty() ? nullptr : std::__to_address(__ptr
); }
1779 template<typename _Up
>
1781 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
1784 template<typename _Ptr
>
1786 _M_data_ptr(_Ptr __ptr
)
1787 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
1789 template<typename _Ptr
>
1791 _M_data_ptr(_Ptr __ptr
) const
1792 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
1796 #if __cpp_deduction_guides >= 201606
1797 template<typename _InputIterator
, typename _ValT
1798 = typename iterator_traits
<_InputIterator
>::value_type
,
1799 typename _Allocator
= allocator
<_ValT
>,
1800 typename
= _RequireInputIter
<_InputIterator
>,
1801 typename
= _RequireAllocator
<_Allocator
>>
1802 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1803 -> vector
<_ValT
, _Allocator
>;
1807 * @brief Vector equality comparison.
1808 * @param __x A %vector.
1809 * @param __y A %vector of the same type as @a __x.
1810 * @return True iff the size and elements of the vectors are equal.
1812 * This is an equivalence relation. It is linear in the size of the
1813 * vectors. Vectors are considered equivalent if their sizes are equal,
1814 * and if corresponding elements compare equal.
1816 template<typename _Tp
, typename _Alloc
>
1818 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1819 { return (__x
.size() == __y
.size()
1820 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1823 * @brief Vector ordering relation.
1824 * @param __x A %vector.
1825 * @param __y A %vector of the same type as @a __x.
1826 * @return True iff @a __x is lexicographically less than @a __y.
1828 * This is a total ordering relation. It is linear in the size of the
1829 * vectors. The elements must be comparable with @c <.
1831 * See std::lexicographical_compare() for how the determination is made.
1833 template<typename _Tp
, typename _Alloc
>
1835 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1836 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1837 __y
.begin(), __y
.end()); }
1839 /// Based on operator==
1840 template<typename _Tp
, typename _Alloc
>
1842 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1843 { return !(__x
== __y
); }
1845 /// Based on operator<
1846 template<typename _Tp
, typename _Alloc
>
1848 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1849 { return __y
< __x
; }
1851 /// Based on operator<
1852 template<typename _Tp
, typename _Alloc
>
1854 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1855 { return !(__y
< __x
); }
1857 /// Based on operator<
1858 template<typename _Tp
, typename _Alloc
>
1860 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1861 { return !(__x
< __y
); }
1863 /// See std::vector::swap().
1864 template<typename _Tp
, typename _Alloc
>
1866 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1867 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1870 _GLIBCXX_END_NAMESPACE_CONTAINER
1871 _GLIBCXX_END_NAMESPACE_VERSION
1874 #endif /* _STL_VECTOR_H */