1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2020 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>
65 #if __cplusplus > 201703L
69 #include <debug/assertions.h>
71 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
73 __sanitizer_annotate_contiguous_container(const void*, const void*,
74 const void*, const void*);
77 namespace std
_GLIBCXX_VISIBILITY(default)
79 _GLIBCXX_BEGIN_NAMESPACE_VERSION
80 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
82 /// See bits/stl_deque.h's _Deque_base for an explanation.
83 template<typename _Tp
, typename _Alloc
>
86 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
87 rebind
<_Tp
>::other _Tp_alloc_type
;
88 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
91 struct _Vector_impl_data
95 pointer _M_end_of_storage
;
97 _Vector_impl_data() _GLIBCXX_NOEXCEPT
98 : _M_start(), _M_finish(), _M_end_of_storage()
101 #if __cplusplus >= 201103L
102 _Vector_impl_data(_Vector_impl_data
&& __x
) noexcept
103 : _M_start(__x
._M_start
), _M_finish(__x
._M_finish
),
104 _M_end_of_storage(__x
._M_end_of_storage
)
105 { __x
._M_start
= __x
._M_finish
= __x
._M_end_of_storage
= pointer(); }
109 _M_copy_data(_Vector_impl_data
const& __x
) _GLIBCXX_NOEXCEPT
111 _M_start
= __x
._M_start
;
112 _M_finish
= __x
._M_finish
;
113 _M_end_of_storage
= __x
._M_end_of_storage
;
117 _M_swap_data(_Vector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
119 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
120 // information used by TBAA.
121 _Vector_impl_data __tmp
;
122 __tmp
._M_copy_data(*this);
124 __x
._M_copy_data(__tmp
);
129 : public _Tp_alloc_type
, public _Vector_impl_data
131 _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
132 is_nothrow_default_constructible
<_Tp_alloc_type
>::value
)
136 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
137 : _Tp_alloc_type(__a
)
140 #if __cplusplus >= 201103L
141 // Not defaulted, to enforce noexcept(true) even when
142 // !is_nothrow_move_constructible<_Tp_alloc_type>.
143 _Vector_impl(_Vector_impl
&& __x
) noexcept
144 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
147 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
148 : _Tp_alloc_type(std::move(__a
))
151 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
152 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
156 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
157 template<typename
= _Tp_alloc_type
>
160 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
161 ::size_type size_type
;
163 static void _S_shrink(_Vector_impl
&, size_type
) { }
164 static void _S_on_dealloc(_Vector_impl
&) { }
166 typedef _Vector_impl
& _Reinit
;
170 _Grow(_Vector_impl
&, size_type
) { }
171 void _M_grew(size_type
) { }
175 // Enable ASan annotations for memory obtained from std::allocator.
176 template<typename _Up
>
177 struct _Asan
<allocator
<_Up
> >
179 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
180 ::size_type size_type
;
182 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
183 // mark end of valid region as __curr instead of __prev.
185 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
187 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
188 __impl
._M_end_of_storage
, __prev
, __curr
);
192 _S_grow(_Vector_impl
& __impl
, size_type __n
)
193 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
196 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
197 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
200 _S_on_dealloc(_Vector_impl
& __impl
)
203 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
206 // Used on reallocation to tell ASan unused capacity is invalid.
209 explicit _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
211 // Mark unused capacity as valid again before deallocating it.
212 _S_on_dealloc(_M_impl
);
217 // Mark unused capacity as invalid after reallocation.
218 if (_M_impl
._M_start
)
219 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
223 _Vector_impl
& _M_impl
;
225 #if __cplusplus >= 201103L
226 _Reinit(const _Reinit
&) = delete;
227 _Reinit
& operator=(const _Reinit
&) = delete;
231 // Tell ASan when unused capacity is initialized to be valid.
234 _Grow(_Vector_impl
& __impl
, size_type __n
)
235 : _M_impl(__impl
), _M_n(__n
)
236 { _S_grow(_M_impl
, __n
); }
238 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
240 void _M_grew(size_type __n
) { _M_n
-= __n
; }
242 #if __cplusplus >= 201103L
243 _Grow(const _Grow
&) = delete;
244 _Grow
& operator=(const _Grow
&) = delete;
247 _Vector_impl
& _M_impl
;
252 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
253 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
254 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
255 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
256 typename _Base::_Vector_impl::template _Asan<>::_Grow \
257 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
258 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
259 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
260 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
261 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
262 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
263 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
264 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
265 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
266 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
267 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
268 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
269 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
273 typedef _Alloc allocator_type
;
276 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
277 { return this->_M_impl
; }
279 const _Tp_alloc_type
&
280 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
281 { return this->_M_impl
; }
284 get_allocator() const _GLIBCXX_NOEXCEPT
285 { return allocator_type(_M_get_Tp_allocator()); }
287 #if __cplusplus >= 201103L
288 _Vector_base() = default;
293 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
296 // Kept for ABI compatibility.
297 #if !_GLIBCXX_INLINE_VERSION
298 _Vector_base(size_t __n
)
300 { _M_create_storage(__n
); }
303 _Vector_base(size_t __n
, const allocator_type
& __a
)
305 { _M_create_storage(__n
); }
307 #if __cplusplus >= 201103L
308 _Vector_base(_Vector_base
&&) = default;
310 // Kept for ABI compatibility.
311 # if !_GLIBCXX_INLINE_VERSION
312 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
313 : _M_impl(std::move(__a
)) { }
315 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
318 if (__x
.get_allocator() == __a
)
319 this->_M_impl
._M_swap_data(__x
._M_impl
);
322 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
323 _M_create_storage(__n
);
328 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
329 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
333 ~_Vector_base() _GLIBCXX_NOEXCEPT
335 _M_deallocate(_M_impl
._M_start
,
336 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
340 _Vector_impl _M_impl
;
343 _M_allocate(size_t __n
)
345 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
346 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
350 _M_deallocate(pointer __p
, size_t __n
)
352 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
354 _Tr::deallocate(_M_impl
, __p
, __n
);
359 _M_create_storage(size_t __n
)
361 this->_M_impl
._M_start
= this->_M_allocate(__n
);
362 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
363 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
368 * @brief A standard container which offers fixed time access to
369 * individual elements in any order.
373 * @tparam _Tp Type of element.
374 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
376 * Meets the requirements of a <a href="tables.html#65">container</a>, a
377 * <a href="tables.html#66">reversible container</a>, and a
378 * <a href="tables.html#67">sequence</a>, including the
379 * <a href="tables.html#68">optional sequence requirements</a> with the
380 * %exception of @c push_front and @c pop_front.
382 * In some terminology a %vector can be described as a dynamic
383 * C-style array, it offers fast and efficient access to individual
384 * elements in any order and saves the user from worrying about
385 * memory and size allocation. Subscripting ( @c [] ) access is
386 * also provided as with C-style arrays.
388 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
389 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
391 #ifdef _GLIBCXX_CONCEPT_CHECKS
392 // Concept requirements.
393 typedef typename
_Alloc::value_type _Alloc_value_type
;
394 # if __cplusplus < 201103L
395 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
397 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
400 #if __cplusplus >= 201103L
401 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
402 "std::vector must have a non-const, non-volatile value_type");
403 # if __cplusplus > 201703L || defined __STRICT_ANSI__
404 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
405 "std::vector must have the same value_type as its allocator");
409 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
410 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
411 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
414 typedef _Tp value_type
;
415 typedef typename
_Base::pointer pointer
;
416 typedef typename
_Alloc_traits::const_pointer const_pointer
;
417 typedef typename
_Alloc_traits::reference reference
;
418 typedef typename
_Alloc_traits::const_reference const_reference
;
419 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
420 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
422 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
423 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
424 typedef size_t size_type
;
425 typedef ptrdiff_t difference_type
;
426 typedef _Alloc allocator_type
;
429 #if __cplusplus >= 201103L
430 static constexpr bool
431 _S_nothrow_relocate(true_type
)
433 return noexcept(std::__relocate_a(std::declval
<pointer
>(),
434 std::declval
<pointer
>(),
435 std::declval
<pointer
>(),
436 std::declval
<_Tp_alloc_type
&>()));
439 static constexpr bool
440 _S_nothrow_relocate(false_type
)
443 static constexpr bool
446 // Instantiating std::__relocate_a might cause an error outside the
447 // immediate context (in __relocate_object_a's noexcept-specifier),
448 // so only do it if we know the type can be move-inserted into *this.
449 return _S_nothrow_relocate(__is_move_insertable
<_Tp_alloc_type
>{});
453 _S_do_relocate(pointer __first
, pointer __last
, pointer __result
,
454 _Tp_alloc_type
& __alloc
, true_type
) noexcept
456 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
460 _S_do_relocate(pointer
, pointer
, pointer __result
,
461 _Tp_alloc_type
&, false_type
) noexcept
465 _S_relocate(pointer __first
, pointer __last
, pointer __result
,
466 _Tp_alloc_type
& __alloc
) noexcept
468 using __do_it
= __bool_constant
<_S_use_relocate()>;
469 return _S_do_relocate(__first
, __last
, __result
, __alloc
, __do_it
{});
474 using _Base::_M_allocate
;
475 using _Base::_M_deallocate
;
476 using _Base::_M_impl
;
477 using _Base::_M_get_Tp_allocator
;
480 // [23.2.4.1] construct/copy/destroy
481 // (assign() and get_allocator() are also listed in this section)
484 * @brief Creates a %vector with no elements.
486 #if __cplusplus >= 201103L
493 * @brief Creates a %vector with no elements.
494 * @param __a An allocator object.
497 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
500 #if __cplusplus >= 201103L
502 * @brief Creates a %vector with default constructed elements.
503 * @param __n The number of elements to initially create.
504 * @param __a An allocator.
506 * This constructor fills the %vector with @a __n default
507 * constructed elements.
510 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
511 : _Base(_S_check_init_len(__n
, __a
), __a
)
512 { _M_default_initialize(__n
); }
515 * @brief Creates a %vector with copies of an exemplar element.
516 * @param __n The number of elements to initially create.
517 * @param __value An element to copy.
518 * @param __a An allocator.
520 * This constructor fills the %vector with @a __n copies of @a __value.
522 vector(size_type __n
, const value_type
& __value
,
523 const allocator_type
& __a
= allocator_type())
524 : _Base(_S_check_init_len(__n
, __a
), __a
)
525 { _M_fill_initialize(__n
, __value
); }
528 * @brief Creates a %vector with copies of an exemplar element.
529 * @param __n The number of elements to initially create.
530 * @param __value An element to copy.
531 * @param __a An allocator.
533 * This constructor fills the %vector with @a __n copies of @a __value.
536 vector(size_type __n
, const value_type
& __value
= value_type(),
537 const allocator_type
& __a
= allocator_type())
538 : _Base(_S_check_init_len(__n
, __a
), __a
)
539 { _M_fill_initialize(__n
, __value
); }
543 * @brief %Vector copy constructor.
544 * @param __x A %vector of identical element and allocator types.
546 * All the elements of @a __x are copied, but any unused capacity in
547 * @a __x will not be copied
548 * (i.e. capacity() == size() in the new %vector).
550 * The newly-created %vector uses a copy of the allocator object used
551 * by @a __x (unless the allocator traits dictate a different object).
553 vector(const vector
& __x
)
555 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
557 this->_M_impl
._M_finish
=
558 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
559 this->_M_impl
._M_start
,
560 _M_get_Tp_allocator());
563 #if __cplusplus >= 201103L
565 * @brief %Vector move constructor.
567 * The newly-created %vector contains the exact contents of the
569 * The contents of the moved instance are a valid, but unspecified
572 vector(vector
&&) noexcept
= default;
574 /// Copy constructor with alternative allocator
575 vector(const vector
& __x
, const allocator_type
& __a
)
576 : _Base(__x
.size(), __a
)
578 this->_M_impl
._M_finish
=
579 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
580 this->_M_impl
._M_start
,
581 _M_get_Tp_allocator());
585 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
586 : _Base(__m
, std::move(__rv
))
589 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
592 if (__rv
.get_allocator() == __m
)
593 this->_M_impl
._M_swap_data(__rv
._M_impl
);
594 else if (!__rv
.empty())
596 this->_M_create_storage(__rv
.size());
597 this->_M_impl
._M_finish
=
598 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
599 this->_M_impl
._M_start
,
600 _M_get_Tp_allocator());
606 /// Move constructor with alternative allocator
607 vector(vector
&& __rv
, const allocator_type
& __m
)
609 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
610 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
611 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
615 * @brief Builds a %vector from an initializer list.
616 * @param __l An initializer_list.
617 * @param __a An allocator.
619 * Create a %vector consisting of copies of the elements in the
620 * initializer_list @a __l.
622 * This will call the element type's copy constructor N times
623 * (where N is @a __l.size()) and do no memory reallocation.
625 vector(initializer_list
<value_type
> __l
,
626 const allocator_type
& __a
= allocator_type())
629 _M_range_initialize(__l
.begin(), __l
.end(),
630 random_access_iterator_tag());
635 * @brief Builds a %vector from a range.
636 * @param __first An input iterator.
637 * @param __last An input iterator.
638 * @param __a An allocator.
640 * Create a %vector consisting of copies of the elements from
643 * If the iterators are forward, bidirectional, or
644 * random-access, then this will call the elements' copy
645 * constructor N times (where N is distance(first,last)) and do
646 * no memory reallocation. But if only input iterators are
647 * used, then this will do at most 2N calls to the copy
648 * constructor, and logN memory reallocations.
650 #if __cplusplus >= 201103L
651 template<typename _InputIterator
,
652 typename
= std::_RequireInputIter
<_InputIterator
>>
653 vector(_InputIterator __first
, _InputIterator __last
,
654 const allocator_type
& __a
= allocator_type())
657 _M_range_initialize(__first
, __last
,
658 std::__iterator_category(__first
));
661 template<typename _InputIterator
>
662 vector(_InputIterator __first
, _InputIterator __last
,
663 const allocator_type
& __a
= allocator_type())
666 // Check whether it's an integral type. If so, it's not an iterator.
667 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
668 _M_initialize_dispatch(__first
, __last
, _Integral());
673 * The dtor only erases the elements, and note that if the
674 * elements themselves are pointers, the pointed-to memory is
675 * not touched in any way. Managing the pointer is the user's
678 ~vector() _GLIBCXX_NOEXCEPT
680 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
681 _M_get_Tp_allocator());
682 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
686 * @brief %Vector assignment operator.
687 * @param __x A %vector of identical element and allocator types.
689 * All the elements of @a __x are copied, but any unused capacity in
690 * @a __x will not be copied.
692 * Whether the allocator is copied depends on the allocator traits.
695 operator=(const vector
& __x
);
697 #if __cplusplus >= 201103L
699 * @brief %Vector move assignment operator.
700 * @param __x A %vector of identical element and allocator types.
702 * The contents of @a __x are moved into this %vector (without copying,
703 * if the allocators permit it).
704 * Afterwards @a __x is a valid, but unspecified %vector.
706 * Whether the allocator is moved depends on the allocator traits.
709 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
711 constexpr bool __move_storage
=
712 _Alloc_traits::_S_propagate_on_move_assign()
713 || _Alloc_traits::_S_always_equal();
714 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
719 * @brief %Vector list assignment operator.
720 * @param __l An initializer_list.
722 * This function fills a %vector with copies of the elements in the
723 * initializer list @a __l.
725 * Note that the assignment completely changes the %vector and
726 * that the resulting %vector's size is the same as the number
727 * of elements assigned.
730 operator=(initializer_list
<value_type
> __l
)
732 this->_M_assign_aux(__l
.begin(), __l
.end(),
733 random_access_iterator_tag());
739 * @brief Assigns a given value to a %vector.
740 * @param __n Number of elements to be assigned.
741 * @param __val Value to be assigned.
743 * This function fills a %vector with @a __n copies of the given
744 * value. Note that the assignment completely changes the
745 * %vector and that the resulting %vector's size is the same as
746 * the number of elements assigned.
749 assign(size_type __n
, const value_type
& __val
)
750 { _M_fill_assign(__n
, __val
); }
753 * @brief Assigns a range to a %vector.
754 * @param __first An input iterator.
755 * @param __last An input iterator.
757 * This function fills a %vector with copies of the elements in the
758 * range [__first,__last).
760 * Note that the assignment completely changes the %vector and
761 * that the resulting %vector's size is the same as the number
762 * of elements assigned.
764 #if __cplusplus >= 201103L
765 template<typename _InputIterator
,
766 typename
= std::_RequireInputIter
<_InputIterator
>>
768 assign(_InputIterator __first
, _InputIterator __last
)
769 { _M_assign_dispatch(__first
, __last
, __false_type()); }
771 template<typename _InputIterator
>
773 assign(_InputIterator __first
, _InputIterator __last
)
775 // Check whether it's an integral type. If so, it's not an iterator.
776 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
777 _M_assign_dispatch(__first
, __last
, _Integral());
781 #if __cplusplus >= 201103L
783 * @brief Assigns an initializer list to a %vector.
784 * @param __l An initializer_list.
786 * This function fills a %vector with copies of the elements in the
787 * initializer list @a __l.
789 * Note that the assignment completely changes the %vector and
790 * that the resulting %vector's size is the same as the number
791 * of elements assigned.
794 assign(initializer_list
<value_type
> __l
)
796 this->_M_assign_aux(__l
.begin(), __l
.end(),
797 random_access_iterator_tag());
801 /// Get a copy of the memory allocation object.
802 using _Base::get_allocator
;
806 * Returns a read/write iterator that points to the first
807 * element in the %vector. Iteration is done in ordinary
811 begin() _GLIBCXX_NOEXCEPT
812 { return iterator(this->_M_impl
._M_start
); }
815 * Returns a read-only (constant) iterator that points to the
816 * first element in the %vector. Iteration is done in ordinary
820 begin() const _GLIBCXX_NOEXCEPT
821 { return const_iterator(this->_M_impl
._M_start
); }
824 * Returns a read/write iterator that points one past the last
825 * element in the %vector. Iteration is done in ordinary
829 end() _GLIBCXX_NOEXCEPT
830 { return iterator(this->_M_impl
._M_finish
); }
833 * Returns a read-only (constant) iterator that points one past
834 * the last element in the %vector. Iteration is done in
835 * ordinary element order.
838 end() const _GLIBCXX_NOEXCEPT
839 { return const_iterator(this->_M_impl
._M_finish
); }
842 * Returns a read/write reverse iterator that points to the
843 * last element in the %vector. Iteration is done in reverse
847 rbegin() _GLIBCXX_NOEXCEPT
848 { return reverse_iterator(end()); }
851 * Returns a read-only (constant) reverse iterator that points
852 * to the last element in the %vector. Iteration is done in
853 * reverse element order.
855 const_reverse_iterator
856 rbegin() const _GLIBCXX_NOEXCEPT
857 { return const_reverse_iterator(end()); }
860 * Returns a read/write reverse iterator that points to one
861 * before the first element in the %vector. Iteration is done
862 * in reverse element order.
865 rend() _GLIBCXX_NOEXCEPT
866 { return reverse_iterator(begin()); }
869 * Returns a read-only (constant) reverse iterator that points
870 * to one before the first element in the %vector. Iteration
871 * is done in reverse element order.
873 const_reverse_iterator
874 rend() const _GLIBCXX_NOEXCEPT
875 { return const_reverse_iterator(begin()); }
877 #if __cplusplus >= 201103L
879 * Returns a read-only (constant) iterator that points to the
880 * first element in the %vector. Iteration is done in ordinary
884 cbegin() const noexcept
885 { return const_iterator(this->_M_impl
._M_start
); }
888 * Returns a read-only (constant) iterator that points one past
889 * the last element in the %vector. Iteration is done in
890 * ordinary element order.
893 cend() const noexcept
894 { return const_iterator(this->_M_impl
._M_finish
); }
897 * Returns a read-only (constant) reverse iterator that points
898 * to the last element in the %vector. Iteration is done in
899 * reverse element order.
901 const_reverse_iterator
902 crbegin() const noexcept
903 { return const_reverse_iterator(end()); }
906 * Returns a read-only (constant) reverse iterator that points
907 * to one before the first element in the %vector. Iteration
908 * is done in reverse element order.
910 const_reverse_iterator
911 crend() const noexcept
912 { return const_reverse_iterator(begin()); }
915 // [23.2.4.2] capacity
916 /** Returns the number of elements in the %vector. */
918 size() const _GLIBCXX_NOEXCEPT
919 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
921 /** Returns the size() of the largest possible %vector. */
923 max_size() const _GLIBCXX_NOEXCEPT
924 { return _S_max_size(_M_get_Tp_allocator()); }
926 #if __cplusplus >= 201103L
928 * @brief Resizes the %vector to the specified number of elements.
929 * @param __new_size Number of elements the %vector should contain.
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 * default constructed elements are appended.
937 resize(size_type __new_size
)
939 if (__new_size
> size())
940 _M_default_append(__new_size
- size());
941 else if (__new_size
< size())
942 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
946 * @brief Resizes the %vector to the specified number of elements.
947 * @param __new_size Number of elements the %vector should contain.
948 * @param __x Data with which new elements should be populated.
950 * This function will %resize the %vector to the specified
951 * number of elements. If the number is smaller than the
952 * %vector's current size the %vector is truncated, otherwise
953 * the %vector is extended and new elements are populated with
957 resize(size_type __new_size
, const value_type
& __x
)
959 if (__new_size
> size())
960 _M_fill_insert(end(), __new_size
- size(), __x
);
961 else if (__new_size
< size())
962 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
966 * @brief Resizes the %vector to the specified number of elements.
967 * @param __new_size Number of elements the %vector should contain.
968 * @param __x Data with which new elements should be populated.
970 * This function will %resize the %vector to the specified
971 * number of elements. If the number is smaller than the
972 * %vector's current size the %vector is truncated, otherwise
973 * the %vector is extended and new elements are populated with
977 resize(size_type __new_size
, value_type __x
= value_type())
979 if (__new_size
> size())
980 _M_fill_insert(end(), __new_size
- size(), __x
);
981 else if (__new_size
< size())
982 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
986 #if __cplusplus >= 201103L
987 /** A non-binding request to reduce capacity() to size(). */
990 { _M_shrink_to_fit(); }
994 * Returns the total number of elements that the %vector can
995 * hold before needing to allocate more memory.
998 capacity() const _GLIBCXX_NOEXCEPT
999 { return size_type(this->_M_impl
._M_end_of_storage
1000 - this->_M_impl
._M_start
); }
1003 * Returns true if the %vector is empty. (Thus begin() would
1006 _GLIBCXX_NODISCARD
bool
1007 empty() const _GLIBCXX_NOEXCEPT
1008 { return begin() == end(); }
1011 * @brief Attempt to preallocate enough memory for specified number of
1013 * @param __n Number of elements required.
1014 * @throw std::length_error If @a n exceeds @c max_size().
1016 * This function attempts to reserve enough memory for the
1017 * %vector to hold the specified number of elements. If the
1018 * number requested is more than max_size(), length_error is
1021 * The advantage of this function is that if optimal code is a
1022 * necessity and the user can determine the number of elements
1023 * that will be required, the user can reserve the memory in
1024 * %advance, and thus prevent a possible reallocation of memory
1025 * and copying of %vector data.
1028 reserve(size_type __n
);
1032 * @brief Subscript 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.
1037 * This operator allows for easy, array-style, data access.
1038 * Note that data access with this operator is unchecked and
1039 * out_of_range lookups are not defined. (For checked lookups
1043 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1045 __glibcxx_requires_subscript(__n
);
1046 return *(this->_M_impl
._M_start
+ __n
);
1050 * @brief Subscript 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.
1055 * This operator allows for easy, array-style, data access.
1056 * Note that data access with this operator is unchecked and
1057 * out_of_range lookups are not defined. (For checked lookups
1061 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1063 __glibcxx_requires_subscript(__n
);
1064 return *(this->_M_impl
._M_start
+ __n
);
1068 /// Safety check used only from at().
1070 _M_range_check(size_type __n
) const
1072 if (__n
>= this->size())
1073 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1074 "(which is %zu) >= this->size() "
1081 * @brief Provides access to the data contained in the %vector.
1082 * @param __n The index of the element for which data should be
1084 * @return Read/write reference to data.
1085 * @throw std::out_of_range If @a __n is an invalid index.
1087 * This function provides for safer data access. The parameter
1088 * is first checked that it is in the range of the vector. The
1089 * function throws out_of_range if the check fails.
1094 _M_range_check(__n
);
1095 return (*this)[__n
];
1099 * @brief Provides access to the data contained in the %vector.
1100 * @param __n The index of the element for which data should be
1102 * @return Read-only (constant) reference to data.
1103 * @throw std::out_of_range If @a __n is an invalid index.
1105 * This function provides for safer data access. The parameter
1106 * is first checked that it is in the range of the vector. The
1107 * function throws out_of_range if the check fails.
1110 at(size_type __n
) const
1112 _M_range_check(__n
);
1113 return (*this)[__n
];
1117 * Returns a read/write reference to the data at the first
1118 * element of the %vector.
1121 front() _GLIBCXX_NOEXCEPT
1123 __glibcxx_requires_nonempty();
1128 * Returns a read-only (constant) reference to the data at the first
1129 * element of the %vector.
1132 front() const _GLIBCXX_NOEXCEPT
1134 __glibcxx_requires_nonempty();
1139 * Returns a read/write reference to the data at the last
1140 * element of the %vector.
1143 back() _GLIBCXX_NOEXCEPT
1145 __glibcxx_requires_nonempty();
1146 return *(end() - 1);
1150 * Returns a read-only (constant) reference to the data at the
1151 * last element of the %vector.
1154 back() const _GLIBCXX_NOEXCEPT
1156 __glibcxx_requires_nonempty();
1157 return *(end() - 1);
1160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1161 // DR 464. Suggestion for new member functions in standard containers.
1164 * Returns a pointer such that [data(), data() + size()) is a valid
1165 * range. For a non-empty %vector, data() == &front().
1168 data() _GLIBCXX_NOEXCEPT
1169 { return _M_data_ptr(this->_M_impl
._M_start
); }
1172 data() const _GLIBCXX_NOEXCEPT
1173 { return _M_data_ptr(this->_M_impl
._M_start
); }
1175 // [23.2.4.3] modifiers
1177 * @brief Add data to the end of the %vector.
1178 * @param __x Data to be added.
1180 * This is a typical stack operation. The function creates an
1181 * element at the end of the %vector and assigns the given data
1182 * to it. Due to the nature of a %vector this operation can be
1183 * done in constant time if the %vector has preallocated space
1187 push_back(const value_type
& __x
)
1189 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1191 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1192 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1194 ++this->_M_impl
._M_finish
;
1195 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1198 _M_realloc_insert(end(), __x
);
1201 #if __cplusplus >= 201103L
1203 push_back(value_type
&& __x
)
1204 { emplace_back(std::move(__x
)); }
1206 template<typename
... _Args
>
1207 #if __cplusplus > 201402L
1212 emplace_back(_Args
&&... __args
);
1216 * @brief Removes last element.
1218 * This is a typical stack operation. It shrinks the %vector by one.
1220 * Note that no data is returned, and if the last element's
1221 * data is needed, it should be retrieved before pop_back() is
1225 pop_back() _GLIBCXX_NOEXCEPT
1227 __glibcxx_requires_nonempty();
1228 --this->_M_impl
._M_finish
;
1229 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1230 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1233 #if __cplusplus >= 201103L
1235 * @brief Inserts an object in %vector before specified iterator.
1236 * @param __position A const_iterator into the %vector.
1237 * @param __args Arguments.
1238 * @return An iterator that points to the inserted data.
1240 * This function will insert an object of type T constructed
1241 * with T(std::forward<Args>(args)...) before the specified location.
1242 * Note that this kind of operation could be expensive for a %vector
1243 * and if it is frequently used the user should consider using
1246 template<typename
... _Args
>
1248 emplace(const_iterator __position
, _Args
&&... __args
)
1249 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1252 * @brief Inserts given value into %vector before specified iterator.
1253 * @param __position A const_iterator into the %vector.
1254 * @param __x Data to be inserted.
1255 * @return An iterator that points to the inserted data.
1257 * This function will insert a copy of the given value before
1258 * the specified location. Note that this kind of operation
1259 * could be expensive for a %vector and if it is frequently
1260 * used the user should consider using std::list.
1263 insert(const_iterator __position
, const value_type
& __x
);
1266 * @brief Inserts given value into %vector before specified iterator.
1267 * @param __position An iterator into the %vector.
1268 * @param __x Data to be inserted.
1269 * @return An iterator that points to the inserted data.
1271 * This function will insert a copy of the given value before
1272 * the specified location. Note that this kind of operation
1273 * could be expensive for a %vector and if it is frequently
1274 * used the user should consider using std::list.
1277 insert(iterator __position
, const value_type
& __x
);
1280 #if __cplusplus >= 201103L
1282 * @brief Inserts given rvalue into %vector before specified iterator.
1283 * @param __position A const_iterator into the %vector.
1284 * @param __x Data to be inserted.
1285 * @return An iterator that points to the inserted data.
1287 * This function will insert a copy of the given rvalue before
1288 * the specified location. Note that this kind of operation
1289 * could be expensive for a %vector and if it is frequently
1290 * used the user should consider using std::list.
1293 insert(const_iterator __position
, value_type
&& __x
)
1294 { return _M_insert_rval(__position
, std::move(__x
)); }
1297 * @brief Inserts an initializer_list into the %vector.
1298 * @param __position An iterator into the %vector.
1299 * @param __l An initializer_list.
1301 * This function will insert copies of the data in the
1302 * initializer_list @a l into the %vector before the location
1303 * specified by @a position.
1305 * Note that this kind of operation could be expensive for a
1306 * %vector and if it is frequently used the user should
1307 * consider using std::list.
1310 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1312 auto __offset
= __position
- cbegin();
1313 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1314 std::random_access_iterator_tag());
1315 return begin() + __offset
;
1319 #if __cplusplus >= 201103L
1321 * @brief Inserts a number of copies of given data into the %vector.
1322 * @param __position A const_iterator into the %vector.
1323 * @param __n Number of elements to be inserted.
1324 * @param __x Data to be inserted.
1325 * @return An iterator that points to the inserted data.
1327 * This function will insert a specified number of copies of
1328 * the given data before the location specified by @a position.
1330 * Note that this kind of operation could be expensive for a
1331 * %vector and if it is frequently used the user should
1332 * consider using std::list.
1335 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1337 difference_type __offset
= __position
- cbegin();
1338 _M_fill_insert(begin() + __offset
, __n
, __x
);
1339 return begin() + __offset
;
1343 * @brief Inserts a number of copies of given data into the %vector.
1344 * @param __position An iterator into the %vector.
1345 * @param __n Number of elements to be inserted.
1346 * @param __x Data to be inserted.
1348 * This function will insert a specified number of copies of
1349 * the given data before the location specified by @a position.
1351 * Note that this kind of operation could be expensive for a
1352 * %vector and if it is frequently used the user should
1353 * consider using std::list.
1356 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1357 { _M_fill_insert(__position
, __n
, __x
); }
1360 #if __cplusplus >= 201103L
1362 * @brief Inserts a range into the %vector.
1363 * @param __position A const_iterator into the %vector.
1364 * @param __first An input iterator.
1365 * @param __last An input iterator.
1366 * @return An iterator that points to the inserted data.
1368 * This function will insert copies of the data in the range
1369 * [__first,__last) into the %vector before the location specified
1372 * Note that this kind of operation could be expensive for a
1373 * %vector and if it is frequently used the user should
1374 * consider using std::list.
1376 template<typename _InputIterator
,
1377 typename
= std::_RequireInputIter
<_InputIterator
>>
1379 insert(const_iterator __position
, _InputIterator __first
,
1380 _InputIterator __last
)
1382 difference_type __offset
= __position
- cbegin();
1383 _M_insert_dispatch(begin() + __offset
,
1384 __first
, __last
, __false_type());
1385 return begin() + __offset
;
1389 * @brief Inserts a range into the %vector.
1390 * @param __position An iterator into the %vector.
1391 * @param __first An input iterator.
1392 * @param __last An input iterator.
1394 * This function will insert copies of the data in the range
1395 * [__first,__last) into the %vector before the location specified
1398 * Note that this kind of operation could be expensive for a
1399 * %vector and if it is frequently used the user should
1400 * consider using std::list.
1402 template<typename _InputIterator
>
1404 insert(iterator __position
, _InputIterator __first
,
1405 _InputIterator __last
)
1407 // Check whether it's an integral type. If so, it's not an iterator.
1408 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1409 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1414 * @brief Remove element at given position.
1415 * @param __position Iterator pointing to element to be erased.
1416 * @return An iterator pointing to the next element (or end()).
1418 * This function will erase the element at the given position and thus
1419 * shorten the %vector by one.
1421 * Note This operation could be expensive and if it is
1422 * frequently used the user should consider using std::list.
1423 * The user is also cautioned that this function only erases
1424 * the element, and that if the element is itself a pointer,
1425 * the pointed-to memory is not touched in any way. Managing
1426 * the pointer is the user's responsibility.
1429 #if __cplusplus >= 201103L
1430 erase(const_iterator __position
)
1431 { return _M_erase(begin() + (__position
- cbegin())); }
1433 erase(iterator __position
)
1434 { return _M_erase(__position
); }
1438 * @brief Remove a range of elements.
1439 * @param __first Iterator pointing to the first element to be erased.
1440 * @param __last Iterator pointing to one past the last element to be
1442 * @return An iterator pointing to the element pointed to by @a __last
1443 * prior to erasing (or end()).
1445 * This function will erase the elements in the range
1446 * [__first,__last) and shorten the %vector accordingly.
1448 * Note This operation could be expensive and if it is
1449 * frequently used the user should consider using std::list.
1450 * The user is also cautioned that this function only erases
1451 * the elements, and that if the elements themselves are
1452 * pointers, the pointed-to memory is not touched in any way.
1453 * Managing the pointer is the user's responsibility.
1456 #if __cplusplus >= 201103L
1457 erase(const_iterator __first
, const_iterator __last
)
1459 const auto __beg
= begin();
1460 const auto __cbeg
= cbegin();
1461 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1464 erase(iterator __first
, iterator __last
)
1465 { return _M_erase(__first
, __last
); }
1469 * @brief Swaps data with another %vector.
1470 * @param __x A %vector of the same element and allocator types.
1472 * This exchanges the elements between two vectors in constant time.
1473 * (Three pointers, so it should be quite fast.)
1474 * Note that the global std::swap() function is specialized such that
1475 * std::swap(v1,v2) will feed to this function.
1477 * Whether the allocators are swapped depends on the allocator traits.
1480 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1482 #if __cplusplus >= 201103L
1483 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1484 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1486 this->_M_impl
._M_swap_data(__x
._M_impl
);
1487 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1488 __x
._M_get_Tp_allocator());
1492 * Erases all the elements. Note that this function only erases the
1493 * elements, and that if the elements themselves are pointers, the
1494 * pointed-to memory is not touched in any way. Managing the pointer is
1495 * the user's responsibility.
1498 clear() _GLIBCXX_NOEXCEPT
1499 { _M_erase_at_end(this->_M_impl
._M_start
); }
1503 * Memory expansion handler. Uses the member allocation function to
1504 * obtain @a n bytes of memory, and then copies [first,last) into it.
1506 template<typename _ForwardIterator
>
1508 _M_allocate_and_copy(size_type __n
,
1509 _ForwardIterator __first
, _ForwardIterator __last
)
1511 pointer __result
= this->_M_allocate(__n
);
1514 std::__uninitialized_copy_a(__first
, __last
, __result
,
1515 _M_get_Tp_allocator());
1520 _M_deallocate(__result
, __n
);
1521 __throw_exception_again
;
1526 // Internal constructor functions follow.
1528 // Called by the range constructor to implement [23.1.1]/9
1530 #if __cplusplus < 201103L
1531 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1532 // 438. Ambiguity in the "do the right thing" clause
1533 template<typename _Integer
>
1535 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1537 this->_M_impl
._M_start
= _M_allocate(_S_check_init_len(
1538 static_cast<size_type
>(__n
), _M_get_Tp_allocator()));
1539 this->_M_impl
._M_end_of_storage
=
1540 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1541 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1544 // Called by the range constructor to implement [23.1.1]/9
1545 template<typename _InputIterator
>
1547 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1550 _M_range_initialize(__first
, __last
,
1551 std::__iterator_category(__first
));
1555 // Called by the second initialize_dispatch above
1556 template<typename _InputIterator
>
1558 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1559 std::input_iterator_tag
)
1562 for (; __first
!= __last
; ++__first
)
1563 #if __cplusplus >= 201103L
1564 emplace_back(*__first
);
1566 push_back(*__first
);
1570 __throw_exception_again
;
1574 // Called by the second initialize_dispatch above
1575 template<typename _ForwardIterator
>
1577 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1578 std::forward_iterator_tag
)
1580 const size_type __n
= std::distance(__first
, __last
);
1581 this->_M_impl
._M_start
1582 = this->_M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1583 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1584 this->_M_impl
._M_finish
=
1585 std::__uninitialized_copy_a(__first
, __last
,
1586 this->_M_impl
._M_start
,
1587 _M_get_Tp_allocator());
1590 // Called by the first initialize_dispatch above and by the
1591 // vector(n,value,a) constructor.
1593 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1595 this->_M_impl
._M_finish
=
1596 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1597 _M_get_Tp_allocator());
1600 #if __cplusplus >= 201103L
1601 // Called by the vector(n) constructor.
1603 _M_default_initialize(size_type __n
)
1605 this->_M_impl
._M_finish
=
1606 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1607 _M_get_Tp_allocator());
1611 // Internal assign functions follow. The *_aux functions do the actual
1612 // assignment work for the range versions.
1614 // Called by the range assign to implement [23.1.1]/9
1616 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1617 // 438. Ambiguity in the "do the right thing" clause
1618 template<typename _Integer
>
1620 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1621 { _M_fill_assign(__n
, __val
); }
1623 // Called by the range assign to implement [23.1.1]/9
1624 template<typename _InputIterator
>
1626 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1628 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1630 // Called by the second assign_dispatch above
1631 template<typename _InputIterator
>
1633 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1634 std::input_iterator_tag
);
1636 // Called by the second assign_dispatch above
1637 template<typename _ForwardIterator
>
1639 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1640 std::forward_iterator_tag
);
1642 // Called by assign(n,t), and the range assign when it turns out
1643 // to be the same thing.
1645 _M_fill_assign(size_type __n
, const value_type
& __val
);
1647 // Internal insert functions follow.
1649 // Called by the range insert to implement [23.1.1]/9
1651 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1652 // 438. Ambiguity in the "do the right thing" clause
1653 template<typename _Integer
>
1655 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1657 { _M_fill_insert(__pos
, __n
, __val
); }
1659 // Called by the range insert to implement [23.1.1]/9
1660 template<typename _InputIterator
>
1662 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1663 _InputIterator __last
, __false_type
)
1665 _M_range_insert(__pos
, __first
, __last
,
1666 std::__iterator_category(__first
));
1669 // Called by the second insert_dispatch above
1670 template<typename _InputIterator
>
1672 _M_range_insert(iterator __pos
, _InputIterator __first
,
1673 _InputIterator __last
, std::input_iterator_tag
);
1675 // Called by the second insert_dispatch above
1676 template<typename _ForwardIterator
>
1678 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1679 _ForwardIterator __last
, std::forward_iterator_tag
);
1681 // Called by insert(p,n,x), and the range insert when it turns out to be
1684 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1686 #if __cplusplus >= 201103L
1687 // Called by resize(n).
1689 _M_default_append(size_type __n
);
1695 #if __cplusplus < 201103L
1696 // Called by insert(p,x)
1698 _M_insert_aux(iterator __position
, const value_type
& __x
);
1701 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1703 // A value_type object constructed with _Alloc_traits::construct()
1704 // and destroyed with _Alloc_traits::destroy().
1705 struct _Temporary_value
1707 template<typename
... _Args
>
1709 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1711 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1712 std::forward
<_Args
>(__args
)...);
1716 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1719 _M_val() { return *_M_ptr(); }
1723 _M_ptr() { return reinterpret_cast<_Tp
*>(&__buf
); }
1726 typename aligned_storage
<sizeof(_Tp
), alignof(_Tp
)>::type __buf
;
1729 // Called by insert(p,x) and other functions when insertion needs to
1730 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1731 template<typename _Arg
>
1733 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1735 template<typename
... _Args
>
1737 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1739 // Either move-construct at the end, or forward to _M_insert_aux.
1741 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1743 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1744 template<typename
... _Args
>
1746 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1748 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1750 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1751 { return _M_insert_rval(__position
, std::move(__v
)); }
1754 // Called by _M_fill_insert, _M_insert_aux etc.
1756 _M_check_len(size_type __n
, const char* __s
) const
1758 if (max_size() - size() < __n
)
1759 __throw_length_error(__N(__s
));
1761 const size_type __len
= size() + (std::max
)(size(), __n
);
1762 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1765 // Called by constructors to check initial size.
1767 _S_check_init_len(size_type __n
, const allocator_type
& __a
)
1769 if (__n
> _S_max_size(_Tp_alloc_type(__a
)))
1770 __throw_length_error(
1771 __N("cannot create std::vector larger than max_size()"));
1776 _S_max_size(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
1778 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1779 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1780 // (even if std::allocator_traits::max_size says we can).
1781 const size_t __diffmax
1782 = __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
1783 const size_t __allocmax
= _Alloc_traits::max_size(__a
);
1784 return (std::min
)(__diffmax
, __allocmax
);
1787 // Internal erase functions follow.
1789 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1792 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1794 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1796 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1797 _M_get_Tp_allocator());
1798 this->_M_impl
._M_finish
= __pos
;
1799 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1804 _M_erase(iterator __position
);
1807 _M_erase(iterator __first
, iterator __last
);
1809 #if __cplusplus >= 201103L
1811 // Constant-time move assignment when source object's memory can be
1812 // moved, either because the source's allocator will move too
1813 // or because the allocators are equal.
1815 _M_move_assign(vector
&& __x
, true_type
) noexcept
1817 vector
__tmp(get_allocator());
1818 this->_M_impl
._M_swap_data(__x
._M_impl
);
1819 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
1820 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1823 // Do move assignment when it might not be possible to move source
1824 // object's memory, resulting in a linear-time operation.
1826 _M_move_assign(vector
&& __x
, false_type
)
1828 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1829 _M_move_assign(std::move(__x
), true_type());
1832 // The rvalue's allocator cannot be moved and is not equal,
1833 // so we need to individually move each element.
1834 this->_M_assign_aux(std::make_move_iterator(__x
.begin()),
1835 std::make_move_iterator(__x
.end()),
1836 std::random_access_iterator_tag());
1842 template<typename _Up
>
1844 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
1847 #if __cplusplus >= 201103L
1848 template<typename _Ptr
>
1849 typename
std::pointer_traits
<_Ptr
>::element_type
*
1850 _M_data_ptr(_Ptr __ptr
) const
1851 { return empty() ? nullptr : std::__to_address(__ptr
); }
1853 template<typename _Up
>
1855 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
1858 template<typename _Ptr
>
1860 _M_data_ptr(_Ptr __ptr
)
1861 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
1863 template<typename _Ptr
>
1865 _M_data_ptr(_Ptr __ptr
) const
1866 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
1870 #if __cpp_deduction_guides >= 201606
1871 template<typename _InputIterator
, typename _ValT
1872 = typename iterator_traits
<_InputIterator
>::value_type
,
1873 typename _Allocator
= allocator
<_ValT
>,
1874 typename
= _RequireInputIter
<_InputIterator
>,
1875 typename
= _RequireAllocator
<_Allocator
>>
1876 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1877 -> vector
<_ValT
, _Allocator
>;
1881 * @brief Vector equality comparison.
1882 * @param __x A %vector.
1883 * @param __y A %vector of the same type as @a __x.
1884 * @return True iff the size and elements of the vectors are equal.
1886 * This is an equivalence relation. It is linear in the size of the
1887 * vectors. Vectors are considered equivalent if their sizes are equal,
1888 * and if corresponding elements compare equal.
1890 template<typename _Tp
, typename _Alloc
>
1892 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1893 { return (__x
.size() == __y
.size()
1894 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1896 #if __cpp_lib_three_way_comparison
1898 * @brief Vector ordering relation.
1899 * @param __x A `vector`.
1900 * @param __y A `vector` of the same type as `__x`.
1901 * @return A value indicating whether `__x` is less than, equal to,
1902 * greater than, or incomparable with `__y`.
1904 * See `std::lexicographical_compare_three_way()` for how the determination
1905 * is made. This operator is used to synthesize relational operators like
1908 template<typename _Tp
, typename _Alloc
>
1909 inline __detail::__synth3way_t
<_Tp
>
1910 operator<=>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1912 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
1913 __y
.begin(), __y
.end(),
1914 __detail::__synth3way
);
1918 * @brief Vector ordering relation.
1919 * @param __x A %vector.
1920 * @param __y A %vector of the same type as @a __x.
1921 * @return True iff @a __x is lexicographically less than @a __y.
1923 * This is a total ordering relation. It is linear in the size of the
1924 * vectors. The elements must be comparable with @c <.
1926 * See std::lexicographical_compare() for how the determination is made.
1928 template<typename _Tp
, typename _Alloc
>
1930 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1931 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1932 __y
.begin(), __y
.end()); }
1934 /// Based on operator==
1935 template<typename _Tp
, typename _Alloc
>
1937 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1938 { return !(__x
== __y
); }
1940 /// Based on operator<
1941 template<typename _Tp
, typename _Alloc
>
1943 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1944 { return __y
< __x
; }
1946 /// Based on operator<
1947 template<typename _Tp
, typename _Alloc
>
1949 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1950 { return !(__y
< __x
); }
1952 /// Based on operator<
1953 template<typename _Tp
, typename _Alloc
>
1955 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1956 { return !(__x
< __y
); }
1957 #endif // three-way comparison
1959 /// See std::vector::swap().
1960 template<typename _Tp
, typename _Alloc
>
1962 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1963 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1966 _GLIBCXX_END_NAMESPACE_CONTAINER
1968 #if __cplusplus >= 201703L
1969 namespace __detail::__variant
1971 template<typename
> struct _Never_valueless_alt
; // see <variant>
1973 // Provide the strong exception-safety guarantee when emplacing a
1974 // vector into a variant, but only if move assignment cannot throw.
1975 template<typename _Tp
, typename _Alloc
>
1976 struct _Never_valueless_alt
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
1977 : std::is_nothrow_move_assignable
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
1979 } // namespace __detail::__variant
1982 _GLIBCXX_END_NAMESPACE_VERSION
1985 #endif /* _STL_VECTOR_H */