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(
129 is_nothrow_default_constructible
<_Tp_alloc_type
>::value
)
133 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
134 : _Tp_alloc_type(__a
)
137 #if __cplusplus >= 201103L
138 // Not defaulted, to enforce noexcept(true) even when
139 // !is_nothrow_move_constructible<_Tp_alloc_type>.
140 _Vector_impl(_Vector_impl
&& __x
) noexcept
141 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
144 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
145 : _Tp_alloc_type(std::move(__a
))
148 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
149 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
153 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
154 template<typename
= _Tp_alloc_type
>
157 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
158 ::size_type size_type
;
160 static void _S_shrink(_Vector_impl
&, size_type
) { }
161 static void _S_on_dealloc(_Vector_impl
&) { }
163 typedef _Vector_impl
& _Reinit
;
167 _Grow(_Vector_impl
&, size_type
) { }
168 void _M_grew(size_type
) { }
172 // Enable ASan annotations for memory obtained from std::allocator.
173 template<typename _Up
>
174 struct _Asan
<allocator
<_Up
> >
176 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
177 ::size_type size_type
;
179 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
180 // mark end of valid region as __curr instead of __prev.
182 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
184 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
185 __impl
._M_end_of_storage
, __prev
, __curr
);
189 _S_grow(_Vector_impl
& __impl
, size_type __n
)
190 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
193 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
194 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
197 _S_on_dealloc(_Vector_impl
& __impl
)
200 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
203 // Used on reallocation to tell ASan unused capacity is invalid.
206 explicit _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
208 // Mark unused capacity as valid again before deallocating it.
209 _S_on_dealloc(_M_impl
);
214 // Mark unused capacity as invalid after reallocation.
215 if (_M_impl
._M_start
)
216 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
220 _Vector_impl
& _M_impl
;
222 #if __cplusplus >= 201103L
223 _Reinit(const _Reinit
&) = delete;
224 _Reinit
& operator=(const _Reinit
&) = delete;
228 // Tell ASan when unused capacity is initialized to be valid.
231 _Grow(_Vector_impl
& __impl
, size_type __n
)
232 : _M_impl(__impl
), _M_n(__n
)
233 { _S_grow(_M_impl
, __n
); }
235 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
237 void _M_grew(size_type __n
) { _M_n
-= __n
; }
239 #if __cplusplus >= 201103L
240 _Grow(const _Grow
&) = delete;
241 _Grow
& operator=(const _Grow
&) = delete;
244 _Vector_impl
& _M_impl
;
249 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
250 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
251 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
252 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
253 typename _Base::_Vector_impl::template _Asan<>::_Grow \
254 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
255 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
256 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
257 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
258 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
259 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
260 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
261 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
262 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
263 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
264 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
265 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
266 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
270 typedef _Alloc allocator_type
;
273 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
274 { return this->_M_impl
; }
276 const _Tp_alloc_type
&
277 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
278 { return this->_M_impl
; }
281 get_allocator() const _GLIBCXX_NOEXCEPT
282 { return allocator_type(_M_get_Tp_allocator()); }
284 #if __cplusplus >= 201103L
285 _Vector_base() = default;
290 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
293 // Kept for ABI compatibility.
294 #if !_GLIBCXX_INLINE_VERSION
295 _Vector_base(size_t __n
)
297 { _M_create_storage(__n
); }
300 _Vector_base(size_t __n
, const allocator_type
& __a
)
302 { _M_create_storage(__n
); }
304 #if __cplusplus >= 201103L
305 _Vector_base(_Vector_base
&&) = default;
307 // Kept for ABI compatibility.
308 # if !_GLIBCXX_INLINE_VERSION
309 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
310 : _M_impl(std::move(__a
)) { }
312 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
315 if (__x
.get_allocator() == __a
)
316 this->_M_impl
._M_swap_data(__x
._M_impl
);
319 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
320 _M_create_storage(__n
);
325 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
326 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
330 ~_Vector_base() _GLIBCXX_NOEXCEPT
332 _M_deallocate(_M_impl
._M_start
,
333 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
337 _Vector_impl _M_impl
;
340 _M_allocate(size_t __n
)
342 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
343 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
347 _M_deallocate(pointer __p
, size_t __n
)
349 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
351 _Tr::deallocate(_M_impl
, __p
, __n
);
356 _M_create_storage(size_t __n
)
358 this->_M_impl
._M_start
= this->_M_allocate(__n
);
359 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
360 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
365 * @brief A standard container which offers fixed time access to
366 * individual elements in any order.
370 * @tparam _Tp Type of element.
371 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
373 * Meets the requirements of a <a href="tables.html#65">container</a>, a
374 * <a href="tables.html#66">reversible container</a>, and a
375 * <a href="tables.html#67">sequence</a>, including the
376 * <a href="tables.html#68">optional sequence requirements</a> with the
377 * %exception of @c push_front and @c pop_front.
379 * In some terminology a %vector can be described as a dynamic
380 * C-style array, it offers fast and efficient access to individual
381 * elements in any order and saves the user from worrying about
382 * memory and size allocation. Subscripting ( @c [] ) access is
383 * also provided as with C-style arrays.
385 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
386 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
388 #ifdef _GLIBCXX_CONCEPT_CHECKS
389 // Concept requirements.
390 typedef typename
_Alloc::value_type _Alloc_value_type
;
391 # if __cplusplus < 201103L
392 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
394 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
397 #if __cplusplus >= 201103L
398 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
399 "std::vector must have a non-const, non-volatile value_type");
400 # ifdef __STRICT_ANSI__
401 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
402 "std::vector must have the same value_type as its allocator");
406 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
407 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
408 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
411 typedef _Tp value_type
;
412 typedef typename
_Base::pointer pointer
;
413 typedef typename
_Alloc_traits::const_pointer const_pointer
;
414 typedef typename
_Alloc_traits::reference reference
;
415 typedef typename
_Alloc_traits::const_reference const_reference
;
416 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
417 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
419 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
420 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
421 typedef size_t size_type
;
422 typedef ptrdiff_t difference_type
;
423 typedef _Alloc allocator_type
;
426 #if __cplusplus >= 201103L
427 static constexpr bool __use_relocate
=
428 noexcept(std::__relocate_object_a(
429 std::addressof(*std::declval
<pointer
>()),
430 std::addressof(*std::declval
<pointer
>()),
431 std::declval
<_Tp_alloc_type
&>()));
435 using _Base::_M_allocate
;
436 using _Base::_M_deallocate
;
437 using _Base::_M_impl
;
438 using _Base::_M_get_Tp_allocator
;
441 // [23.2.4.1] construct/copy/destroy
442 // (assign() and get_allocator() are also listed in this section)
445 * @brief Creates a %vector with no elements.
447 #if __cplusplus >= 201103L
454 * @brief Creates a %vector with no elements.
455 * @param __a An allocator object.
458 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
461 #if __cplusplus >= 201103L
463 * @brief Creates a %vector with default constructed elements.
464 * @param __n The number of elements to initially create.
465 * @param __a An allocator.
467 * This constructor fills the %vector with @a __n default
468 * constructed elements.
471 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
472 : _Base(_S_check_init_len(__n
, __a
), __a
)
473 { _M_default_initialize(__n
); }
476 * @brief Creates a %vector with copies of an exemplar element.
477 * @param __n The number of elements to initially create.
478 * @param __value An element to copy.
479 * @param __a An allocator.
481 * This constructor fills the %vector with @a __n copies of @a __value.
483 vector(size_type __n
, const value_type
& __value
,
484 const allocator_type
& __a
= allocator_type())
485 : _Base(_S_check_init_len(__n
, __a
), __a
)
486 { _M_fill_initialize(__n
, __value
); }
489 * @brief Creates a %vector with copies of an exemplar element.
490 * @param __n The number of elements to initially create.
491 * @param __value An element to copy.
492 * @param __a An allocator.
494 * This constructor fills the %vector with @a __n copies of @a __value.
497 vector(size_type __n
, const value_type
& __value
= value_type(),
498 const allocator_type
& __a
= allocator_type())
499 : _Base(_S_check_init_len(__n
, __a
), __a
)
500 { _M_fill_initialize(__n
, __value
); }
504 * @brief %Vector copy constructor.
505 * @param __x A %vector of identical element and allocator types.
507 * All the elements of @a __x are copied, but any unused capacity in
508 * @a __x will not be copied
509 * (i.e. capacity() == size() in the new %vector).
511 * The newly-created %vector uses a copy of the allocator object used
512 * by @a __x (unless the allocator traits dictate a different object).
514 vector(const vector
& __x
)
516 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
518 this->_M_impl
._M_finish
=
519 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
520 this->_M_impl
._M_start
,
521 _M_get_Tp_allocator());
524 #if __cplusplus >= 201103L
526 * @brief %Vector move constructor.
528 * The newly-created %vector contains the exact contents of the
530 * The contents of the moved instance are a valid, but unspecified
533 vector(vector
&&) noexcept
= default;
535 /// Copy constructor with alternative allocator
536 vector(const vector
& __x
, const allocator_type
& __a
)
537 : _Base(__x
.size(), __a
)
539 this->_M_impl
._M_finish
=
540 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
541 this->_M_impl
._M_start
,
542 _M_get_Tp_allocator());
546 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
547 : _Base(__m
, std::move(__rv
))
550 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
553 if (__rv
.get_allocator() == __m
)
554 this->_M_impl
._M_swap_data(__rv
._M_impl
);
555 else if (!__rv
.empty())
557 this->_M_create_storage(__rv
.size());
558 this->_M_impl
._M_finish
=
559 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
560 this->_M_impl
._M_start
,
561 _M_get_Tp_allocator());
567 /// Move constructor with alternative allocator
568 vector(vector
&& __rv
, const allocator_type
& __m
)
570 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
571 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
572 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
576 * @brief Builds a %vector from an initializer list.
577 * @param __l An initializer_list.
578 * @param __a An allocator.
580 * Create a %vector consisting of copies of the elements in the
581 * initializer_list @a __l.
583 * This will call the element type's copy constructor N times
584 * (where N is @a __l.size()) and do no memory reallocation.
586 vector(initializer_list
<value_type
> __l
,
587 const allocator_type
& __a
= allocator_type())
590 _M_range_initialize(__l
.begin(), __l
.end(),
591 random_access_iterator_tag());
596 * @brief Builds a %vector from a range.
597 * @param __first An input iterator.
598 * @param __last An input iterator.
599 * @param __a An allocator.
601 * Create a %vector consisting of copies of the elements from
604 * If the iterators are forward, bidirectional, or
605 * random-access, then this will call the elements' copy
606 * constructor N times (where N is distance(first,last)) and do
607 * no memory reallocation. But if only input iterators are
608 * used, then this will do at most 2N calls to the copy
609 * constructor, and logN memory reallocations.
611 #if __cplusplus >= 201103L
612 template<typename _InputIterator
,
613 typename
= std::_RequireInputIter
<_InputIterator
>>
614 vector(_InputIterator __first
, _InputIterator __last
,
615 const allocator_type
& __a
= allocator_type())
618 _M_range_initialize(__first
, __last
,
619 std::__iterator_category(__first
));
622 template<typename _InputIterator
>
623 vector(_InputIterator __first
, _InputIterator __last
,
624 const allocator_type
& __a
= allocator_type())
627 // Check whether it's an integral type. If so, it's not an iterator.
628 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
629 _M_initialize_dispatch(__first
, __last
, _Integral());
634 * The dtor only erases the elements, and note that if the
635 * elements themselves are pointers, the pointed-to memory is
636 * not touched in any way. Managing the pointer is the user's
639 ~vector() _GLIBCXX_NOEXCEPT
641 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
642 _M_get_Tp_allocator());
643 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
647 * @brief %Vector assignment operator.
648 * @param __x A %vector of identical element and allocator types.
650 * All the elements of @a __x are copied, but any unused capacity in
651 * @a __x will not be copied.
653 * Whether the allocator is copied depends on the allocator traits.
656 operator=(const vector
& __x
);
658 #if __cplusplus >= 201103L
660 * @brief %Vector move assignment operator.
661 * @param __x A %vector of identical element and allocator types.
663 * The contents of @a __x are moved into this %vector (without copying,
664 * if the allocators permit it).
665 * Afterwards @a __x is a valid, but unspecified %vector.
667 * Whether the allocator is moved depends on the allocator traits.
670 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
672 constexpr bool __move_storage
=
673 _Alloc_traits::_S_propagate_on_move_assign()
674 || _Alloc_traits::_S_always_equal();
675 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
680 * @brief %Vector list assignment operator.
681 * @param __l An initializer_list.
683 * This function fills a %vector with copies of the elements in the
684 * initializer list @a __l.
686 * Note that the assignment completely changes the %vector and
687 * that the resulting %vector's size is the same as the number
688 * of elements assigned.
691 operator=(initializer_list
<value_type
> __l
)
693 this->_M_assign_aux(__l
.begin(), __l
.end(),
694 random_access_iterator_tag());
700 * @brief Assigns a given value to a %vector.
701 * @param __n Number of elements to be assigned.
702 * @param __val Value to be assigned.
704 * This function fills a %vector with @a __n copies of the given
705 * value. Note that the assignment completely changes the
706 * %vector and that the resulting %vector's size is the same as
707 * the number of elements assigned.
710 assign(size_type __n
, const value_type
& __val
)
711 { _M_fill_assign(__n
, __val
); }
714 * @brief Assigns a range to a %vector.
715 * @param __first An input iterator.
716 * @param __last An input iterator.
718 * This function fills a %vector with copies of the elements in the
719 * range [__first,__last).
721 * Note that the assignment completely changes the %vector and
722 * that the resulting %vector's size is the same as the number
723 * of elements assigned.
725 #if __cplusplus >= 201103L
726 template<typename _InputIterator
,
727 typename
= std::_RequireInputIter
<_InputIterator
>>
729 assign(_InputIterator __first
, _InputIterator __last
)
730 { _M_assign_dispatch(__first
, __last
, __false_type()); }
732 template<typename _InputIterator
>
734 assign(_InputIterator __first
, _InputIterator __last
)
736 // Check whether it's an integral type. If so, it's not an iterator.
737 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
738 _M_assign_dispatch(__first
, __last
, _Integral());
742 #if __cplusplus >= 201103L
744 * @brief Assigns an initializer list to a %vector.
745 * @param __l An initializer_list.
747 * This function fills a %vector with copies of the elements in the
748 * initializer list @a __l.
750 * Note that the assignment completely changes the %vector and
751 * that the resulting %vector's size is the same as the number
752 * of elements assigned.
755 assign(initializer_list
<value_type
> __l
)
757 this->_M_assign_aux(__l
.begin(), __l
.end(),
758 random_access_iterator_tag());
762 /// Get a copy of the memory allocation object.
763 using _Base::get_allocator
;
767 * Returns a read/write iterator that points to the first
768 * element in the %vector. Iteration is done in ordinary
772 begin() _GLIBCXX_NOEXCEPT
773 { return iterator(this->_M_impl
._M_start
); }
776 * Returns a read-only (constant) iterator that points to the
777 * first element in the %vector. Iteration is done in ordinary
781 begin() const _GLIBCXX_NOEXCEPT
782 { return const_iterator(this->_M_impl
._M_start
); }
785 * Returns a read/write iterator that points one past the last
786 * element in the %vector. Iteration is done in ordinary
790 end() _GLIBCXX_NOEXCEPT
791 { return iterator(this->_M_impl
._M_finish
); }
794 * Returns a read-only (constant) iterator that points one past
795 * the last element in the %vector. Iteration is done in
796 * ordinary element order.
799 end() const _GLIBCXX_NOEXCEPT
800 { return const_iterator(this->_M_impl
._M_finish
); }
803 * Returns a read/write reverse iterator that points to the
804 * last element in the %vector. Iteration is done in reverse
808 rbegin() _GLIBCXX_NOEXCEPT
809 { return reverse_iterator(end()); }
812 * Returns a read-only (constant) reverse iterator that points
813 * to the last element in the %vector. Iteration is done in
814 * reverse element order.
816 const_reverse_iterator
817 rbegin() const _GLIBCXX_NOEXCEPT
818 { return const_reverse_iterator(end()); }
821 * Returns a read/write reverse iterator that points to one
822 * before the first element in the %vector. Iteration is done
823 * in reverse element order.
826 rend() _GLIBCXX_NOEXCEPT
827 { return reverse_iterator(begin()); }
830 * Returns a read-only (constant) reverse iterator that points
831 * to one before the first element in the %vector. Iteration
832 * is done in reverse element order.
834 const_reverse_iterator
835 rend() const _GLIBCXX_NOEXCEPT
836 { return const_reverse_iterator(begin()); }
838 #if __cplusplus >= 201103L
840 * Returns a read-only (constant) iterator that points to the
841 * first element in the %vector. Iteration is done in ordinary
845 cbegin() const noexcept
846 { return const_iterator(this->_M_impl
._M_start
); }
849 * Returns a read-only (constant) iterator that points one past
850 * the last element in the %vector. Iteration is done in
851 * ordinary element order.
854 cend() const noexcept
855 { return const_iterator(this->_M_impl
._M_finish
); }
858 * Returns a read-only (constant) reverse iterator that points
859 * to the last element in the %vector. Iteration is done in
860 * reverse element order.
862 const_reverse_iterator
863 crbegin() const noexcept
864 { return const_reverse_iterator(end()); }
867 * Returns a read-only (constant) reverse iterator that points
868 * to one before the first element in the %vector. Iteration
869 * is done in reverse element order.
871 const_reverse_iterator
872 crend() const noexcept
873 { return const_reverse_iterator(begin()); }
876 // [23.2.4.2] capacity
877 /** Returns the number of elements in the %vector. */
879 size() const _GLIBCXX_NOEXCEPT
880 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
882 /** Returns the size() of the largest possible %vector. */
884 max_size() const _GLIBCXX_NOEXCEPT
885 { return _S_max_size(_M_get_Tp_allocator()); }
887 #if __cplusplus >= 201103L
889 * @brief Resizes the %vector to the specified number of elements.
890 * @param __new_size Number of elements the %vector should contain.
892 * This function will %resize the %vector to the specified
893 * number of elements. If the number is smaller than the
894 * %vector's current size the %vector is truncated, otherwise
895 * default constructed elements are appended.
898 resize(size_type __new_size
)
900 if (__new_size
> size())
901 _M_default_append(__new_size
- size());
902 else if (__new_size
< size())
903 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
907 * @brief Resizes the %vector to the specified number of elements.
908 * @param __new_size Number of elements the %vector should contain.
909 * @param __x Data with which new elements should be populated.
911 * This function will %resize the %vector to the specified
912 * number of elements. If the number is smaller than the
913 * %vector's current size the %vector is truncated, otherwise
914 * the %vector is extended and new elements are populated with
918 resize(size_type __new_size
, const value_type
& __x
)
920 if (__new_size
> size())
921 _M_fill_insert(end(), __new_size
- size(), __x
);
922 else if (__new_size
< size())
923 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
927 * @brief Resizes the %vector to the specified number of elements.
928 * @param __new_size Number of elements the %vector should contain.
929 * @param __x Data with which new elements should be populated.
931 * This function will %resize the %vector to the specified
932 * number of elements. If the number is smaller than the
933 * %vector's current size the %vector is truncated, otherwise
934 * the %vector is extended and new elements are populated with
938 resize(size_type __new_size
, value_type __x
= value_type())
940 if (__new_size
> size())
941 _M_fill_insert(end(), __new_size
- size(), __x
);
942 else if (__new_size
< size())
943 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
947 #if __cplusplus >= 201103L
948 /** A non-binding request to reduce capacity() to size(). */
951 { _M_shrink_to_fit(); }
955 * Returns the total number of elements that the %vector can
956 * hold before needing to allocate more memory.
959 capacity() const _GLIBCXX_NOEXCEPT
960 { return size_type(this->_M_impl
._M_end_of_storage
961 - this->_M_impl
._M_start
); }
964 * Returns true if the %vector is empty. (Thus begin() would
968 empty() const _GLIBCXX_NOEXCEPT
969 { return begin() == end(); }
972 * @brief Attempt to preallocate enough memory for specified number of
974 * @param __n Number of elements required.
975 * @throw std::length_error If @a n exceeds @c max_size().
977 * This function attempts to reserve enough memory for the
978 * %vector to hold the specified number of elements. If the
979 * number requested is more than max_size(), length_error is
982 * The advantage of this function is that if optimal code is a
983 * necessity and the user can determine the number of elements
984 * that will be required, the user can reserve the memory in
985 * %advance, and thus prevent a possible reallocation of memory
986 * and copying of %vector data.
989 reserve(size_type __n
);
993 * @brief Subscript access to the data contained in the %vector.
994 * @param __n The index of the element for which data should be
996 * @return Read/write reference to data.
998 * This operator allows for easy, array-style, data access.
999 * Note that data access with this operator is unchecked and
1000 * out_of_range lookups are not defined. (For checked lookups
1004 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1006 __glibcxx_requires_subscript(__n
);
1007 return *(this->_M_impl
._M_start
+ __n
);
1011 * @brief Subscript access to the data contained in the %vector.
1012 * @param __n The index of the element for which data should be
1014 * @return Read-only (constant) reference to data.
1016 * This operator allows for easy, array-style, data access.
1017 * Note that data access with this operator is unchecked and
1018 * out_of_range lookups are not defined. (For checked lookups
1022 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1024 __glibcxx_requires_subscript(__n
);
1025 return *(this->_M_impl
._M_start
+ __n
);
1029 /// Safety check used only from at().
1031 _M_range_check(size_type __n
) const
1033 if (__n
>= this->size())
1034 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1035 "(which is %zu) >= this->size() "
1042 * @brief Provides access to the data contained in the %vector.
1043 * @param __n The index of the element for which data should be
1045 * @return Read/write reference to data.
1046 * @throw std::out_of_range If @a __n is an invalid index.
1048 * This function provides for safer data access. The parameter
1049 * is first checked that it is in the range of the vector. The
1050 * function throws out_of_range if the check fails.
1055 _M_range_check(__n
);
1056 return (*this)[__n
];
1060 * @brief Provides access to the data contained in the %vector.
1061 * @param __n The index of the element for which data should be
1063 * @return Read-only (constant) reference to data.
1064 * @throw std::out_of_range If @a __n is an invalid index.
1066 * This function provides for safer data access. The parameter
1067 * is first checked that it is in the range of the vector. The
1068 * function throws out_of_range if the check fails.
1071 at(size_type __n
) const
1073 _M_range_check(__n
);
1074 return (*this)[__n
];
1078 * Returns a read/write reference to the data at the first
1079 * element of the %vector.
1082 front() _GLIBCXX_NOEXCEPT
1084 __glibcxx_requires_nonempty();
1089 * Returns a read-only (constant) reference to the data at the first
1090 * element of the %vector.
1093 front() const _GLIBCXX_NOEXCEPT
1095 __glibcxx_requires_nonempty();
1100 * Returns a read/write reference to the data at the last
1101 * element of the %vector.
1104 back() _GLIBCXX_NOEXCEPT
1106 __glibcxx_requires_nonempty();
1107 return *(end() - 1);
1111 * Returns a read-only (constant) reference to the data at the
1112 * last element of the %vector.
1115 back() const _GLIBCXX_NOEXCEPT
1117 __glibcxx_requires_nonempty();
1118 return *(end() - 1);
1121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1122 // DR 464. Suggestion for new member functions in standard containers.
1125 * Returns a pointer such that [data(), data() + size()) is a valid
1126 * range. For a non-empty %vector, data() == &front().
1129 data() _GLIBCXX_NOEXCEPT
1130 { return _M_data_ptr(this->_M_impl
._M_start
); }
1133 data() const _GLIBCXX_NOEXCEPT
1134 { return _M_data_ptr(this->_M_impl
._M_start
); }
1136 // [23.2.4.3] modifiers
1138 * @brief Add data to the end of the %vector.
1139 * @param __x Data to be added.
1141 * This is a typical stack operation. The function creates an
1142 * element at the end of the %vector and assigns the given data
1143 * to it. Due to the nature of a %vector this operation can be
1144 * done in constant time if the %vector has preallocated space
1148 push_back(const value_type
& __x
)
1150 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1152 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1153 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1155 ++this->_M_impl
._M_finish
;
1156 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1159 _M_realloc_insert(end(), __x
);
1162 #if __cplusplus >= 201103L
1164 push_back(value_type
&& __x
)
1165 { emplace_back(std::move(__x
)); }
1167 template<typename
... _Args
>
1168 #if __cplusplus > 201402L
1173 emplace_back(_Args
&&... __args
);
1177 * @brief Removes last element.
1179 * This is a typical stack operation. It shrinks the %vector by one.
1181 * Note that no data is returned, and if the last element's
1182 * data is needed, it should be retrieved before pop_back() is
1186 pop_back() _GLIBCXX_NOEXCEPT
1188 __glibcxx_requires_nonempty();
1189 --this->_M_impl
._M_finish
;
1190 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1191 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1194 #if __cplusplus >= 201103L
1196 * @brief Inserts an object in %vector before specified iterator.
1197 * @param __position A const_iterator into the %vector.
1198 * @param __args Arguments.
1199 * @return An iterator that points to the inserted data.
1201 * This function will insert an object of type T constructed
1202 * with T(std::forward<Args>(args)...) before the specified location.
1203 * Note that this kind of operation could be expensive for a %vector
1204 * and if it is frequently used the user should consider using
1207 template<typename
... _Args
>
1209 emplace(const_iterator __position
, _Args
&&... __args
)
1210 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1213 * @brief Inserts given value into %vector before specified iterator.
1214 * @param __position A const_iterator into the %vector.
1215 * @param __x Data to be inserted.
1216 * @return An iterator that points to the inserted data.
1218 * This function will insert a copy of the given value before
1219 * the specified location. Note that this kind of operation
1220 * could be expensive for a %vector and if it is frequently
1221 * used the user should consider using std::list.
1224 insert(const_iterator __position
, const value_type
& __x
);
1227 * @brief Inserts given value into %vector before specified iterator.
1228 * @param __position An iterator into the %vector.
1229 * @param __x Data to be inserted.
1230 * @return An iterator that points to the inserted data.
1232 * This function will insert a copy of the given value before
1233 * the specified location. Note that this kind of operation
1234 * could be expensive for a %vector and if it is frequently
1235 * used the user should consider using std::list.
1238 insert(iterator __position
, const value_type
& __x
);
1241 #if __cplusplus >= 201103L
1243 * @brief Inserts given rvalue into %vector before specified iterator.
1244 * @param __position A const_iterator into the %vector.
1245 * @param __x Data to be inserted.
1246 * @return An iterator that points to the inserted data.
1248 * This function will insert a copy of the given rvalue before
1249 * the specified location. Note that this kind of operation
1250 * could be expensive for a %vector and if it is frequently
1251 * used the user should consider using std::list.
1254 insert(const_iterator __position
, value_type
&& __x
)
1255 { return _M_insert_rval(__position
, std::move(__x
)); }
1258 * @brief Inserts an initializer_list into the %vector.
1259 * @param __position An iterator into the %vector.
1260 * @param __l An initializer_list.
1262 * This function will insert copies of the data in the
1263 * initializer_list @a l into the %vector before the location
1264 * specified by @a position.
1266 * Note that this kind of operation could be expensive for a
1267 * %vector and if it is frequently used the user should
1268 * consider using std::list.
1271 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1273 auto __offset
= __position
- cbegin();
1274 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1275 std::random_access_iterator_tag());
1276 return begin() + __offset
;
1280 #if __cplusplus >= 201103L
1282 * @brief Inserts a number of copies of given data into the %vector.
1283 * @param __position A const_iterator into the %vector.
1284 * @param __n Number of elements to be inserted.
1285 * @param __x Data to be inserted.
1286 * @return An iterator that points to the inserted data.
1288 * This function will insert a specified number of copies of
1289 * the given data before the location specified by @a position.
1291 * Note that this kind of operation could be expensive for a
1292 * %vector and if it is frequently used the user should
1293 * consider using std::list.
1296 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1298 difference_type __offset
= __position
- cbegin();
1299 _M_fill_insert(begin() + __offset
, __n
, __x
);
1300 return begin() + __offset
;
1304 * @brief Inserts a number of copies of given data into the %vector.
1305 * @param __position An iterator into the %vector.
1306 * @param __n Number of elements to be inserted.
1307 * @param __x Data to be inserted.
1309 * This function will insert a specified number of copies of
1310 * the given data before the location specified by @a position.
1312 * Note that this kind of operation could be expensive for a
1313 * %vector and if it is frequently used the user should
1314 * consider using std::list.
1317 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1318 { _M_fill_insert(__position
, __n
, __x
); }
1321 #if __cplusplus >= 201103L
1323 * @brief Inserts a range into the %vector.
1324 * @param __position A const_iterator into the %vector.
1325 * @param __first An input iterator.
1326 * @param __last An input iterator.
1327 * @return An iterator that points to the inserted data.
1329 * This function will insert copies of the data in the range
1330 * [__first,__last) into the %vector before the location specified
1333 * Note that this kind of operation could be expensive for a
1334 * %vector and if it is frequently used the user should
1335 * consider using std::list.
1337 template<typename _InputIterator
,
1338 typename
= std::_RequireInputIter
<_InputIterator
>>
1340 insert(const_iterator __position
, _InputIterator __first
,
1341 _InputIterator __last
)
1343 difference_type __offset
= __position
- cbegin();
1344 _M_insert_dispatch(begin() + __offset
,
1345 __first
, __last
, __false_type());
1346 return begin() + __offset
;
1350 * @brief Inserts a range into the %vector.
1351 * @param __position An iterator into the %vector.
1352 * @param __first An input iterator.
1353 * @param __last An input iterator.
1355 * This function will insert copies of the data in the range
1356 * [__first,__last) into the %vector before the location specified
1359 * Note that this kind of operation could be expensive for a
1360 * %vector and if it is frequently used the user should
1361 * consider using std::list.
1363 template<typename _InputIterator
>
1365 insert(iterator __position
, _InputIterator __first
,
1366 _InputIterator __last
)
1368 // Check whether it's an integral type. If so, it's not an iterator.
1369 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1370 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1375 * @brief Remove element at given position.
1376 * @param __position Iterator pointing to element to be erased.
1377 * @return An iterator pointing to the next element (or end()).
1379 * This function will erase the element at the given position and thus
1380 * shorten the %vector by one.
1382 * Note This operation could be expensive and if it is
1383 * frequently used the user should consider using std::list.
1384 * The user is also cautioned that this function only erases
1385 * the element, and that if the element is itself a pointer,
1386 * the pointed-to memory is not touched in any way. Managing
1387 * the pointer is the user's responsibility.
1390 #if __cplusplus >= 201103L
1391 erase(const_iterator __position
)
1392 { return _M_erase(begin() + (__position
- cbegin())); }
1394 erase(iterator __position
)
1395 { return _M_erase(__position
); }
1399 * @brief Remove a range of elements.
1400 * @param __first Iterator pointing to the first element to be erased.
1401 * @param __last Iterator pointing to one past the last element to be
1403 * @return An iterator pointing to the element pointed to by @a __last
1404 * prior to erasing (or end()).
1406 * This function will erase the elements in the range
1407 * [__first,__last) and shorten the %vector accordingly.
1409 * Note This operation could be expensive and if it is
1410 * frequently used the user should consider using std::list.
1411 * The user is also cautioned that this function only erases
1412 * the elements, and that if the elements themselves are
1413 * pointers, the pointed-to memory is not touched in any way.
1414 * Managing the pointer is the user's responsibility.
1417 #if __cplusplus >= 201103L
1418 erase(const_iterator __first
, const_iterator __last
)
1420 const auto __beg
= begin();
1421 const auto __cbeg
= cbegin();
1422 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1425 erase(iterator __first
, iterator __last
)
1426 { return _M_erase(__first
, __last
); }
1430 * @brief Swaps data with another %vector.
1431 * @param __x A %vector of the same element and allocator types.
1433 * This exchanges the elements between two vectors in constant time.
1434 * (Three pointers, so it should be quite fast.)
1435 * Note that the global std::swap() function is specialized such that
1436 * std::swap(v1,v2) will feed to this function.
1438 * Whether the allocators are swapped depends on the allocator traits.
1441 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1443 #if __cplusplus >= 201103L
1444 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1445 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1447 this->_M_impl
._M_swap_data(__x
._M_impl
);
1448 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1449 __x
._M_get_Tp_allocator());
1453 * Erases all the elements. Note that this function only erases the
1454 * elements, and that if the elements themselves are pointers, the
1455 * pointed-to memory is not touched in any way. Managing the pointer is
1456 * the user's responsibility.
1459 clear() _GLIBCXX_NOEXCEPT
1460 { _M_erase_at_end(this->_M_impl
._M_start
); }
1464 * Memory expansion handler. Uses the member allocation function to
1465 * obtain @a n bytes of memory, and then copies [first,last) into it.
1467 template<typename _ForwardIterator
>
1469 _M_allocate_and_copy(size_type __n
,
1470 _ForwardIterator __first
, _ForwardIterator __last
)
1472 pointer __result
= this->_M_allocate(__n
);
1475 std::__uninitialized_copy_a(__first
, __last
, __result
,
1476 _M_get_Tp_allocator());
1481 _M_deallocate(__result
, __n
);
1482 __throw_exception_again
;
1487 // Internal constructor functions follow.
1489 // Called by the range constructor to implement [23.1.1]/9
1491 #if __cplusplus < 201103L
1492 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1493 // 438. Ambiguity in the "do the right thing" clause
1494 template<typename _Integer
>
1496 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1498 this->_M_impl
._M_start
= _M_allocate(_S_check_init_len(
1499 static_cast<size_type
>(__n
), _M_get_Tp_allocator()));
1500 this->_M_impl
._M_end_of_storage
=
1501 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1502 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1505 // Called by the range constructor to implement [23.1.1]/9
1506 template<typename _InputIterator
>
1508 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1511 _M_range_initialize(__first
, __last
,
1512 std::__iterator_category(__first
));
1516 // Called by the second initialize_dispatch above
1517 template<typename _InputIterator
>
1519 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1520 std::input_iterator_tag
)
1523 for (; __first
!= __last
; ++__first
)
1524 #if __cplusplus >= 201103L
1525 emplace_back(*__first
);
1527 push_back(*__first
);
1531 __throw_exception_again
;
1535 // Called by the second initialize_dispatch above
1536 template<typename _ForwardIterator
>
1538 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1539 std::forward_iterator_tag
)
1541 const size_type __n
= std::distance(__first
, __last
);
1542 this->_M_impl
._M_start
1543 = this->_M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1544 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1545 this->_M_impl
._M_finish
=
1546 std::__uninitialized_copy_a(__first
, __last
,
1547 this->_M_impl
._M_start
,
1548 _M_get_Tp_allocator());
1551 // Called by the first initialize_dispatch above and by the
1552 // vector(n,value,a) constructor.
1554 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1556 this->_M_impl
._M_finish
=
1557 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1558 _M_get_Tp_allocator());
1561 #if __cplusplus >= 201103L
1562 // Called by the vector(n) constructor.
1564 _M_default_initialize(size_type __n
)
1566 this->_M_impl
._M_finish
=
1567 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1568 _M_get_Tp_allocator());
1572 // Internal assign functions follow. The *_aux functions do the actual
1573 // assignment work for the range versions.
1575 // Called by the range assign to implement [23.1.1]/9
1577 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1578 // 438. Ambiguity in the "do the right thing" clause
1579 template<typename _Integer
>
1581 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1582 { _M_fill_assign(__n
, __val
); }
1584 // Called by the range assign to implement [23.1.1]/9
1585 template<typename _InputIterator
>
1587 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1589 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1591 // Called by the second assign_dispatch above
1592 template<typename _InputIterator
>
1594 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1595 std::input_iterator_tag
);
1597 // Called by the second assign_dispatch above
1598 template<typename _ForwardIterator
>
1600 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1601 std::forward_iterator_tag
);
1603 // Called by assign(n,t), and the range assign when it turns out
1604 // to be the same thing.
1606 _M_fill_assign(size_type __n
, const value_type
& __val
);
1608 // Internal insert functions follow.
1610 // Called by the range insert to implement [23.1.1]/9
1612 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1613 // 438. Ambiguity in the "do the right thing" clause
1614 template<typename _Integer
>
1616 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1618 { _M_fill_insert(__pos
, __n
, __val
); }
1620 // Called by the range insert to implement [23.1.1]/9
1621 template<typename _InputIterator
>
1623 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1624 _InputIterator __last
, __false_type
)
1626 _M_range_insert(__pos
, __first
, __last
,
1627 std::__iterator_category(__first
));
1630 // Called by the second insert_dispatch above
1631 template<typename _InputIterator
>
1633 _M_range_insert(iterator __pos
, _InputIterator __first
,
1634 _InputIterator __last
, std::input_iterator_tag
);
1636 // Called by the second insert_dispatch above
1637 template<typename _ForwardIterator
>
1639 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1640 _ForwardIterator __last
, std::forward_iterator_tag
);
1642 // Called by insert(p,n,x), and the range insert when it turns out to be
1645 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1647 #if __cplusplus >= 201103L
1648 // Called by resize(n).
1650 _M_default_append(size_type __n
);
1656 #if __cplusplus < 201103L
1657 // Called by insert(p,x)
1659 _M_insert_aux(iterator __position
, const value_type
& __x
);
1662 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1664 // A value_type object constructed with _Alloc_traits::construct()
1665 // and destroyed with _Alloc_traits::destroy().
1666 struct _Temporary_value
1668 template<typename
... _Args
>
1670 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1672 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1673 std::forward
<_Args
>(__args
)...);
1677 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1680 _M_val() { return *_M_ptr(); }
1684 _M_ptr() { return reinterpret_cast<_Tp
*>(&__buf
); }
1687 typename aligned_storage
<sizeof(_Tp
), alignof(_Tp
)>::type __buf
;
1690 // Called by insert(p,x) and other functions when insertion needs to
1691 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1692 template<typename _Arg
>
1694 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1696 template<typename
... _Args
>
1698 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1700 // Either move-construct at the end, or forward to _M_insert_aux.
1702 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1704 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1705 template<typename
... _Args
>
1707 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1709 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1711 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1712 { return _M_insert_rval(__position
, std::move(__v
)); }
1715 // Called by _M_fill_insert, _M_insert_aux etc.
1717 _M_check_len(size_type __n
, const char* __s
) const
1719 if (max_size() - size() < __n
)
1720 __throw_length_error(__N(__s
));
1722 const size_type __len
= size() + (std::max
)(size(), __n
);
1723 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1726 // Called by constructors to check initial size.
1728 _S_check_init_len(size_type __n
, const allocator_type
& __a
)
1730 if (__n
> _S_max_size(_Tp_alloc_type(__a
)))
1731 __throw_length_error(
1732 __N("cannot create std::vector larger than max_size()"));
1737 _S_max_size(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
1739 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1740 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1741 // (even if std::allocator_traits::max_size says we can).
1742 const size_t __diffmax
1743 = __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
1744 const size_t __allocmax
= _Alloc_traits::max_size(__a
);
1745 return (std::min
)(__diffmax
, __allocmax
);
1748 // Internal erase functions follow.
1750 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1753 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1755 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1757 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1758 _M_get_Tp_allocator());
1759 this->_M_impl
._M_finish
= __pos
;
1760 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1765 _M_erase(iterator __position
);
1768 _M_erase(iterator __first
, iterator __last
);
1770 #if __cplusplus >= 201103L
1772 // Constant-time move assignment when source object's memory can be
1773 // moved, either because the source's allocator will move too
1774 // or because the allocators are equal.
1776 _M_move_assign(vector
&& __x
, true_type
) noexcept
1778 vector
__tmp(get_allocator());
1779 this->_M_impl
._M_swap_data(__x
._M_impl
);
1780 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
1781 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1784 // Do move assignment when it might not be possible to move source
1785 // object's memory, resulting in a linear-time operation.
1787 _M_move_assign(vector
&& __x
, false_type
)
1789 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1790 _M_move_assign(std::move(__x
), true_type());
1793 // The rvalue's allocator cannot be moved and is not equal,
1794 // so we need to individually move each element.
1795 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1796 std::__make_move_if_noexcept_iterator(__x
.end()));
1802 template<typename _Up
>
1804 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
1807 #if __cplusplus >= 201103L
1808 template<typename _Ptr
>
1809 typename
std::pointer_traits
<_Ptr
>::element_type
*
1810 _M_data_ptr(_Ptr __ptr
) const
1811 { return empty() ? nullptr : std::__to_address(__ptr
); }
1813 template<typename _Up
>
1815 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
1818 template<typename _Ptr
>
1820 _M_data_ptr(_Ptr __ptr
)
1821 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
1823 template<typename _Ptr
>
1825 _M_data_ptr(_Ptr __ptr
) const
1826 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
1830 #if __cpp_deduction_guides >= 201606
1831 template<typename _InputIterator
, typename _ValT
1832 = typename iterator_traits
<_InputIterator
>::value_type
,
1833 typename _Allocator
= allocator
<_ValT
>,
1834 typename
= _RequireInputIter
<_InputIterator
>,
1835 typename
= _RequireAllocator
<_Allocator
>>
1836 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1837 -> vector
<_ValT
, _Allocator
>;
1841 * @brief Vector equality comparison.
1842 * @param __x A %vector.
1843 * @param __y A %vector of the same type as @a __x.
1844 * @return True iff the size and elements of the vectors are equal.
1846 * This is an equivalence relation. It is linear in the size of the
1847 * vectors. Vectors are considered equivalent if their sizes are equal,
1848 * and if corresponding elements compare equal.
1850 template<typename _Tp
, typename _Alloc
>
1852 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1853 { return (__x
.size() == __y
.size()
1854 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1857 * @brief Vector ordering relation.
1858 * @param __x A %vector.
1859 * @param __y A %vector of the same type as @a __x.
1860 * @return True iff @a __x is lexicographically less than @a __y.
1862 * This is a total ordering relation. It is linear in the size of the
1863 * vectors. The elements must be comparable with @c <.
1865 * See std::lexicographical_compare() for how the determination is made.
1867 template<typename _Tp
, typename _Alloc
>
1869 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1870 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1871 __y
.begin(), __y
.end()); }
1873 /// Based on operator==
1874 template<typename _Tp
, typename _Alloc
>
1876 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1877 { return !(__x
== __y
); }
1879 /// Based on operator<
1880 template<typename _Tp
, typename _Alloc
>
1882 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1883 { return __y
< __x
; }
1885 /// Based on operator<
1886 template<typename _Tp
, typename _Alloc
>
1888 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1889 { return !(__y
< __x
); }
1891 /// Based on operator<
1892 template<typename _Tp
, typename _Alloc
>
1894 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1895 { return !(__x
< __y
); }
1897 /// See std::vector::swap().
1898 template<typename _Tp
, typename _Alloc
>
1900 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1901 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1904 _GLIBCXX_END_NAMESPACE_CONTAINER
1905 _GLIBCXX_END_NAMESPACE_VERSION
1908 #endif /* _STL_VECTOR_H */