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
)
142 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
143 : _Tp_alloc_type(__a
)
146 #if __cplusplus >= 201103L
147 // Not defaulted, to enforce noexcept(true) even when
148 // !is_nothrow_move_constructible<_Tp_alloc_type>.
150 _Vector_impl(_Vector_impl
&& __x
) noexcept
151 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
155 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
156 : _Tp_alloc_type(std::move(__a
))
160 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
161 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
165 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
166 template<typename
= _Tp_alloc_type
>
169 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
170 ::size_type size_type
;
172 static _GLIBCXX20_CONSTEXPR
void
173 _S_shrink(_Vector_impl
&, size_type
) { }
174 static _GLIBCXX20_CONSTEXPR
void
175 _S_on_dealloc(_Vector_impl
&) { }
177 typedef _Vector_impl
& _Reinit
;
181 _GLIBCXX20_CONSTEXPR
_Grow(_Vector_impl
&, size_type
) { }
182 _GLIBCXX20_CONSTEXPR
void _M_grew(size_type
) { }
186 // Enable ASan annotations for memory obtained from std::allocator.
187 template<typename _Up
>
188 struct _Asan
<allocator
<_Up
> >
190 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
191 ::size_type size_type
;
193 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
194 // mark end of valid region as __curr instead of __prev.
195 static _GLIBCXX20_CONSTEXPR
void
196 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
198 #if __cpp_lib_is_constant_evaluated
199 if (std::is_constant_evaluated())
202 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
203 __impl
._M_end_of_storage
, __prev
, __curr
);
206 static _GLIBCXX20_CONSTEXPR
void
207 _S_grow(_Vector_impl
& __impl
, size_type __n
)
208 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
210 static _GLIBCXX20_CONSTEXPR
void
211 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
212 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
214 static _GLIBCXX20_CONSTEXPR
void
215 _S_on_dealloc(_Vector_impl
& __impl
)
218 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
221 // Used on reallocation to tell ASan unused capacity is invalid.
224 explicit _GLIBCXX20_CONSTEXPR
225 _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
227 // Mark unused capacity as valid again before deallocating it.
228 _S_on_dealloc(_M_impl
);
234 // Mark unused capacity as invalid after reallocation.
235 if (_M_impl
._M_start
)
236 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
240 _Vector_impl
& _M_impl
;
242 #if __cplusplus >= 201103L
243 _Reinit(const _Reinit
&) = delete;
244 _Reinit
& operator=(const _Reinit
&) = delete;
248 // Tell ASan when unused capacity is initialized to be valid.
252 _Grow(_Vector_impl
& __impl
, size_type __n
)
253 : _M_impl(__impl
), _M_n(__n
)
254 { _S_grow(_M_impl
, __n
); }
257 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
260 void _M_grew(size_type __n
) { _M_n
-= __n
; }
262 #if __cplusplus >= 201103L
263 _Grow(const _Grow
&) = delete;
264 _Grow
& operator=(const _Grow
&) = delete;
267 _Vector_impl
& _M_impl
;
272 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
273 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
274 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
275 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
276 typename _Base::_Vector_impl::template _Asan<>::_Grow \
277 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
278 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
279 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
280 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
281 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
282 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
283 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
284 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
285 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
286 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
287 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
288 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
289 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
293 typedef _Alloc allocator_type
;
297 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
298 { return this->_M_impl
; }
301 const _Tp_alloc_type
&
302 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
303 { return this->_M_impl
; }
307 get_allocator() const _GLIBCXX_NOEXCEPT
308 { return allocator_type(_M_get_Tp_allocator()); }
310 #if __cplusplus >= 201103L
311 _Vector_base() = default;
317 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
320 // Kept for ABI compatibility.
321 #if !_GLIBCXX_INLINE_VERSION
323 _Vector_base(size_t __n
)
325 { _M_create_storage(__n
); }
329 _Vector_base(size_t __n
, const allocator_type
& __a
)
331 { _M_create_storage(__n
); }
333 #if __cplusplus >= 201103L
334 _Vector_base(_Vector_base
&&) = default;
336 // Kept for ABI compatibility.
337 # if !_GLIBCXX_INLINE_VERSION
339 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
340 : _M_impl(std::move(__a
)) { }
343 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
346 if (__x
.get_allocator() == __a
)
347 this->_M_impl
._M_swap_data(__x
._M_impl
);
350 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
351 _M_create_storage(__n
);
357 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
358 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
363 ~_Vector_base() _GLIBCXX_NOEXCEPT
365 _M_deallocate(_M_impl
._M_start
,
366 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
370 _Vector_impl _M_impl
;
374 _M_allocate(size_t __n
)
376 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
377 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
382 _M_deallocate(pointer __p
, size_t __n
)
384 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
386 _Tr::deallocate(_M_impl
, __p
, __n
);
393 _M_create_storage(size_t __n
)
395 this->_M_impl
._M_start
= this->_M_allocate(__n
);
396 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
397 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
402 * @brief A standard container which offers fixed time access to
403 * individual elements in any order.
409 * @tparam _Tp Type of element.
410 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
412 * Meets the requirements of a <a href="tables.html#65">container</a>, a
413 * <a href="tables.html#66">reversible container</a>, and a
414 * <a href="tables.html#67">sequence</a>, including the
415 * <a href="tables.html#68">optional sequence requirements</a> with the
416 * %exception of @c push_front and @c pop_front.
418 * In some terminology a %vector can be described as a dynamic
419 * C-style array, it offers fast and efficient access to individual
420 * elements in any order and saves the user from worrying about
421 * memory and size allocation. Subscripting ( @c [] ) access is
422 * also provided as with C-style arrays.
424 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
425 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
427 #ifdef _GLIBCXX_CONCEPT_CHECKS
428 // Concept requirements.
429 typedef typename
_Alloc::value_type _Alloc_value_type
;
430 # if __cplusplus < 201103L
431 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
433 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
436 #if __cplusplus >= 201103L
437 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
438 "std::vector must have a non-const, non-volatile value_type");
439 # if __cplusplus > 201703L || defined __STRICT_ANSI__
440 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
441 "std::vector must have the same value_type as its allocator");
445 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
446 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
447 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
450 typedef _Tp value_type
;
451 typedef typename
_Base::pointer pointer
;
452 typedef typename
_Alloc_traits::const_pointer const_pointer
;
453 typedef typename
_Alloc_traits::reference reference
;
454 typedef typename
_Alloc_traits::const_reference const_reference
;
455 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
456 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
458 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
459 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
460 typedef size_t size_type
;
461 typedef ptrdiff_t difference_type
;
462 typedef _Alloc allocator_type
;
465 #if __cplusplus >= 201103L
466 static constexpr bool
467 _S_nothrow_relocate(true_type
)
469 return noexcept(std::__relocate_a(std::declval
<pointer
>(),
470 std::declval
<pointer
>(),
471 std::declval
<pointer
>(),
472 std::declval
<_Tp_alloc_type
&>()));
475 static constexpr bool
476 _S_nothrow_relocate(false_type
)
479 static constexpr bool
482 // Instantiating std::__relocate_a might cause an error outside the
483 // immediate context (in __relocate_object_a's noexcept-specifier),
484 // so only do it if we know the type can be move-inserted into *this.
485 return _S_nothrow_relocate(__is_move_insertable
<_Tp_alloc_type
>{});
489 _S_do_relocate(pointer __first
, pointer __last
, pointer __result
,
490 _Tp_alloc_type
& __alloc
, true_type
) noexcept
492 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
496 _S_do_relocate(pointer
, pointer
, pointer __result
,
497 _Tp_alloc_type
&, false_type
) noexcept
500 static _GLIBCXX20_CONSTEXPR pointer
501 _S_relocate(pointer __first
, pointer __last
, pointer __result
,
502 _Tp_alloc_type
& __alloc
) noexcept
504 #if __cpp_if_constexpr
505 // All callers have already checked _S_use_relocate() so just do it.
506 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
508 using __do_it
= __bool_constant
<_S_use_relocate()>;
509 return _S_do_relocate(__first
, __last
, __result
, __alloc
, __do_it
{});
515 using _Base::_M_allocate
;
516 using _Base::_M_deallocate
;
517 using _Base::_M_impl
;
518 using _Base::_M_get_Tp_allocator
;
521 // [23.2.4.1] construct/copy/destroy
522 // (assign() and get_allocator() are also listed in this section)
525 * @brief Creates a %vector with no elements.
527 #if __cplusplus >= 201103L
534 * @brief Creates a %vector with no elements.
535 * @param __a An allocator object.
539 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
542 #if __cplusplus >= 201103L
544 * @brief Creates a %vector with default constructed elements.
545 * @param __n The number of elements to initially create.
546 * @param __a An allocator.
548 * This constructor fills the %vector with @a __n default
549 * constructed elements.
553 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
554 : _Base(_S_check_init_len(__n
, __a
), __a
)
555 { _M_default_initialize(__n
); }
558 * @brief Creates a %vector with copies of an exemplar element.
559 * @param __n The number of elements to initially create.
560 * @param __value An element to copy.
561 * @param __a An allocator.
563 * This constructor fills the %vector with @a __n copies of @a __value.
566 vector(size_type __n
, const value_type
& __value
,
567 const allocator_type
& __a
= allocator_type())
568 : _Base(_S_check_init_len(__n
, __a
), __a
)
569 { _M_fill_initialize(__n
, __value
); }
572 * @brief Creates a %vector with copies of an exemplar element.
573 * @param __n The number of elements to initially create.
574 * @param __value An element to copy.
575 * @param __a An allocator.
577 * This constructor fills the %vector with @a __n copies of @a __value.
580 vector(size_type __n
, const value_type
& __value
= value_type(),
581 const allocator_type
& __a
= allocator_type())
582 : _Base(_S_check_init_len(__n
, __a
), __a
)
583 { _M_fill_initialize(__n
, __value
); }
587 * @brief %Vector copy constructor.
588 * @param __x A %vector of identical element and allocator types.
590 * All the elements of @a __x are copied, but any unused capacity in
591 * @a __x will not be copied
592 * (i.e. capacity() == size() in the new %vector).
594 * The newly-created %vector uses a copy of the allocator object used
595 * by @a __x (unless the allocator traits dictate a different object).
598 vector(const vector
& __x
)
600 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
602 this->_M_impl
._M_finish
=
603 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
604 this->_M_impl
._M_start
,
605 _M_get_Tp_allocator());
608 #if __cplusplus >= 201103L
610 * @brief %Vector move constructor.
612 * The newly-created %vector contains the exact contents of the
614 * The contents of the moved instance are a valid, but unspecified
617 vector(vector
&&) noexcept
= default;
619 /// Copy constructor with alternative allocator
621 vector(const vector
& __x
, const __type_identity_t
<allocator_type
>& __a
)
622 : _Base(__x
.size(), __a
)
624 this->_M_impl
._M_finish
=
625 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
626 this->_M_impl
._M_start
,
627 _M_get_Tp_allocator());
632 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
633 : _Base(__m
, std::move(__rv
))
637 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
640 if (__rv
.get_allocator() == __m
)
641 this->_M_impl
._M_swap_data(__rv
._M_impl
);
642 else if (!__rv
.empty())
644 this->_M_create_storage(__rv
.size());
645 this->_M_impl
._M_finish
=
646 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
647 this->_M_impl
._M_start
,
648 _M_get_Tp_allocator());
654 /// Move constructor with alternative allocator
656 vector(vector
&& __rv
, const __type_identity_t
<allocator_type
>& __m
)
658 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
659 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
660 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
664 * @brief Builds a %vector from an initializer list.
665 * @param __l An initializer_list.
666 * @param __a An allocator.
668 * Create a %vector consisting of copies of the elements in the
669 * initializer_list @a __l.
671 * This will call the element type's copy constructor N times
672 * (where N is @a __l.size()) and do no memory reallocation.
675 vector(initializer_list
<value_type
> __l
,
676 const allocator_type
& __a
= allocator_type())
679 _M_range_initialize(__l
.begin(), __l
.end(),
680 random_access_iterator_tag());
685 * @brief Builds a %vector from a range.
686 * @param __first An input iterator.
687 * @param __last An input iterator.
688 * @param __a An allocator.
690 * Create a %vector consisting of copies of the elements from
693 * If the iterators are forward, bidirectional, or
694 * random-access, then this will call the elements' copy
695 * constructor N times (where N is distance(first,last)) and do
696 * no memory reallocation. But if only input iterators are
697 * used, then this will do at most 2N calls to the copy
698 * constructor, and logN memory reallocations.
700 #if __cplusplus >= 201103L
701 template<typename _InputIterator
,
702 typename
= std::_RequireInputIter
<_InputIterator
>>
704 vector(_InputIterator __first
, _InputIterator __last
,
705 const allocator_type
& __a
= allocator_type())
708 _M_range_initialize(__first
, __last
,
709 std::__iterator_category(__first
));
712 template<typename _InputIterator
>
713 vector(_InputIterator __first
, _InputIterator __last
,
714 const allocator_type
& __a
= allocator_type())
717 // Check whether it's an integral type. If so, it's not an iterator.
718 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
719 _M_initialize_dispatch(__first
, __last
, _Integral());
724 * The dtor only erases the elements, and note that if the
725 * elements themselves are pointers, the pointed-to memory is
726 * not touched in any way. Managing the pointer is the user's
730 ~vector() _GLIBCXX_NOEXCEPT
732 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
733 _M_get_Tp_allocator());
734 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
738 * @brief %Vector assignment operator.
739 * @param __x A %vector of identical element and allocator types.
741 * All the elements of @a __x are copied, but any unused capacity in
742 * @a __x will not be copied.
744 * Whether the allocator is copied depends on the allocator traits.
748 operator=(const vector
& __x
);
750 #if __cplusplus >= 201103L
752 * @brief %Vector move assignment operator.
753 * @param __x A %vector of identical element and allocator types.
755 * The contents of @a __x are moved into this %vector (without copying,
756 * if the allocators permit it).
757 * Afterwards @a __x is a valid, but unspecified %vector.
759 * Whether the allocator is moved depends on the allocator traits.
763 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
765 constexpr bool __move_storage
=
766 _Alloc_traits::_S_propagate_on_move_assign()
767 || _Alloc_traits::_S_always_equal();
768 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
773 * @brief %Vector list assignment operator.
774 * @param __l An initializer_list.
776 * This function fills a %vector with copies of the elements in the
777 * initializer list @a __l.
779 * Note that the assignment completely changes the %vector and
780 * that the resulting %vector's size is the same as the number
781 * of elements assigned.
785 operator=(initializer_list
<value_type
> __l
)
787 this->_M_assign_aux(__l
.begin(), __l
.end(),
788 random_access_iterator_tag());
794 * @brief Assigns a given value to a %vector.
795 * @param __n Number of elements to be assigned.
796 * @param __val Value to be assigned.
798 * This function fills a %vector with @a __n copies of the given
799 * value. Note that the assignment completely changes the
800 * %vector and that the resulting %vector's size is the same as
801 * the number of elements assigned.
805 assign(size_type __n
, const value_type
& __val
)
806 { _M_fill_assign(__n
, __val
); }
809 * @brief Assigns a range to a %vector.
810 * @param __first An input iterator.
811 * @param __last An input iterator.
813 * This function fills a %vector with copies of the elements in the
814 * range [__first,__last).
816 * Note that the assignment completely changes the %vector and
817 * that the resulting %vector's size is the same as the number
818 * of elements assigned.
820 #if __cplusplus >= 201103L
821 template<typename _InputIterator
,
822 typename
= std::_RequireInputIter
<_InputIterator
>>
825 assign(_InputIterator __first
, _InputIterator __last
)
826 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
828 template<typename _InputIterator
>
830 assign(_InputIterator __first
, _InputIterator __last
)
832 // Check whether it's an integral type. If so, it's not an iterator.
833 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
834 _M_assign_dispatch(__first
, __last
, _Integral());
838 #if __cplusplus >= 201103L
840 * @brief Assigns an initializer list to a %vector.
841 * @param __l An initializer_list.
843 * This function fills a %vector with copies of the elements in the
844 * initializer list @a __l.
846 * Note that the assignment completely changes the %vector and
847 * that the resulting %vector's size is the same as the number
848 * of elements assigned.
852 assign(initializer_list
<value_type
> __l
)
854 this->_M_assign_aux(__l
.begin(), __l
.end(),
855 random_access_iterator_tag());
859 /// Get a copy of the memory allocation object.
860 using _Base::get_allocator
;
864 * Returns a read/write iterator that points to the first
865 * element in the %vector. Iteration is done in ordinary
868 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
870 begin() _GLIBCXX_NOEXCEPT
871 { return iterator(this->_M_impl
._M_start
); }
874 * Returns a read-only (constant) iterator that points to the
875 * first element in the %vector. Iteration is done in ordinary
878 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
880 begin() const _GLIBCXX_NOEXCEPT
881 { return const_iterator(this->_M_impl
._M_start
); }
884 * Returns a read/write iterator that points one past the last
885 * element in the %vector. Iteration is done in ordinary
888 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
890 end() _GLIBCXX_NOEXCEPT
891 { return iterator(this->_M_impl
._M_finish
); }
894 * Returns a read-only (constant) iterator that points one past
895 * the last element in the %vector. Iteration is done in
896 * ordinary element order.
898 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
900 end() const _GLIBCXX_NOEXCEPT
901 { return const_iterator(this->_M_impl
._M_finish
); }
904 * Returns a read/write reverse iterator that points to the
905 * last element in the %vector. Iteration is done in reverse
908 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
910 rbegin() _GLIBCXX_NOEXCEPT
911 { return reverse_iterator(end()); }
914 * Returns a read-only (constant) reverse iterator that points
915 * to the last element in the %vector. Iteration is done in
916 * reverse element order.
918 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
919 const_reverse_iterator
920 rbegin() const _GLIBCXX_NOEXCEPT
921 { return const_reverse_iterator(end()); }
924 * Returns a read/write reverse iterator that points to one
925 * before the first element in the %vector. Iteration is done
926 * in reverse element order.
928 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
930 rend() _GLIBCXX_NOEXCEPT
931 { return reverse_iterator(begin()); }
934 * Returns a read-only (constant) reverse iterator that points
935 * to one before the first element in the %vector. Iteration
936 * is done in reverse element order.
938 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
939 const_reverse_iterator
940 rend() const _GLIBCXX_NOEXCEPT
941 { return const_reverse_iterator(begin()); }
943 #if __cplusplus >= 201103L
945 * Returns a read-only (constant) iterator that points to the
946 * first element in the %vector. Iteration is done in ordinary
949 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
951 cbegin() const noexcept
952 { return const_iterator(this->_M_impl
._M_start
); }
955 * Returns a read-only (constant) iterator that points one past
956 * the last element in the %vector. Iteration is done in
957 * ordinary element order.
959 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
961 cend() const noexcept
962 { return const_iterator(this->_M_impl
._M_finish
); }
965 * Returns a read-only (constant) reverse iterator that points
966 * to the last element in the %vector. Iteration is done in
967 * reverse element order.
969 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
970 const_reverse_iterator
971 crbegin() const noexcept
972 { return const_reverse_iterator(end()); }
975 * Returns a read-only (constant) reverse iterator that points
976 * to one before the first element in the %vector. Iteration
977 * is done in reverse element order.
979 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
980 const_reverse_iterator
981 crend() const noexcept
982 { return const_reverse_iterator(begin()); }
985 // [23.2.4.2] capacity
986 /** Returns the number of elements in the %vector. */
987 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
989 size() const _GLIBCXX_NOEXCEPT
990 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
992 /** Returns the size() of the largest possible %vector. */
993 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
995 max_size() const _GLIBCXX_NOEXCEPT
996 { return _S_max_size(_M_get_Tp_allocator()); }
998 #if __cplusplus >= 201103L
1000 * @brief Resizes the %vector to the specified number of elements.
1001 * @param __new_size Number of elements the %vector should contain.
1003 * This function will %resize the %vector to the specified
1004 * number of elements. If the number is smaller than the
1005 * %vector's current size the %vector is truncated, otherwise
1006 * default constructed elements are appended.
1008 _GLIBCXX20_CONSTEXPR
1010 resize(size_type __new_size
)
1012 if (__new_size
> size())
1013 _M_default_append(__new_size
- size());
1014 else if (__new_size
< size())
1015 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1019 * @brief Resizes the %vector to the specified number of elements.
1020 * @param __new_size Number of elements the %vector should contain.
1021 * @param __x Data with which new elements should be populated.
1023 * This function will %resize the %vector to the specified
1024 * number of elements. If the number is smaller than the
1025 * %vector's current size the %vector is truncated, otherwise
1026 * the %vector is extended and new elements are populated with
1029 _GLIBCXX20_CONSTEXPR
1031 resize(size_type __new_size
, const value_type
& __x
)
1033 if (__new_size
> size())
1034 _M_fill_insert(end(), __new_size
- size(), __x
);
1035 else if (__new_size
< size())
1036 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1040 * @brief Resizes the %vector to the specified number of elements.
1041 * @param __new_size Number of elements the %vector should contain.
1042 * @param __x Data with which new elements should be populated.
1044 * This function will %resize the %vector to the specified
1045 * number of elements. If the number is smaller than the
1046 * %vector's current size the %vector is truncated, otherwise
1047 * the %vector is extended and new elements are populated with
1050 _GLIBCXX20_CONSTEXPR
1052 resize(size_type __new_size
, value_type __x
= value_type())
1054 if (__new_size
> size())
1055 _M_fill_insert(end(), __new_size
- size(), __x
);
1056 else if (__new_size
< size())
1057 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
1061 #if __cplusplus >= 201103L
1062 /** A non-binding request to reduce capacity() to size(). */
1063 _GLIBCXX20_CONSTEXPR
1066 { _M_shrink_to_fit(); }
1070 * Returns the total number of elements that the %vector can
1071 * hold before needing to allocate more memory.
1073 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1075 capacity() const _GLIBCXX_NOEXCEPT
1077 return size_type(this->_M_impl
._M_end_of_storage
1078 - this->_M_impl
._M_start
);
1082 * Returns true if the %vector is empty. (Thus begin() would
1085 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1087 empty() const _GLIBCXX_NOEXCEPT
1088 { return begin() == end(); }
1091 * @brief Attempt to preallocate enough memory for specified number of
1093 * @param __n Number of elements required.
1094 * @throw std::length_error If @a n exceeds @c max_size().
1096 * This function attempts to reserve enough memory for the
1097 * %vector to hold the specified number of elements. If the
1098 * number requested is more than max_size(), length_error is
1101 * The advantage of this function is that if optimal code is a
1102 * necessity and the user can determine the number of elements
1103 * that will be required, the user can reserve the memory in
1104 * %advance, and thus prevent a possible reallocation of memory
1105 * and copying of %vector data.
1107 _GLIBCXX20_CONSTEXPR
1109 reserve(size_type __n
);
1113 * @brief Subscript access to the data contained in the %vector.
1114 * @param __n The index of the element for which data should be
1116 * @return Read/write reference to data.
1118 * This operator allows for easy, array-style, data access.
1119 * Note that data access with this operator is unchecked and
1120 * out_of_range lookups are not defined. (For checked lookups
1123 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1125 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1127 __glibcxx_requires_subscript(__n
);
1128 return *(this->_M_impl
._M_start
+ __n
);
1132 * @brief Subscript access to the data contained in the %vector.
1133 * @param __n The index of the element for which data should be
1135 * @return Read-only (constant) reference to data.
1137 * This operator allows for easy, array-style, data access.
1138 * Note that data access with this operator is unchecked and
1139 * out_of_range lookups are not defined. (For checked lookups
1142 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1144 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1146 __glibcxx_requires_subscript(__n
);
1147 return *(this->_M_impl
._M_start
+ __n
);
1151 /// Safety check used only from at().
1152 _GLIBCXX20_CONSTEXPR
1154 _M_range_check(size_type __n
) const
1156 if (__n
>= this->size())
1157 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1158 "(which is %zu) >= this->size() "
1165 * @brief Provides access to the data contained in the %vector.
1166 * @param __n The index of the element for which data should be
1168 * @return Read/write reference to data.
1169 * @throw std::out_of_range If @a __n is an invalid index.
1171 * This function provides for safer data access. The parameter
1172 * is first checked that it is in the range of the vector. The
1173 * function throws out_of_range if the check fails.
1175 _GLIBCXX20_CONSTEXPR
1179 _M_range_check(__n
);
1180 return (*this)[__n
];
1184 * @brief Provides access to the data contained in the %vector.
1185 * @param __n The index of the element for which data should be
1187 * @return Read-only (constant) reference to data.
1188 * @throw std::out_of_range If @a __n is an invalid index.
1190 * This function provides for safer data access. The parameter
1191 * is first checked that it is in the range of the vector. The
1192 * function throws out_of_range if the check fails.
1194 _GLIBCXX20_CONSTEXPR
1196 at(size_type __n
) const
1198 _M_range_check(__n
);
1199 return (*this)[__n
];
1203 * Returns a read/write reference to the data at the first
1204 * element of the %vector.
1206 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1208 front() _GLIBCXX_NOEXCEPT
1210 __glibcxx_requires_nonempty();
1215 * Returns a read-only (constant) reference to the data at the first
1216 * element of the %vector.
1218 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1220 front() const _GLIBCXX_NOEXCEPT
1222 __glibcxx_requires_nonempty();
1227 * Returns a read/write reference to the data at the last
1228 * element of the %vector.
1230 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1232 back() _GLIBCXX_NOEXCEPT
1234 __glibcxx_requires_nonempty();
1235 return *(end() - 1);
1239 * Returns a read-only (constant) reference to the data at the
1240 * last element of the %vector.
1242 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1244 back() const _GLIBCXX_NOEXCEPT
1246 __glibcxx_requires_nonempty();
1247 return *(end() - 1);
1250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1251 // DR 464. Suggestion for new member functions in standard containers.
1254 * Returns a pointer such that [data(), data() + size()) is a valid
1255 * range. For a non-empty %vector, data() == &front().
1257 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1259 data() _GLIBCXX_NOEXCEPT
1260 { return _M_data_ptr(this->_M_impl
._M_start
); }
1262 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1264 data() const _GLIBCXX_NOEXCEPT
1265 { return _M_data_ptr(this->_M_impl
._M_start
); }
1267 // [23.2.4.3] modifiers
1269 * @brief Add data to the end of the %vector.
1270 * @param __x Data to be added.
1272 * This is a typical stack operation. The function creates an
1273 * element at the end of the %vector and assigns the given data
1274 * to it. Due to the nature of a %vector this operation can be
1275 * done in constant time if the %vector has preallocated space
1278 _GLIBCXX20_CONSTEXPR
1280 push_back(const value_type
& __x
)
1282 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1284 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1285 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1287 ++this->_M_impl
._M_finish
;
1288 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1291 _M_realloc_append(__x
);
1294 #if __cplusplus >= 201103L
1295 _GLIBCXX20_CONSTEXPR
1297 push_back(value_type
&& __x
)
1298 { emplace_back(std::move(__x
)); }
1300 template<typename
... _Args
>
1301 #if __cplusplus > 201402L
1302 _GLIBCXX20_CONSTEXPR
1307 emplace_back(_Args
&&... __args
);
1311 * @brief Removes last element.
1313 * This is a typical stack operation. It shrinks the %vector by one.
1315 * Note that no data is returned, and if the last element's
1316 * data is needed, it should be retrieved before pop_back() is
1319 _GLIBCXX20_CONSTEXPR
1321 pop_back() _GLIBCXX_NOEXCEPT
1323 __glibcxx_requires_nonempty();
1324 --this->_M_impl
._M_finish
;
1325 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1326 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1329 #if __cplusplus >= 201103L
1331 * @brief Inserts an object in %vector before specified iterator.
1332 * @param __position A const_iterator into the %vector.
1333 * @param __args Arguments.
1334 * @return An iterator that points to the inserted data.
1336 * This function will insert an object of type T constructed
1337 * with T(std::forward<Args>(args)...) before the specified location.
1338 * Note that this kind of operation could be expensive for a %vector
1339 * and if it is frequently used the user should consider using
1342 template<typename
... _Args
>
1343 _GLIBCXX20_CONSTEXPR
1345 emplace(const_iterator __position
, _Args
&&... __args
)
1346 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1349 * @brief Inserts given value into %vector before specified iterator.
1350 * @param __position A const_iterator into the %vector.
1351 * @param __x Data to be inserted.
1352 * @return An iterator that points to the inserted data.
1354 * This function will insert a copy of the given value before
1355 * the specified location. Note that this kind of operation
1356 * could be expensive for a %vector and if it is frequently
1357 * used the user should consider using std::list.
1359 _GLIBCXX20_CONSTEXPR
1361 insert(const_iterator __position
, const value_type
& __x
);
1364 * @brief Inserts given value into %vector before specified iterator.
1365 * @param __position An iterator into the %vector.
1366 * @param __x Data to be inserted.
1367 * @return An iterator that points to the inserted data.
1369 * This function will insert a copy of the given value before
1370 * the specified location. Note that this kind of operation
1371 * could be expensive for a %vector and if it is frequently
1372 * used the user should consider using std::list.
1375 insert(iterator __position
, const value_type
& __x
);
1378 #if __cplusplus >= 201103L
1380 * @brief Inserts given rvalue into %vector before specified iterator.
1381 * @param __position A const_iterator into the %vector.
1382 * @param __x Data to be inserted.
1383 * @return An iterator that points to the inserted data.
1385 * This function will insert a copy of the given rvalue before
1386 * the specified location. Note that this kind of operation
1387 * could be expensive for a %vector and if it is frequently
1388 * used the user should consider using std::list.
1390 _GLIBCXX20_CONSTEXPR
1392 insert(const_iterator __position
, value_type
&& __x
)
1393 { return _M_insert_rval(__position
, std::move(__x
)); }
1396 * @brief Inserts an initializer_list into the %vector.
1397 * @param __position An iterator into the %vector.
1398 * @param __l An initializer_list.
1400 * This function will insert copies of the data in the
1401 * initializer_list @a l into the %vector before the location
1402 * specified by @a position.
1404 * Note that this kind of operation could be expensive for a
1405 * %vector and if it is frequently used the user should
1406 * consider using std::list.
1408 _GLIBCXX20_CONSTEXPR
1410 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1412 auto __offset
= __position
- cbegin();
1413 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1414 std::random_access_iterator_tag());
1415 return begin() + __offset
;
1419 #if __cplusplus >= 201103L
1421 * @brief Inserts a number of copies of given data into the %vector.
1422 * @param __position A const_iterator into the %vector.
1423 * @param __n Number of elements to be inserted.
1424 * @param __x Data to be inserted.
1425 * @return An iterator that points to the inserted data.
1427 * This function will insert a specified number of copies of
1428 * the given data before the location specified by @a position.
1430 * Note that this kind of operation could be expensive for a
1431 * %vector and if it is frequently used the user should
1432 * consider using std::list.
1434 _GLIBCXX20_CONSTEXPR
1436 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1438 difference_type __offset
= __position
- cbegin();
1439 _M_fill_insert(begin() + __offset
, __n
, __x
);
1440 return begin() + __offset
;
1444 * @brief Inserts a number of copies of given data into the %vector.
1445 * @param __position An iterator into the %vector.
1446 * @param __n Number of elements to be inserted.
1447 * @param __x Data to be inserted.
1449 * This function will insert a specified number of copies of
1450 * the given data before the location specified by @a position.
1452 * Note that this kind of operation could be expensive for a
1453 * %vector and if it is frequently used the user should
1454 * consider using std::list.
1457 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1458 { _M_fill_insert(__position
, __n
, __x
); }
1461 #if __cplusplus >= 201103L
1463 * @brief Inserts a range into the %vector.
1464 * @param __position A const_iterator into the %vector.
1465 * @param __first An input iterator.
1466 * @param __last An input iterator.
1467 * @return An iterator that points to the inserted data.
1469 * This function will insert copies of the data in the range
1470 * [__first,__last) into the %vector before the location specified
1473 * Note that this kind of operation could be expensive for a
1474 * %vector and if it is frequently used the user should
1475 * consider using std::list.
1477 template<typename _InputIterator
,
1478 typename
= std::_RequireInputIter
<_InputIterator
>>
1479 _GLIBCXX20_CONSTEXPR
1481 insert(const_iterator __position
, _InputIterator __first
,
1482 _InputIterator __last
)
1484 difference_type __offset
= __position
- cbegin();
1485 _M_range_insert(begin() + __offset
, __first
, __last
,
1486 std::__iterator_category(__first
));
1487 return begin() + __offset
;
1491 * @brief Inserts a range into the %vector.
1492 * @param __position An iterator into the %vector.
1493 * @param __first An input iterator.
1494 * @param __last An input iterator.
1496 * This function will insert copies of the data in the range
1497 * [__first,__last) into the %vector before the location specified
1500 * Note that this kind of operation could be expensive for a
1501 * %vector and if it is frequently used the user should
1502 * consider using std::list.
1504 template<typename _InputIterator
>
1506 insert(iterator __position
, _InputIterator __first
,
1507 _InputIterator __last
)
1509 // Check whether it's an integral type. If so, it's not an iterator.
1510 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1511 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1516 * @brief Remove element at given position.
1517 * @param __position Iterator pointing to element to be erased.
1518 * @return An iterator pointing to the next element (or end()).
1520 * This function will erase the element at the given position and thus
1521 * shorten the %vector by one.
1523 * Note This operation could be expensive and if it is
1524 * frequently used the user should consider using std::list.
1525 * The user is also cautioned that this function only erases
1526 * the element, and that if the element is itself a pointer,
1527 * the pointed-to memory is not touched in any way. Managing
1528 * the pointer is the user's responsibility.
1530 _GLIBCXX20_CONSTEXPR
1532 #if __cplusplus >= 201103L
1533 erase(const_iterator __position
)
1534 { return _M_erase(begin() + (__position
- cbegin())); }
1536 erase(iterator __position
)
1537 { return _M_erase(__position
); }
1541 * @brief Remove a range of elements.
1542 * @param __first Iterator pointing to the first element to be erased.
1543 * @param __last Iterator pointing to one past the last element to be
1545 * @return An iterator pointing to the element pointed to by @a __last
1546 * prior to erasing (or end()).
1548 * This function will erase the elements in the range
1549 * [__first,__last) and shorten the %vector accordingly.
1551 * Note This operation could be expensive and if it is
1552 * frequently used the user should consider using std::list.
1553 * The user is also cautioned that this function only erases
1554 * the elements, and that if the elements themselves are
1555 * pointers, the pointed-to memory is not touched in any way.
1556 * Managing the pointer is the user's responsibility.
1558 _GLIBCXX20_CONSTEXPR
1560 #if __cplusplus >= 201103L
1561 erase(const_iterator __first
, const_iterator __last
)
1563 const auto __beg
= begin();
1564 const auto __cbeg
= cbegin();
1565 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1568 erase(iterator __first
, iterator __last
)
1569 { return _M_erase(__first
, __last
); }
1573 * @brief Swaps data with another %vector.
1574 * @param __x A %vector of the same element and allocator types.
1576 * This exchanges the elements between two vectors in constant time.
1577 * (Three pointers, so it should be quite fast.)
1578 * Note that the global std::swap() function is specialized such that
1579 * std::swap(v1,v2) will feed to this function.
1581 * Whether the allocators are swapped depends on the allocator traits.
1583 _GLIBCXX20_CONSTEXPR
1585 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1587 #if __cplusplus >= 201103L
1588 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1589 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1591 this->_M_impl
._M_swap_data(__x
._M_impl
);
1592 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1593 __x
._M_get_Tp_allocator());
1597 * Erases all the elements. Note that this function only erases the
1598 * elements, and that if the elements themselves are pointers, the
1599 * pointed-to memory is not touched in any way. Managing the pointer is
1600 * the user's responsibility.
1602 _GLIBCXX20_CONSTEXPR
1604 clear() _GLIBCXX_NOEXCEPT
1605 { _M_erase_at_end(this->_M_impl
._M_start
); }
1609 * Memory expansion handler. Uses the member allocation function to
1610 * obtain @a n bytes of memory, and then copies [first,last) into it.
1612 template<typename _ForwardIterator
>
1613 _GLIBCXX20_CONSTEXPR
1615 _M_allocate_and_copy(size_type __n
,
1616 _ForwardIterator __first
, _ForwardIterator __last
)
1618 pointer __result
= this->_M_allocate(__n
);
1621 std::__uninitialized_copy_a(__first
, __last
, __result
,
1622 _M_get_Tp_allocator());
1627 _M_deallocate(__result
, __n
);
1628 __throw_exception_again
;
1633 // Internal constructor functions follow.
1635 // Called by the range constructor to implement [23.1.1]/9
1637 #if __cplusplus < 201103L
1638 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1639 // 438. Ambiguity in the "do the right thing" clause
1640 template<typename _Integer
>
1642 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1644 this->_M_impl
._M_start
= _M_allocate(_S_check_init_len(
1645 static_cast<size_type
>(__n
), _M_get_Tp_allocator()));
1646 this->_M_impl
._M_end_of_storage
=
1647 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1648 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1651 // Called by the range constructor to implement [23.1.1]/9
1652 template<typename _InputIterator
>
1654 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1657 _M_range_initialize(__first
, __last
,
1658 std::__iterator_category(__first
));
1662 // Called by the second initialize_dispatch above
1663 template<typename _InputIterator
>
1664 _GLIBCXX20_CONSTEXPR
1666 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1667 std::input_iterator_tag
)
1670 for (; __first
!= __last
; ++__first
)
1671 #if __cplusplus >= 201103L
1672 emplace_back(*__first
);
1674 push_back(*__first
);
1678 __throw_exception_again
;
1682 // Called by the second initialize_dispatch above
1683 template<typename _ForwardIterator
>
1684 _GLIBCXX20_CONSTEXPR
1686 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1687 std::forward_iterator_tag
)
1689 const size_type __n
= std::distance(__first
, __last
);
1690 this->_M_impl
._M_start
1691 = this->_M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1692 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1693 this->_M_impl
._M_finish
=
1694 std::__uninitialized_copy_a(__first
, __last
,
1695 this->_M_impl
._M_start
,
1696 _M_get_Tp_allocator());
1699 // Called by the first initialize_dispatch above and by the
1700 // vector(n,value,a) constructor.
1701 _GLIBCXX20_CONSTEXPR
1703 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1705 this->_M_impl
._M_finish
=
1706 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1707 _M_get_Tp_allocator());
1710 #if __cplusplus >= 201103L
1711 // Called by the vector(n) constructor.
1712 _GLIBCXX20_CONSTEXPR
1714 _M_default_initialize(size_type __n
)
1716 this->_M_impl
._M_finish
=
1717 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1718 _M_get_Tp_allocator());
1722 // Internal assign functions follow. The *_aux functions do the actual
1723 // assignment work for the range versions.
1725 // Called by the range assign to implement [23.1.1]/9
1727 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1728 // 438. Ambiguity in the "do the right thing" clause
1729 template<typename _Integer
>
1730 _GLIBCXX20_CONSTEXPR
1732 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1733 { _M_fill_assign(__n
, __val
); }
1735 // Called by the range assign to implement [23.1.1]/9
1736 template<typename _InputIterator
>
1737 _GLIBCXX20_CONSTEXPR
1739 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1741 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1743 // Called by the second assign_dispatch above
1744 template<typename _InputIterator
>
1745 _GLIBCXX20_CONSTEXPR
1747 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1748 std::input_iterator_tag
);
1750 // Called by the second assign_dispatch above
1751 template<typename _ForwardIterator
>
1752 _GLIBCXX20_CONSTEXPR
1754 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1755 std::forward_iterator_tag
);
1757 // Called by assign(n,t), and the range assign when it turns out
1758 // to be the same thing.
1759 _GLIBCXX20_CONSTEXPR
1761 _M_fill_assign(size_type __n
, const value_type
& __val
);
1763 // Internal insert functions follow.
1765 // Called by the range insert to implement [23.1.1]/9
1767 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1768 // 438. Ambiguity in the "do the right thing" clause
1769 template<typename _Integer
>
1770 _GLIBCXX20_CONSTEXPR
1772 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1774 { _M_fill_insert(__pos
, __n
, __val
); }
1776 // Called by the range insert to implement [23.1.1]/9
1777 template<typename _InputIterator
>
1778 _GLIBCXX20_CONSTEXPR
1780 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1781 _InputIterator __last
, __false_type
)
1783 _M_range_insert(__pos
, __first
, __last
,
1784 std::__iterator_category(__first
));
1787 // Called by the second insert_dispatch above
1788 template<typename _InputIterator
>
1789 _GLIBCXX20_CONSTEXPR
1791 _M_range_insert(iterator __pos
, _InputIterator __first
,
1792 _InputIterator __last
, std::input_iterator_tag
);
1794 // Called by the second insert_dispatch above
1795 template<typename _ForwardIterator
>
1796 _GLIBCXX20_CONSTEXPR
1798 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1799 _ForwardIterator __last
, std::forward_iterator_tag
);
1801 // Called by insert(p,n,x), and the range insert when it turns out to be
1803 _GLIBCXX20_CONSTEXPR
1805 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1807 #if __cplusplus >= 201103L
1808 // Called by resize(n).
1809 _GLIBCXX20_CONSTEXPR
1811 _M_default_append(size_type __n
);
1813 _GLIBCXX20_CONSTEXPR
1818 #if __cplusplus < 201103L
1819 // Called by insert(p,x)
1821 _M_insert_aux(iterator __position
, const value_type
& __x
);
1824 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1827 _M_realloc_append(const value_type
& __x
);
1829 // A value_type object constructed with _Alloc_traits::construct()
1830 // and destroyed with _Alloc_traits::destroy().
1831 struct _Temporary_value
1833 template<typename
... _Args
>
1834 _GLIBCXX20_CONSTEXPR
explicit
1835 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1837 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1838 std::forward
<_Args
>(__args
)...);
1841 _GLIBCXX20_CONSTEXPR
1843 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1845 _GLIBCXX20_CONSTEXPR value_type
&
1846 _M_val() noexcept
{ return _M_storage
._M_val
; }
1849 _GLIBCXX20_CONSTEXPR _Tp
*
1850 _M_ptr() noexcept
{ return std::__addressof(_M_storage
._M_val
); }
1854 constexpr _Storage() : _M_byte() { }
1855 _GLIBCXX20_CONSTEXPR
~_Storage() { }
1856 _Storage
& operator=(const _Storage
&) = delete;
1857 unsigned char _M_byte
;
1862 _Storage _M_storage
;
1865 // Called by insert(p,x) and other functions when insertion needs to
1866 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1867 template<typename _Arg
>
1868 _GLIBCXX20_CONSTEXPR
1870 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1872 template<typename
... _Args
>
1873 _GLIBCXX20_CONSTEXPR
1875 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1877 template<typename
... _Args
>
1878 _GLIBCXX20_CONSTEXPR
1880 _M_realloc_append(_Args
&&... __args
);
1882 // Either move-construct at the end, or forward to _M_insert_aux.
1883 _GLIBCXX20_CONSTEXPR
1885 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1887 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1888 template<typename
... _Args
>
1889 _GLIBCXX20_CONSTEXPR
1891 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1893 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1894 _GLIBCXX20_CONSTEXPR
1896 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1897 { return _M_insert_rval(__position
, std::move(__v
)); }
1900 // Called by _M_fill_insert, _M_insert_aux etc.
1901 _GLIBCXX20_CONSTEXPR
1903 _M_check_len(size_type __n
, const char* __s
) const
1905 if (max_size() - size() < __n
)
1906 __throw_length_error(__N(__s
));
1908 const size_type __len
= size() + (std::max
)(size(), __n
);
1909 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1912 // Called by constructors to check initial size.
1913 static _GLIBCXX20_CONSTEXPR size_type
1914 _S_check_init_len(size_type __n
, const allocator_type
& __a
)
1916 if (__n
> _S_max_size(_Tp_alloc_type(__a
)))
1917 __throw_length_error(
1918 __N("cannot create std::vector larger than max_size()"));
1922 static _GLIBCXX20_CONSTEXPR size_type
1923 _S_max_size(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
1925 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1926 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1927 // (even if std::allocator_traits::max_size says we can).
1928 const size_t __diffmax
1929 = __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
1930 const size_t __allocmax
= _Alloc_traits::max_size(__a
);
1931 return (std::min
)(__diffmax
, __allocmax
);
1934 // Internal erase functions follow.
1936 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1938 _GLIBCXX20_CONSTEXPR
1940 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1942 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1944 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1945 _M_get_Tp_allocator());
1946 this->_M_impl
._M_finish
= __pos
;
1947 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1951 _GLIBCXX20_CONSTEXPR
1953 _M_erase(iterator __position
);
1955 _GLIBCXX20_CONSTEXPR
1957 _M_erase(iterator __first
, iterator __last
);
1959 #if __cplusplus >= 201103L
1961 // Constant-time move assignment when source object's memory can be
1962 // moved, either because the source's allocator will move too
1963 // or because the allocators are equal.
1964 _GLIBCXX20_CONSTEXPR
1966 _M_move_assign(vector
&& __x
, true_type
) noexcept
1968 vector
__tmp(get_allocator());
1969 this->_M_impl
._M_swap_data(__x
._M_impl
);
1970 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
1971 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1974 // Do move assignment when it might not be possible to move source
1975 // object's memory, resulting in a linear-time operation.
1976 _GLIBCXX20_CONSTEXPR
1978 _M_move_assign(vector
&& __x
, false_type
)
1980 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1981 _M_move_assign(std::move(__x
), true_type());
1984 // The rvalue's allocator cannot be moved and is not equal,
1985 // so we need to individually move each element.
1986 this->_M_assign_aux(std::make_move_iterator(__x
.begin()),
1987 std::make_move_iterator(__x
.end()),
1988 std::random_access_iterator_tag());
1994 template<typename _Up
>
1995 _GLIBCXX20_CONSTEXPR
1997 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
2000 #if __cplusplus >= 201103L
2001 template<typename _Ptr
>
2002 _GLIBCXX20_CONSTEXPR
2003 typename
std::pointer_traits
<_Ptr
>::element_type
*
2004 _M_data_ptr(_Ptr __ptr
) const
2005 { return empty() ? nullptr : std::__to_address(__ptr
); }
2007 template<typename _Up
>
2009 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
2012 template<typename _Ptr
>
2014 _M_data_ptr(_Ptr __ptr
)
2015 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
2017 template<typename _Ptr
>
2019 _M_data_ptr(_Ptr __ptr
) const
2020 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
2024 #if __cpp_deduction_guides >= 201606
2025 template<typename _InputIterator
, typename _ValT
2026 = typename iterator_traits
<_InputIterator
>::value_type
,
2027 typename _Allocator
= allocator
<_ValT
>,
2028 typename
= _RequireInputIter
<_InputIterator
>,
2029 typename
= _RequireAllocator
<_Allocator
>>
2030 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
2031 -> vector
<_ValT
, _Allocator
>;
2035 * @brief Vector equality comparison.
2036 * @param __x A %vector.
2037 * @param __y A %vector of the same type as @a __x.
2038 * @return True iff the size and elements of the vectors are equal.
2040 * This is an equivalence relation. It is linear in the size of the
2041 * vectors. Vectors are considered equivalent if their sizes are equal,
2042 * and if corresponding elements compare equal.
2044 template<typename _Tp
, typename _Alloc
>
2045 _GLIBCXX20_CONSTEXPR
2047 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2048 { return (__x
.size() == __y
.size()
2049 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
2051 #if __cpp_lib_three_way_comparison
2053 * @brief Vector ordering relation.
2054 * @param __x A `vector`.
2055 * @param __y A `vector` of the same type as `__x`.
2056 * @return A value indicating whether `__x` is less than, equal to,
2057 * greater than, or incomparable with `__y`.
2059 * See `std::lexicographical_compare_three_way()` for how the determination
2060 * is made. This operator is used to synthesize relational operators like
2063 template<typename _Tp
, typename _Alloc
>
2064 _GLIBCXX20_CONSTEXPR
2065 inline __detail::__synth3way_t
<_Tp
>
2066 operator<=>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2068 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
2069 __y
.begin(), __y
.end(),
2070 __detail::__synth3way
);
2074 * @brief Vector ordering relation.
2075 * @param __x A %vector.
2076 * @param __y A %vector of the same type as @a __x.
2077 * @return True iff @a __x is lexicographically less than @a __y.
2079 * This is a total ordering relation. It is linear in the size of the
2080 * vectors. The elements must be comparable with @c <.
2082 * See std::lexicographical_compare() for how the determination is made.
2084 template<typename _Tp
, typename _Alloc
>
2086 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2087 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2088 __y
.begin(), __y
.end()); }
2090 /// Based on operator==
2091 template<typename _Tp
, typename _Alloc
>
2093 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2094 { return !(__x
== __y
); }
2096 /// Based on operator<
2097 template<typename _Tp
, typename _Alloc
>
2099 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2100 { return __y
< __x
; }
2102 /// Based on operator<
2103 template<typename _Tp
, typename _Alloc
>
2105 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2106 { return !(__y
< __x
); }
2108 /// Based on operator<
2109 template<typename _Tp
, typename _Alloc
>
2111 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
2112 { return !(__x
< __y
); }
2113 #endif // three-way comparison
2115 /// See std::vector::swap().
2116 template<typename _Tp
, typename _Alloc
>
2117 _GLIBCXX20_CONSTEXPR
2119 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
2120 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2123 _GLIBCXX_END_NAMESPACE_CONTAINER
2125 #if __cplusplus >= 201703L
2126 namespace __detail::__variant
2128 template<typename
> struct _Never_valueless_alt
; // see <variant>
2130 // Provide the strong exception-safety guarantee when emplacing a
2131 // vector into a variant, but only if move assignment cannot throw.
2132 template<typename _Tp
, typename _Alloc
>
2133 struct _Never_valueless_alt
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
2134 : std::is_nothrow_move_assignable
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
2136 } // namespace __detail::__variant
2139 _GLIBCXX_END_NAMESPACE_VERSION
2142 #endif /* _STL_VECTOR_H */