1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2024 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 >= 202002L
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
;
98 _Vector_impl_data() _GLIBCXX_NOEXCEPT
99 : _M_start(), _M_finish(), _M_end_of_storage()
102 #if __cplusplus >= 201103L
104 _Vector_impl_data(_Vector_impl_data
&& __x
) noexcept
105 : _M_start(__x
._M_start
), _M_finish(__x
._M_finish
),
106 _M_end_of_storage(__x
._M_end_of_storage
)
107 { __x
._M_start
= __x
._M_finish
= __x
._M_end_of_storage
= pointer(); }
112 _M_copy_data(_Vector_impl_data
const& __x
) _GLIBCXX_NOEXCEPT
114 _M_start
= __x
._M_start
;
115 _M_finish
= __x
._M_finish
;
116 _M_end_of_storage
= __x
._M_end_of_storage
;
121 _M_swap_data(_Vector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
123 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
124 // information used by TBAA.
125 _Vector_impl_data __tmp
;
126 __tmp
._M_copy_data(*this);
128 __x
._M_copy_data(__tmp
);
133 : public _Tp_alloc_type
, public _Vector_impl_data
136 _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
137 is_nothrow_default_constructible
<_Tp_alloc_type
>::value
)
138 #if __cpp_lib_concepts
139 requires is_default_constructible_v
<_Tp_alloc_type
>
145 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
146 : _Tp_alloc_type(__a
)
149 #if __cplusplus >= 201103L
150 // Not defaulted, to enforce noexcept(true) even when
151 // !is_nothrow_move_constructible<_Tp_alloc_type>.
153 _Vector_impl(_Vector_impl
&& __x
) noexcept
154 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
158 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
159 : _Tp_alloc_type(std::move(__a
))
163 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
164 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
168 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
169 template<typename
= _Tp_alloc_type
>
172 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
173 ::size_type size_type
;
175 static _GLIBCXX20_CONSTEXPR
void
176 _S_shrink(_Vector_impl
&, size_type
) { }
177 static _GLIBCXX20_CONSTEXPR
void
178 _S_on_dealloc(_Vector_impl
&) { }
180 typedef _Vector_impl
& _Reinit
;
184 _GLIBCXX20_CONSTEXPR
_Grow(_Vector_impl
&, size_type
) { }
185 _GLIBCXX20_CONSTEXPR
void _M_grew(size_type
) { }
189 // Enable ASan annotations for memory obtained from std::allocator.
190 template<typename _Up
>
191 struct _Asan
<allocator
<_Up
> >
193 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
194 ::size_type size_type
;
196 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
197 // mark end of valid region as __curr instead of __prev.
198 static _GLIBCXX20_CONSTEXPR
void
199 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
201 #if __cpp_lib_is_constant_evaluated
202 if (std::is_constant_evaluated())
205 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
206 __impl
._M_end_of_storage
, __prev
, __curr
);
209 static _GLIBCXX20_CONSTEXPR
void
210 _S_grow(_Vector_impl
& __impl
, size_type __n
)
211 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
213 static _GLIBCXX20_CONSTEXPR
void
214 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
215 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
217 static _GLIBCXX20_CONSTEXPR
void
218 _S_on_dealloc(_Vector_impl
& __impl
)
221 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
224 // Used on reallocation to tell ASan unused capacity is invalid.
227 explicit _GLIBCXX20_CONSTEXPR
228 _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
230 // Mark unused capacity as valid again before deallocating it.
231 _S_on_dealloc(_M_impl
);
237 // Mark unused capacity as invalid after reallocation.
238 if (_M_impl
._M_start
)
239 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
243 _Vector_impl
& _M_impl
;
245 #if __cplusplus >= 201103L
246 _Reinit(const _Reinit
&) = delete;
247 _Reinit
& operator=(const _Reinit
&) = delete;
251 // Tell ASan when unused capacity is initialized to be valid.
255 _Grow(_Vector_impl
& __impl
, size_type __n
)
256 : _M_impl(__impl
), _M_n(__n
)
257 { _S_grow(_M_impl
, __n
); }
260 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
263 void _M_grew(size_type __n
) { _M_n
-= __n
; }
265 #if __cplusplus >= 201103L
266 _Grow(const _Grow
&) = delete;
267 _Grow
& operator=(const _Grow
&) = delete;
270 _Vector_impl
& _M_impl
;
275 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
276 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
277 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
278 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
279 typename _Base::_Vector_impl::template _Asan<>::_Grow \
280 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
281 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
282 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
283 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
284 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
285 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
286 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
287 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
288 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
289 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
290 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
291 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
292 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
296 typedef _Alloc allocator_type
;
300 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
301 { return this->_M_impl
; }
304 const _Tp_alloc_type
&
305 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
306 { return this->_M_impl
; }
310 get_allocator() const _GLIBCXX_NOEXCEPT
311 { return allocator_type(_M_get_Tp_allocator()); }
313 #if __cplusplus >= 201103L
314 _Vector_base() = default;
320 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
323 // Kept for ABI compatibility.
324 #if !_GLIBCXX_INLINE_VERSION
326 _Vector_base(size_t __n
)
328 { _M_create_storage(__n
); }
332 _Vector_base(size_t __n
, const allocator_type
& __a
)
334 { _M_create_storage(__n
); }
336 #if __cplusplus >= 201103L
337 _Vector_base(_Vector_base
&&) = default;
339 // Kept for ABI compatibility.
340 # if !_GLIBCXX_INLINE_VERSION
342 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
343 : _M_impl(std::move(__a
)) { }
346 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
349 if (__x
.get_allocator() == __a
)
350 this->_M_impl
._M_swap_data(__x
._M_impl
);
353 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
354 _M_create_storage(__n
);
360 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
361 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
366 ~_Vector_base() _GLIBCXX_NOEXCEPT
368 _M_deallocate(_M_impl
._M_start
,
369 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
373 _Vector_impl _M_impl
;
377 _M_allocate(size_t __n
)
379 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
380 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
385 _M_deallocate(pointer __p
, size_t __n
)
387 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
389 _Tr::deallocate(_M_impl
, __p
, __n
);
396 _M_create_storage(size_t __n
)
398 this->_M_impl
._M_start
= this->_M_allocate(__n
);
399 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
400 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
405 * @brief A standard container which offers fixed time access to
406 * individual elements in any order.
412 * @tparam _Tp Type of element.
413 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
415 * Meets the requirements of a <a href="tables.html#65">container</a>, a
416 * <a href="tables.html#66">reversible container</a>, and a
417 * <a href="tables.html#67">sequence</a>, including the
418 * <a href="tables.html#68">optional sequence requirements</a> with the
419 * %exception of @c push_front and @c pop_front.
421 * In some terminology a %vector can be described as a dynamic
422 * C-style array, it offers fast and efficient access to individual
423 * elements in any order and saves the user from worrying about
424 * memory and size allocation. Subscripting ( @c [] ) access is
425 * also provided as with C-style arrays.
427 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
428 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
430 #ifdef _GLIBCXX_CONCEPT_CHECKS
431 // Concept requirements.
432 typedef typename
_Alloc::value_type _Alloc_value_type
;
433 # if __cplusplus < 201103L
434 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
436 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
439 #if __cplusplus >= 201103L
440 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
441 "std::vector must have a non-const, non-volatile value_type");
442 # if __cplusplus > 201703L || defined __STRICT_ANSI__
443 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
444 "std::vector must have the same value_type as its allocator");
448 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
449 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
450 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
453 typedef _Tp value_type
;
454 typedef typename
_Base::pointer pointer
;
455 typedef typename
_Alloc_traits::const_pointer const_pointer
;
456 typedef typename
_Alloc_traits::reference reference
;
457 typedef typename
_Alloc_traits::const_reference const_reference
;
458 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
459 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
461 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
462 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
463 typedef size_t size_type
;
464 typedef ptrdiff_t difference_type
;
465 typedef _Alloc allocator_type
;
468 #if __cplusplus >= 201103L
469 static constexpr bool
470 _S_nothrow_relocate(true_type
)
472 return noexcept(std::__relocate_a(std::declval
<pointer
>(),
473 std::declval
<pointer
>(),
474 std::declval
<pointer
>(),
475 std::declval
<_Tp_alloc_type
&>()));
478 static constexpr bool
479 _S_nothrow_relocate(false_type
)
482 static constexpr bool
485 // Instantiating std::__relocate_a might cause an error outside the
486 // immediate context (in __relocate_object_a's noexcept-specifier),
487 // so only do it if we know the type can be move-inserted into *this.
488 return _S_nothrow_relocate(__is_move_insertable
<_Tp_alloc_type
>{});
492 _S_do_relocate(pointer __first
, pointer __last
, pointer __result
,
493 _Tp_alloc_type
& __alloc
, true_type
) noexcept
495 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
499 _S_do_relocate(pointer
, pointer
, pointer __result
,
500 _Tp_alloc_type
&, false_type
) noexcept
503 static _GLIBCXX20_CONSTEXPR pointer
504 _S_relocate(pointer __first
, pointer __last
, pointer __result
,
505 _Tp_alloc_type
& __alloc
) noexcept
507 #if __cpp_if_constexpr
508 // All callers have already checked _S_use_relocate() so just do it.
509 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
511 using __do_it
= __bool_constant
<_S_use_relocate()>;
512 return _S_do_relocate(__first
, __last
, __result
, __alloc
, __do_it
{});
518 using _Base::_M_allocate
;
519 using _Base::_M_deallocate
;
520 using _Base::_M_impl
;
521 using _Base::_M_get_Tp_allocator
;
524 // [23.2.4.1] construct/copy/destroy
525 // (assign() and get_allocator() are also listed in this section)
528 * @brief Creates a %vector with no elements.
530 #if __cplusplus >= 201103L
537 * @brief Creates a %vector with no elements.
538 * @param __a An allocator object.
542 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
545 #if __cplusplus >= 201103L
547 * @brief Creates a %vector with default constructed elements.
548 * @param __n The number of elements to initially create.
549 * @param __a An allocator.
551 * This constructor fills the %vector with @a __n default
552 * constructed elements.
556 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
557 : _Base(_S_check_init_len(__n
, __a
), __a
)
558 { _M_default_initialize(__n
); }
561 * @brief Creates a %vector with copies of an exemplar element.
562 * @param __n The number of elements to initially create.
563 * @param __value An element to copy.
564 * @param __a An allocator.
566 * This constructor fills the %vector with @a __n copies of @a __value.
569 vector(size_type __n
, const value_type
& __value
,
570 const allocator_type
& __a
= allocator_type())
571 : _Base(_S_check_init_len(__n
, __a
), __a
)
572 { _M_fill_initialize(__n
, __value
); }
575 * @brief Creates a %vector with copies of an exemplar element.
576 * @param __n The number of elements to initially create.
577 * @param __value An element to copy.
578 * @param __a An allocator.
580 * This constructor fills the %vector with @a __n copies of @a __value.
583 vector(size_type __n
, const value_type
& __value
= value_type(),
584 const allocator_type
& __a
= allocator_type())
585 : _Base(_S_check_init_len(__n
, __a
), __a
)
586 { _M_fill_initialize(__n
, __value
); }
590 * @brief %Vector copy constructor.
591 * @param __x A %vector of identical element and allocator types.
593 * All the elements of @a __x are copied, but any unused capacity in
594 * @a __x will not be copied
595 * (i.e. capacity() == size() in the new %vector).
597 * The newly-created %vector uses a copy of the allocator object used
598 * by @a __x (unless the allocator traits dictate a different object).
601 vector(const vector
& __x
)
603 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
605 this->_M_impl
._M_finish
=
606 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
607 this->_M_impl
._M_start
,
608 _M_get_Tp_allocator());
611 #if __cplusplus >= 201103L
613 * @brief %Vector move constructor.
615 * The newly-created %vector contains the exact contents of the
617 * The contents of the moved instance are a valid, but unspecified
620 vector(vector
&&) noexcept
= default;
622 /// Copy constructor with alternative allocator
624 vector(const vector
& __x
, const __type_identity_t
<allocator_type
>& __a
)
625 : _Base(__x
.size(), __a
)
627 this->_M_impl
._M_finish
=
628 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
629 this->_M_impl
._M_start
,
630 _M_get_Tp_allocator());
635 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
636 : _Base(__m
, std::move(__rv
))
640 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
643 if (__rv
.get_allocator() == __m
)
644 this->_M_impl
._M_swap_data(__rv
._M_impl
);
645 else if (!__rv
.empty())
647 this->_M_create_storage(__rv
.size());
648 this->_M_impl
._M_finish
=
649 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
650 this->_M_impl
._M_start
,
651 _M_get_Tp_allocator());
657 /// Move constructor with alternative allocator
659 vector(vector
&& __rv
, const __type_identity_t
<allocator_type
>& __m
)
661 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
662 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
663 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
667 * @brief Builds a %vector from an initializer list.
668 * @param __l An initializer_list.
669 * @param __a An allocator.
671 * Create a %vector consisting of copies of the elements in the
672 * initializer_list @a __l.
674 * This will call the element type's copy constructor N times
675 * (where N is @a __l.size()) and do no memory reallocation.
678 vector(initializer_list
<value_type
> __l
,
679 const allocator_type
& __a
= allocator_type())
682 _M_range_initialize(__l
.begin(), __l
.end(),
683 random_access_iterator_tag());
688 * @brief Builds a %vector from a range.
689 * @param __first An input iterator.
690 * @param __last An input iterator.
691 * @param __a An allocator.
693 * Create a %vector consisting of copies of the elements from
696 * If the iterators are forward, bidirectional, or
697 * random-access, then this will call the elements' copy
698 * constructor N times (where N is distance(first,last)) and do
699 * no memory reallocation. But if only input iterators are
700 * used, then this will do at most 2N calls to the copy
701 * constructor, and logN memory reallocations.
703 #if __cplusplus >= 201103L
704 template<typename _InputIterator
,
705 typename
= std::_RequireInputIter
<_InputIterator
>>
707 vector(_InputIterator __first
, _InputIterator __last
,
708 const allocator_type
& __a
= allocator_type())
711 _M_range_initialize(__first
, __last
,
712 std::__iterator_category(__first
));
715 template<typename _InputIterator
>
716 vector(_InputIterator __first
, _InputIterator __last
,
717 const allocator_type
& __a
= allocator_type())
720 // Check whether it's an integral type. If so, it's not an iterator.
721 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
722 _M_initialize_dispatch(__first
, __last
, _Integral());
727 * The dtor only erases the elements, and note that if the
728 * elements themselves are pointers, the pointed-to memory is
729 * not touched in any way. Managing the pointer is the user's
733 ~vector() _GLIBCXX_NOEXCEPT
735 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
736 _M_get_Tp_allocator());
737 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
741 * @brief %Vector assignment operator.
742 * @param __x A %vector of identical element and allocator types.
744 * All the elements of @a __x are copied, but any unused capacity in
745 * @a __x will not be copied.
747 * Whether the allocator is copied depends on the allocator traits.
751 operator=(const vector
& __x
);
753 #if __cplusplus >= 201103L
755 * @brief %Vector move assignment operator.
756 * @param __x A %vector of identical element and allocator types.
758 * The contents of @a __x are moved into this %vector (without copying,
759 * if the allocators permit it).
760 * Afterwards @a __x is a valid, but unspecified %vector.
762 * Whether the allocator is moved depends on the allocator traits.
766 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
768 constexpr bool __move_storage
=
769 _Alloc_traits::_S_propagate_on_move_assign()
770 || _Alloc_traits::_S_always_equal();
771 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
776 * @brief %Vector list assignment operator.
777 * @param __l An initializer_list.
779 * This function fills a %vector with copies of the elements in the
780 * initializer list @a __l.
782 * Note that the assignment completely changes the %vector and
783 * that the resulting %vector's size is the same as the number
784 * of elements assigned.
788 operator=(initializer_list
<value_type
> __l
)
790 this->_M_assign_aux(__l
.begin(), __l
.end(),
791 random_access_iterator_tag());
797 * @brief Assigns a given value to a %vector.
798 * @param __n Number of elements to be assigned.
799 * @param __val Value to be assigned.
801 * This function fills a %vector with @a __n copies of the given
802 * value. Note that the assignment completely changes the
803 * %vector and that the resulting %vector's size is the same as
804 * the number of elements assigned.
808 assign(size_type __n
, const value_type
& __val
)
809 { _M_fill_assign(__n
, __val
); }
812 * @brief Assigns a range to a %vector.
813 * @param __first An input iterator.
814 * @param __last An input iterator.
816 * This function fills a %vector with copies of the elements in the
817 * range [__first,__last).
819 * Note that the assignment completely changes the %vector and
820 * that the resulting %vector's size is the same as the number
821 * of elements assigned.
823 #if __cplusplus >= 201103L
824 template<typename _InputIterator
,
825 typename
= std::_RequireInputIter
<_InputIterator
>>
828 assign(_InputIterator __first
, _InputIterator __last
)
829 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
831 template<typename _InputIterator
>
833 assign(_InputIterator __first
, _InputIterator __last
)
835 // Check whether it's an integral type. If so, it's not an iterator.
836 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
837 _M_assign_dispatch(__first
, __last
, _Integral());
841 #if __cplusplus >= 201103L
843 * @brief Assigns an initializer list to a %vector.
844 * @param __l An initializer_list.
846 * This function fills a %vector with copies of the elements in the
847 * initializer list @a __l.
849 * Note that the assignment completely changes the %vector and
850 * that the resulting %vector's size is the same as the number
851 * of elements assigned.
855 assign(initializer_list
<value_type
> __l
)
857 this->_M_assign_aux(__l
.begin(), __l
.end(),
858 random_access_iterator_tag());
862 /// Get a copy of the memory allocation object.
863 using _Base::get_allocator
;
867 * Returns a read/write iterator that points to the first
868 * element in the %vector. Iteration is done in ordinary
871 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
873 begin() _GLIBCXX_NOEXCEPT
874 { return iterator(this->_M_impl
._M_start
); }
877 * Returns a read-only (constant) iterator that points to the
878 * first element in the %vector. Iteration is done in ordinary
881 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
883 begin() const _GLIBCXX_NOEXCEPT
884 { return const_iterator(this->_M_impl
._M_start
); }
887 * Returns a read/write iterator that points one past the last
888 * element in the %vector. Iteration is done in ordinary
891 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
893 end() _GLIBCXX_NOEXCEPT
894 { return iterator(this->_M_impl
._M_finish
); }
897 * Returns a read-only (constant) iterator that points one past
898 * the last element in the %vector. Iteration is done in
899 * ordinary element order.
901 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
903 end() const _GLIBCXX_NOEXCEPT
904 { return const_iterator(this->_M_impl
._M_finish
); }
907 * Returns a read/write reverse iterator that points to the
908 * last element in the %vector. Iteration is done in reverse
911 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
913 rbegin() _GLIBCXX_NOEXCEPT
914 { return reverse_iterator(end()); }
917 * Returns a read-only (constant) reverse iterator that points
918 * to the last element in the %vector. Iteration is done in
919 * reverse element order.
921 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
922 const_reverse_iterator
923 rbegin() const _GLIBCXX_NOEXCEPT
924 { return const_reverse_iterator(end()); }
927 * Returns a read/write reverse iterator that points to one
928 * before the first element in the %vector. Iteration is done
929 * in reverse element order.
931 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
933 rend() _GLIBCXX_NOEXCEPT
934 { return reverse_iterator(begin()); }
937 * Returns a read-only (constant) reverse iterator that points
938 * to one before the first element in the %vector. Iteration
939 * is done in reverse element order.
941 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
942 const_reverse_iterator
943 rend() const _GLIBCXX_NOEXCEPT
944 { return const_reverse_iterator(begin()); }
946 #if __cplusplus >= 201103L
948 * Returns a read-only (constant) iterator that points to the
949 * first element in the %vector. Iteration is done in ordinary
952 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
954 cbegin() const noexcept
955 { return const_iterator(this->_M_impl
._M_start
); }
958 * Returns a read-only (constant) iterator that points one past
959 * the last element in the %vector. Iteration is done in
960 * ordinary element order.
962 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
964 cend() const noexcept
965 { return const_iterator(this->_M_impl
._M_finish
); }
968 * Returns a read-only (constant) reverse iterator that points
969 * to the last element in the %vector. Iteration is done in
970 * reverse element order.
972 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
973 const_reverse_iterator
974 crbegin() const noexcept
975 { return const_reverse_iterator(end()); }
978 * Returns a read-only (constant) reverse iterator that points
979 * to one before the first element in the %vector. Iteration
980 * is done in reverse element order.
982 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
983 const_reverse_iterator
984 crend() const noexcept
985 { return const_reverse_iterator(begin()); }
988 // [23.2.4.2] capacity
989 /** Returns the number of elements in the %vector. */
990 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
992 size() const _GLIBCXX_NOEXCEPT
993 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
995 /** Returns the size() of the largest possible %vector. */
996 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
998 max_size() const _GLIBCXX_NOEXCEPT
999 { return _S_max_size(_M_get_Tp_allocator()); }
1001 #if __cplusplus >= 201103L
1003 * @brief Resizes the %vector to the specified number of elements.
1004 * @param __new_size Number of elements the %vector should contain.
1006 * This function will %resize the %vector to the specified
1007 * number of elements. If the number is smaller than the
1008 * %vector's current size the %vector is truncated, otherwise
1009 * default constructed elements are appended.
1011 _GLIBCXX20_CONSTEXPR
1013 resize(size_type __new_size
)
1015 if (__new_size
> size())
1016 _M_default_append(__new_size
- size());
1017 else if (__new_size
< size())
1018 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1022 * @brief Resizes the %vector to the specified number of elements.
1023 * @param __new_size Number of elements the %vector should contain.
1024 * @param __x Data with which new elements should be populated.
1026 * This function will %resize the %vector to the specified
1027 * number of elements. If the number is smaller than the
1028 * %vector's current size the %vector is truncated, otherwise
1029 * the %vector is extended and new elements are populated with
1032 _GLIBCXX20_CONSTEXPR
1034 resize(size_type __new_size
, const value_type
& __x
)
1036 if (__new_size
> size())
1037 _M_fill_insert(end(), __new_size
- size(), __x
);
1038 else if (__new_size
< size())
1039 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1043 * @brief Resizes the %vector to the specified number of elements.
1044 * @param __new_size Number of elements the %vector should contain.
1045 * @param __x Data with which new elements should be populated.
1047 * This function will %resize the %vector to the specified
1048 * number of elements. If the number is smaller than the
1049 * %vector's current size the %vector is truncated, otherwise
1050 * the %vector is extended and new elements are populated with
1053 _GLIBCXX20_CONSTEXPR
1055 resize(size_type __new_size
, value_type __x
= value_type())
1057 if (__new_size
> size())
1058 _M_fill_insert(end(), __new_size
- size(), __x
);
1059 else if (__new_size
< size())
1060 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1064 #if __cplusplus >= 201103L
1065 /** A non-binding request to reduce capacity() to size(). */
1066 _GLIBCXX20_CONSTEXPR
1069 { _M_shrink_to_fit(); }
1073 * Returns the total number of elements that the %vector can
1074 * hold before needing to allocate more memory.
1076 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1078 capacity() const _GLIBCXX_NOEXCEPT
1080 return size_type(this->_M_impl
._M_end_of_storage
1081 - this->_M_impl
._M_start
);
1085 * Returns true if the %vector is empty. (Thus begin() would
1088 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1090 empty() const _GLIBCXX_NOEXCEPT
1091 { return begin() == end(); }
1094 * @brief Attempt to preallocate enough memory for specified number of
1096 * @param __n Number of elements required.
1097 * @throw std::length_error If @a n exceeds @c max_size().
1099 * This function attempts to reserve enough memory for the
1100 * %vector to hold the specified number of elements. If the
1101 * number requested is more than max_size(), length_error is
1104 * The advantage of this function is that if optimal code is a
1105 * necessity and the user can determine the number of elements
1106 * that will be required, the user can reserve the memory in
1107 * %advance, and thus prevent a possible reallocation of memory
1108 * and copying of %vector data.
1110 _GLIBCXX20_CONSTEXPR
1112 reserve(size_type __n
);
1116 * @brief Subscript access to the data contained in the %vector.
1117 * @param __n The index of the element for which data should be
1119 * @return Read/write reference to data.
1121 * This operator allows for easy, array-style, data access.
1122 * Note that data access with this operator is unchecked and
1123 * out_of_range lookups are not defined. (For checked lookups
1126 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1128 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1130 __glibcxx_requires_subscript(__n
);
1131 return *(this->_M_impl
._M_start
+ __n
);
1135 * @brief Subscript access to the data contained in the %vector.
1136 * @param __n The index of the element for which data should be
1138 * @return Read-only (constant) reference to data.
1140 * This operator allows for easy, array-style, data access.
1141 * Note that data access with this operator is unchecked and
1142 * out_of_range lookups are not defined. (For checked lookups
1145 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1147 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1149 __glibcxx_requires_subscript(__n
);
1150 return *(this->_M_impl
._M_start
+ __n
);
1154 /// Safety check used only from at().
1155 _GLIBCXX20_CONSTEXPR
1157 _M_range_check(size_type __n
) const
1159 if (__n
>= this->size())
1160 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1161 "(which is %zu) >= this->size() "
1168 * @brief Provides access to the data contained in the %vector.
1169 * @param __n The index of the element for which data should be
1171 * @return Read/write reference to data.
1172 * @throw std::out_of_range If @a __n is an invalid index.
1174 * This function provides for safer data access. The parameter
1175 * is first checked that it is in the range of the vector. The
1176 * function throws out_of_range if the check fails.
1178 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1182 _M_range_check(__n
);
1183 return (*this)[__n
];
1187 * @brief Provides access to the data contained in the %vector.
1188 * @param __n The index of the element for which data should be
1190 * @return Read-only (constant) reference to data.
1191 * @throw std::out_of_range If @a __n is an invalid index.
1193 * This function provides for safer data access. The parameter
1194 * is first checked that it is in the range of the vector. The
1195 * function throws out_of_range if the check fails.
1197 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1199 at(size_type __n
) const
1201 _M_range_check(__n
);
1202 return (*this)[__n
];
1206 * Returns a read/write reference to the data at the first
1207 * element of the %vector.
1209 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1211 front() _GLIBCXX_NOEXCEPT
1213 __glibcxx_requires_nonempty();
1218 * Returns a read-only (constant) reference to the data at the first
1219 * element of the %vector.
1221 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1223 front() const _GLIBCXX_NOEXCEPT
1225 __glibcxx_requires_nonempty();
1230 * Returns a read/write reference to the data at the last
1231 * element of the %vector.
1233 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1235 back() _GLIBCXX_NOEXCEPT
1237 __glibcxx_requires_nonempty();
1238 return *(end() - 1);
1242 * Returns a read-only (constant) reference to the data at the
1243 * last element of the %vector.
1245 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1247 back() const _GLIBCXX_NOEXCEPT
1249 __glibcxx_requires_nonempty();
1250 return *(end() - 1);
1253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1254 // DR 464. Suggestion for new member functions in standard containers.
1257 * Returns a pointer such that [data(), data() + size()) is a valid
1258 * range. For a non-empty %vector, data() == &front().
1260 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1262 data() _GLIBCXX_NOEXCEPT
1263 { return _M_data_ptr(this->_M_impl
._M_start
); }
1265 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1267 data() const _GLIBCXX_NOEXCEPT
1268 { return _M_data_ptr(this->_M_impl
._M_start
); }
1270 // [23.2.4.3] modifiers
1272 * @brief Add data to the end of the %vector.
1273 * @param __x Data to be added.
1275 * This is a typical stack operation. The function creates an
1276 * element at the end of the %vector and assigns the given data
1277 * to it. Due to the nature of a %vector this operation can be
1278 * done in constant time if the %vector has preallocated space
1281 _GLIBCXX20_CONSTEXPR
1283 push_back(const value_type
& __x
)
1285 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1287 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1288 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1290 ++this->_M_impl
._M_finish
;
1291 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1294 _M_realloc_append(__x
);
1297 #if __cplusplus >= 201103L
1298 _GLIBCXX20_CONSTEXPR
1300 push_back(value_type
&& __x
)
1301 { emplace_back(std::move(__x
)); }
1303 template<typename
... _Args
>
1304 #if __cplusplus > 201402L
1305 _GLIBCXX20_CONSTEXPR
1310 emplace_back(_Args
&&... __args
);
1314 * @brief Removes last element.
1316 * This is a typical stack operation. It shrinks the %vector by one.
1318 * Note that no data is returned, and if the last element's
1319 * data is needed, it should be retrieved before pop_back() is
1322 _GLIBCXX20_CONSTEXPR
1324 pop_back() _GLIBCXX_NOEXCEPT
1326 __glibcxx_requires_nonempty();
1327 --this->_M_impl
._M_finish
;
1328 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1329 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1332 #if __cplusplus >= 201103L
1334 * @brief Inserts an object in %vector before specified iterator.
1335 * @param __position A const_iterator into the %vector.
1336 * @param __args Arguments.
1337 * @return An iterator that points to the inserted data.
1339 * This function will insert an object of type T constructed
1340 * with T(std::forward<Args>(args)...) before the specified location.
1341 * Note that this kind of operation could be expensive for a %vector
1342 * and if it is frequently used the user should consider using
1345 template<typename
... _Args
>
1346 _GLIBCXX20_CONSTEXPR
1348 emplace(const_iterator __position
, _Args
&&... __args
)
1349 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1352 * @brief Inserts given value into %vector before specified iterator.
1353 * @param __position A const_iterator into the %vector.
1354 * @param __x Data to be inserted.
1355 * @return An iterator that points to the inserted data.
1357 * This function will insert a copy of the given value before
1358 * the specified location. Note that this kind of operation
1359 * could be expensive for a %vector and if it is frequently
1360 * used the user should consider using std::list.
1362 _GLIBCXX20_CONSTEXPR
1364 insert(const_iterator __position
, const value_type
& __x
);
1367 * @brief Inserts given value into %vector before specified iterator.
1368 * @param __position An iterator into the %vector.
1369 * @param __x Data to be inserted.
1370 * @return An iterator that points to the inserted data.
1372 * This function will insert a copy of the given value before
1373 * the specified location. Note that this kind of operation
1374 * could be expensive for a %vector and if it is frequently
1375 * used the user should consider using std::list.
1378 insert(iterator __position
, const value_type
& __x
);
1381 #if __cplusplus >= 201103L
1383 * @brief Inserts given rvalue into %vector before specified iterator.
1384 * @param __position A const_iterator into the %vector.
1385 * @param __x Data to be inserted.
1386 * @return An iterator that points to the inserted data.
1388 * This function will insert a copy of the given rvalue before
1389 * the specified location. Note that this kind of operation
1390 * could be expensive for a %vector and if it is frequently
1391 * used the user should consider using std::list.
1393 _GLIBCXX20_CONSTEXPR
1395 insert(const_iterator __position
, value_type
&& __x
)
1396 { return _M_insert_rval(__position
, std::move(__x
)); }
1399 * @brief Inserts an initializer_list into the %vector.
1400 * @param __position An iterator into the %vector.
1401 * @param __l An initializer_list.
1403 * This function will insert copies of the data in the
1404 * initializer_list @a l into the %vector before the location
1405 * specified by @a position.
1407 * Note that this kind of operation could be expensive for a
1408 * %vector and if it is frequently used the user should
1409 * consider using std::list.
1411 _GLIBCXX20_CONSTEXPR
1413 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1415 auto __offset
= __position
- cbegin();
1416 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1417 std::random_access_iterator_tag());
1418 return begin() + __offset
;
1422 #if __cplusplus >= 201103L
1424 * @brief Inserts a number of copies of given data into the %vector.
1425 * @param __position A const_iterator into the %vector.
1426 * @param __n Number of elements to be inserted.
1427 * @param __x Data to be inserted.
1428 * @return An iterator that points to the inserted data.
1430 * This function will insert a specified number of copies of
1431 * the given data before the location specified by @a position.
1433 * Note that this kind of operation could be expensive for a
1434 * %vector and if it is frequently used the user should
1435 * consider using std::list.
1437 _GLIBCXX20_CONSTEXPR
1439 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1441 difference_type __offset
= __position
- cbegin();
1442 _M_fill_insert(begin() + __offset
, __n
, __x
);
1443 return begin() + __offset
;
1447 * @brief Inserts a number of copies of given data into the %vector.
1448 * @param __position An iterator into the %vector.
1449 * @param __n Number of elements to be inserted.
1450 * @param __x Data to be inserted.
1452 * This function will insert a specified number of copies of
1453 * the given data before the location specified by @a position.
1455 * Note that this kind of operation could be expensive for a
1456 * %vector and if it is frequently used the user should
1457 * consider using std::list.
1460 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1461 { _M_fill_insert(__position
, __n
, __x
); }
1464 #if __cplusplus >= 201103L
1466 * @brief Inserts a range into the %vector.
1467 * @param __position A const_iterator into the %vector.
1468 * @param __first An input iterator.
1469 * @param __last An input iterator.
1470 * @return An iterator that points to the inserted data.
1472 * This function will insert copies of the data in the range
1473 * [__first,__last) into the %vector before the location specified
1476 * Note that this kind of operation could be expensive for a
1477 * %vector and if it is frequently used the user should
1478 * consider using std::list.
1480 template<typename _InputIterator
,
1481 typename
= std::_RequireInputIter
<_InputIterator
>>
1482 _GLIBCXX20_CONSTEXPR
1484 insert(const_iterator __position
, _InputIterator __first
,
1485 _InputIterator __last
)
1487 difference_type __offset
= __position
- cbegin();
1488 _M_range_insert(begin() + __offset
, __first
, __last
,
1489 std::__iterator_category(__first
));
1490 return begin() + __offset
;
1494 * @brief Inserts a range into the %vector.
1495 * @param __position An iterator into the %vector.
1496 * @param __first An input iterator.
1497 * @param __last An input iterator.
1499 * This function will insert copies of the data in the range
1500 * [__first,__last) into the %vector before the location specified
1503 * Note that this kind of operation could be expensive for a
1504 * %vector and if it is frequently used the user should
1505 * consider using std::list.
1507 template<typename _InputIterator
>
1509 insert(iterator __position
, _InputIterator __first
,
1510 _InputIterator __last
)
1512 // Check whether it's an integral type. If so, it's not an iterator.
1513 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1514 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1519 * @brief Remove element at given position.
1520 * @param __position Iterator pointing to element to be erased.
1521 * @return An iterator pointing to the next element (or end()).
1523 * This function will erase the element at the given position and thus
1524 * shorten the %vector by one.
1526 * Note This operation could be expensive and if it is
1527 * frequently used the user should consider using std::list.
1528 * The user is also cautioned that this function only erases
1529 * the element, and that if the element is itself a pointer,
1530 * the pointed-to memory is not touched in any way. Managing
1531 * the pointer is the user's responsibility.
1533 _GLIBCXX20_CONSTEXPR
1535 #if __cplusplus >= 201103L
1536 erase(const_iterator __position
)
1537 { return _M_erase(begin() + (__position
- cbegin())); }
1539 erase(iterator __position
)
1540 { return _M_erase(__position
); }
1544 * @brief Remove a range of elements.
1545 * @param __first Iterator pointing to the first element to be erased.
1546 * @param __last Iterator pointing to one past the last element to be
1548 * @return An iterator pointing to the element pointed to by @a __last
1549 * prior to erasing (or end()).
1551 * This function will erase the elements in the range
1552 * [__first,__last) and shorten the %vector accordingly.
1554 * Note This operation could be expensive and if it is
1555 * frequently used the user should consider using std::list.
1556 * The user is also cautioned that this function only erases
1557 * the elements, and that if the elements themselves are
1558 * pointers, the pointed-to memory is not touched in any way.
1559 * Managing the pointer is the user's responsibility.
1561 _GLIBCXX20_CONSTEXPR
1563 #if __cplusplus >= 201103L
1564 erase(const_iterator __first
, const_iterator __last
)
1566 const auto __beg
= begin();
1567 const auto __cbeg
= cbegin();
1568 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1571 erase(iterator __first
, iterator __last
)
1572 { return _M_erase(__first
, __last
); }
1576 * @brief Swaps data with another %vector.
1577 * @param __x A %vector of the same element and allocator types.
1579 * This exchanges the elements between two vectors in constant time.
1580 * (Three pointers, so it should be quite fast.)
1581 * Note that the global std::swap() function is specialized such that
1582 * std::swap(v1,v2) will feed to this function.
1584 * Whether the allocators are swapped depends on the allocator traits.
1586 _GLIBCXX20_CONSTEXPR
1588 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1590 #if __cplusplus >= 201103L
1591 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1592 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1594 this->_M_impl
._M_swap_data(__x
._M_impl
);
1595 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1596 __x
._M_get_Tp_allocator());
1600 * Erases all the elements. Note that this function only erases the
1601 * elements, and that if the elements themselves are pointers, the
1602 * pointed-to memory is not touched in any way. Managing the pointer is
1603 * the user's responsibility.
1605 _GLIBCXX20_CONSTEXPR
1607 clear() _GLIBCXX_NOEXCEPT
1608 { _M_erase_at_end(this->_M_impl
._M_start
); }
1611 // RAII guard for allocated storage.
1614 pointer _M_storage
; // Storage to deallocate
1618 _GLIBCXX20_CONSTEXPR
1619 _Guard_alloc(pointer __s
, size_type __l
, _Base
& __vect
)
1620 : _M_storage(__s
), _M_len(__l
), _M_vect(__vect
)
1623 _GLIBCXX20_CONSTEXPR
1627 _M_vect
._M_deallocate(_M_storage
, _M_len
);
1630 _GLIBCXX20_CONSTEXPR
1634 pointer __res
= _M_storage
;
1635 _M_storage
= pointer();
1640 _Guard_alloc(const _Guard_alloc
&);
1645 * Memory expansion handler. Uses the member allocation function to
1646 * obtain @a n bytes of memory, and then copies [first,last) into it.
1648 template<typename _ForwardIterator
>
1649 _GLIBCXX20_CONSTEXPR
1651 _M_allocate_and_copy(size_type __n
,
1652 _ForwardIterator __first
, _ForwardIterator __last
)
1654 _Guard_alloc
__guard(this->_M_allocate(__n
), __n
, *this);
1655 std::__uninitialized_copy_a
1656 (__first
, __last
, __guard
._M_storage
, _M_get_Tp_allocator());
1657 return __guard
._M_release();
1661 // Internal constructor functions follow.
1663 // Called by the range constructor to implement [23.1.1]/9
1665 #if __cplusplus < 201103L
1666 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1667 // 438. Ambiguity in the "do the right thing" clause
1668 template<typename _Integer
>
1670 _M_initialize_dispatch(_Integer __int_n
, _Integer __value
, __true_type
)
1672 const size_type __n
= static_cast<size_type
>(__int_n
);
1674 _M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1675 this->_M_impl
._M_start
= __start
;
1676 this->_M_impl
._M_end_of_storage
= __start
+ __n
;
1677 _M_fill_initialize(__n
, __value
);
1680 // Called by the range constructor to implement [23.1.1]/9
1681 template<typename _InputIterator
>
1683 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1686 _M_range_initialize(__first
, __last
,
1687 std::__iterator_category(__first
));
1691 // Called by the second initialize_dispatch above
1692 template<typename _InputIterator
>
1693 _GLIBCXX20_CONSTEXPR
1695 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1696 std::input_iterator_tag
)
1699 for (; __first
!= __last
; ++__first
)
1700 #if __cplusplus >= 201103L
1701 emplace_back(*__first
);
1703 push_back(*__first
);
1707 __throw_exception_again
;
1711 // Called by the second initialize_dispatch above
1712 template<typename _ForwardIterator
>
1713 _GLIBCXX20_CONSTEXPR
1715 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1716 std::forward_iterator_tag
)
1718 const size_type __n
= std::distance(__first
, __last
);
1720 this->_M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1721 _Guard_alloc
__guard(__start
, __n
, *this);
1722 this->_M_impl
._M_finish
= std::__uninitialized_copy_a
1723 (__first
, __last
, __start
, _M_get_Tp_allocator());
1724 this->_M_impl
._M_start
= __start
;
1725 (void) __guard
._M_release();
1726 this->_M_impl
._M_end_of_storage
= __start
+ __n
;
1729 // Called by the first initialize_dispatch above and by the
1730 // vector(n,value,a) constructor.
1731 _GLIBCXX20_CONSTEXPR
1733 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1735 this->_M_impl
._M_finish
=
1736 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1737 _M_get_Tp_allocator());
1740 #if __cplusplus >= 201103L
1741 // Called by the vector(n) constructor.
1742 _GLIBCXX20_CONSTEXPR
1744 _M_default_initialize(size_type __n
)
1746 this->_M_impl
._M_finish
=
1747 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1748 _M_get_Tp_allocator());
1752 // Internal assign functions follow. The *_aux functions do the actual
1753 // assignment work for the range versions.
1755 // Called by the range assign to implement [23.1.1]/9
1757 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1758 // 438. Ambiguity in the "do the right thing" clause
1759 template<typename _Integer
>
1760 _GLIBCXX20_CONSTEXPR
1762 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1763 { _M_fill_assign(__n
, __val
); }
1765 // Called by the range assign to implement [23.1.1]/9
1766 template<typename _InputIterator
>
1767 _GLIBCXX20_CONSTEXPR
1769 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1771 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1773 // Called by the second assign_dispatch above
1774 template<typename _InputIterator
>
1775 _GLIBCXX20_CONSTEXPR
1777 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1778 std::input_iterator_tag
);
1780 // Called by the second assign_dispatch above
1781 template<typename _ForwardIterator
>
1782 _GLIBCXX20_CONSTEXPR
1784 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1785 std::forward_iterator_tag
);
1787 // Called by assign(n,t), and the range assign when it turns out
1788 // to be the same thing.
1789 _GLIBCXX20_CONSTEXPR
1791 _M_fill_assign(size_type __n
, const value_type
& __val
);
1793 // Internal insert functions follow.
1795 // Called by the range insert to implement [23.1.1]/9
1797 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1798 // 438. Ambiguity in the "do the right thing" clause
1799 template<typename _Integer
>
1800 _GLIBCXX20_CONSTEXPR
1802 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1804 { _M_fill_insert(__pos
, __n
, __val
); }
1806 // Called by the range insert to implement [23.1.1]/9
1807 template<typename _InputIterator
>
1808 _GLIBCXX20_CONSTEXPR
1810 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1811 _InputIterator __last
, __false_type
)
1813 _M_range_insert(__pos
, __first
, __last
,
1814 std::__iterator_category(__first
));
1817 // Called by the second insert_dispatch above
1818 template<typename _InputIterator
>
1819 _GLIBCXX20_CONSTEXPR
1821 _M_range_insert(iterator __pos
, _InputIterator __first
,
1822 _InputIterator __last
, std::input_iterator_tag
);
1824 // Called by the second insert_dispatch above
1825 template<typename _ForwardIterator
>
1826 _GLIBCXX20_CONSTEXPR
1828 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1829 _ForwardIterator __last
, std::forward_iterator_tag
);
1831 // Called by insert(p,n,x), and the range insert when it turns out to be
1833 _GLIBCXX20_CONSTEXPR
1835 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1837 #if __cplusplus >= 201103L
1838 // Called by resize(n).
1839 _GLIBCXX20_CONSTEXPR
1841 _M_default_append(size_type __n
);
1843 _GLIBCXX20_CONSTEXPR
1848 #if __cplusplus < 201103L
1849 // Called by insert(p,x)
1851 _M_insert_aux(iterator __position
, const value_type
& __x
);
1854 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1857 _M_realloc_append(const value_type
& __x
);
1859 // A value_type object constructed with _Alloc_traits::construct()
1860 // and destroyed with _Alloc_traits::destroy().
1861 struct _Temporary_value
1863 template<typename
... _Args
>
1864 _GLIBCXX20_CONSTEXPR
explicit
1865 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1867 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1868 std::forward
<_Args
>(__args
)...);
1871 _GLIBCXX20_CONSTEXPR
1873 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1875 _GLIBCXX20_CONSTEXPR value_type
&
1876 _M_val() noexcept
{ return _M_storage
._M_val
; }
1879 _GLIBCXX20_CONSTEXPR _Tp
*
1880 _M_ptr() noexcept
{ return std::__addressof(_M_storage
._M_val
); }
1884 constexpr _Storage() : _M_byte() { }
1885 _GLIBCXX20_CONSTEXPR
~_Storage() { }
1886 _Storage
& operator=(const _Storage
&) = delete;
1887 unsigned char _M_byte
;
1892 _Storage _M_storage
;
1895 // Called by insert(p,x) and other functions when insertion needs to
1896 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1897 template<typename _Arg
>
1898 _GLIBCXX20_CONSTEXPR
1900 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1902 template<typename
... _Args
>
1903 _GLIBCXX20_CONSTEXPR
1905 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1907 template<typename
... _Args
>
1908 _GLIBCXX20_CONSTEXPR
1910 _M_realloc_append(_Args
&&... __args
);
1912 // Either move-construct at the end, or forward to _M_insert_aux.
1913 _GLIBCXX20_CONSTEXPR
1915 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1917 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1918 template<typename
... _Args
>
1919 _GLIBCXX20_CONSTEXPR
1921 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1923 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1924 _GLIBCXX20_CONSTEXPR
1926 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1927 { return _M_insert_rval(__position
, std::move(__v
)); }
1930 // Called by _M_fill_insert, _M_insert_aux etc.
1931 _GLIBCXX20_CONSTEXPR
1933 _M_check_len(size_type __n
, const char* __s
) const
1935 if (max_size() - size() < __n
)
1936 __throw_length_error(__N(__s
));
1938 const size_type __len
= size() + (std::max
)(size(), __n
);
1939 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1942 // Called by constructors to check initial size.
1943 static _GLIBCXX20_CONSTEXPR size_type
1944 _S_check_init_len(size_type __n
, const allocator_type
& __a
)
1946 if (__n
> _S_max_size(_Tp_alloc_type(__a
)))
1947 __throw_length_error(
1948 __N("cannot create std::vector larger than max_size()"));
1952 static _GLIBCXX20_CONSTEXPR size_type
1953 _S_max_size(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
1955 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1956 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1957 // (even if std::allocator_traits::max_size says we can).
1958 const size_t __diffmax
1959 = __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
1960 const size_t __allocmax
= _Alloc_traits::max_size(__a
);
1961 return (std::min
)(__diffmax
, __allocmax
);
1964 // Internal erase functions follow.
1966 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1968 _GLIBCXX20_CONSTEXPR
1970 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1972 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1974 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1975 _M_get_Tp_allocator());
1976 this->_M_impl
._M_finish
= __pos
;
1977 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1981 _GLIBCXX20_CONSTEXPR
1983 _M_erase(iterator __position
);
1985 _GLIBCXX20_CONSTEXPR
1987 _M_erase(iterator __first
, iterator __last
);
1989 #if __cplusplus >= 201103L
1991 // Constant-time move assignment when source object's memory can be
1992 // moved, either because the source's allocator will move too
1993 // or because the allocators are equal.
1994 _GLIBCXX20_CONSTEXPR
1996 _M_move_assign(vector
&& __x
, true_type
) noexcept
1998 vector
__tmp(get_allocator());
1999 this->_M_impl
._M_swap_data(__x
._M_impl
);
2000 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
2001 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
2004 // Do move assignment when it might not be possible to move source
2005 // object's memory, resulting in a linear-time operation.
2006 _GLIBCXX20_CONSTEXPR
2008 _M_move_assign(vector
&& __x
, false_type
)
2010 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2011 _M_move_assign(std::move(__x
), true_type());
2014 // The rvalue's allocator cannot be moved and is not equal,
2015 // so we need to individually move each element.
2016 this->_M_assign_aux(std::make_move_iterator(__x
.begin()),
2017 std::make_move_iterator(__x
.end()),
2018 std::random_access_iterator_tag());
2024 template<typename _Up
>
2025 _GLIBCXX20_CONSTEXPR
2027 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
2030 #if __cplusplus >= 201103L
2031 template<typename _Ptr
>
2032 _GLIBCXX20_CONSTEXPR
2033 typename
std::pointer_traits
<_Ptr
>::element_type
*
2034 _M_data_ptr(_Ptr __ptr
) const
2035 { return empty() ? nullptr : std::__to_address(__ptr
); }
2037 template<typename _Up
>
2039 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
2042 template<typename _Ptr
>
2044 _M_data_ptr(_Ptr __ptr
)
2045 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
2047 template<typename _Ptr
>
2049 _M_data_ptr(_Ptr __ptr
) const
2050 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
2054 #if __cpp_deduction_guides >= 201606
2055 template<typename _InputIterator
, typename _ValT
2056 = typename iterator_traits
<_InputIterator
>::value_type
,
2057 typename _Allocator
= allocator
<_ValT
>,
2058 typename
= _RequireInputIter
<_InputIterator
>,
2059 typename
= _RequireAllocator
<_Allocator
>>
2060 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
2061 -> vector
<_ValT
, _Allocator
>;
2065 * @brief Vector equality comparison.
2066 * @param __x A %vector.
2067 * @param __y A %vector of the same type as @a __x.
2068 * @return True iff the size and elements of the vectors are equal.
2070 * This is an equivalence relation. It is linear in the size of the
2071 * vectors. Vectors are considered equivalent if their sizes are equal,
2072 * and if corresponding elements compare equal.
2074 template<typename _Tp
, typename _Alloc
>
2075 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2077 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2078 { return (__x
.size() == __y
.size()
2079 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
2081 #if __cpp_lib_three_way_comparison
2083 * @brief Vector ordering relation.
2084 * @param __x A `vector`.
2085 * @param __y A `vector` of the same type as `__x`.
2086 * @return A value indicating whether `__x` is less than, equal to,
2087 * greater than, or incomparable with `__y`.
2089 * See `std::lexicographical_compare_three_way()` for how the determination
2090 * is made. This operator is used to synthesize relational operators like
2093 template<typename _Tp
, typename _Alloc
>
2094 [[nodiscard
]] _GLIBCXX20_CONSTEXPR
2095 inline __detail::__synth3way_t
<_Tp
>
2096 operator<=>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2098 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
2099 __y
.begin(), __y
.end(),
2100 __detail::__synth3way
);
2104 * @brief Vector ordering relation.
2105 * @param __x A %vector.
2106 * @param __y A %vector of the same type as @a __x.
2107 * @return True iff @a __x is lexicographically less than @a __y.
2109 * This is a total ordering relation. It is linear in the size of the
2110 * vectors. The elements must be comparable with @c <.
2112 * See std::lexicographical_compare() for how the determination is made.
2114 template<typename _Tp
, typename _Alloc
>
2115 _GLIBCXX_NODISCARD
inline bool
2116 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2117 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2118 __y
.begin(), __y
.end()); }
2120 /// Based on operator==
2121 template<typename _Tp
, typename _Alloc
>
2122 _GLIBCXX_NODISCARD
inline bool
2123 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2124 { return !(__x
== __y
); }
2126 /// Based on operator<
2127 template<typename _Tp
, typename _Alloc
>
2128 _GLIBCXX_NODISCARD
inline bool
2129 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2130 { return __y
< __x
; }
2132 /// Based on operator<
2133 template<typename _Tp
, typename _Alloc
>
2134 _GLIBCXX_NODISCARD
inline bool
2135 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2136 { return !(__y
< __x
); }
2138 /// Based on operator<
2139 template<typename _Tp
, typename _Alloc
>
2140 _GLIBCXX_NODISCARD
inline bool
2141 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2142 { return !(__x
< __y
); }
2143 #endif // three-way comparison
2145 /// See std::vector::swap().
2146 template<typename _Tp
, typename _Alloc
>
2147 _GLIBCXX20_CONSTEXPR
2149 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
2150 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2153 _GLIBCXX_END_NAMESPACE_CONTAINER
2155 #if __cplusplus >= 201703L
2156 namespace __detail::__variant
2158 template<typename
> struct _Never_valueless_alt
; // see <variant>
2160 // Provide the strong exception-safety guarantee when emplacing a
2161 // vector into a variant, but only if move assignment cannot throw.
2162 template<typename _Tp
, typename _Alloc
>
2163 struct _Never_valueless_alt
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
2164 : std::is_nothrow_move_assignable
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
2166 } // namespace __detail::__variant
2169 _GLIBCXX_END_NAMESPACE_VERSION
2172 #endif /* _STL_VECTOR_H */