1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_vector.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{vector}
57 #define _STL_VECTOR_H 1
59 #include <bits/stl_iterator_base_funcs.h>
60 #include <bits/functexcept.h>
61 #include <bits/concept_check.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
66 #include <debug/assertions.h>
68 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
70 __sanitizer_annotate_contiguous_container(const void*, const void*,
71 const void*, const void*);
74 namespace std
_GLIBCXX_VISIBILITY(default)
76 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
79 /// See bits/stl_deque.h's _Deque_base for an explanation.
80 template<typename _Tp
, typename _Alloc
>
83 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
84 rebind
<_Tp
>::other _Tp_alloc_type
;
85 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
88 struct _Vector_impl_data
92 pointer _M_end_of_storage
;
94 _Vector_impl_data() _GLIBCXX_NOEXCEPT
95 : _M_start(), _M_finish(), _M_end_of_storage()
98 #if __cplusplus >= 201103L
99 _Vector_impl_data(_Vector_impl_data
&& __x
) noexcept
100 : _M_start(__x
._M_start
), _M_finish(__x
._M_finish
),
101 _M_end_of_storage(__x
._M_end_of_storage
)
102 { __x
._M_start
= __x
._M_finish
= __x
._M_end_of_storage
= pointer(); }
106 _M_copy_data(_Vector_impl_data
const& __x
) _GLIBCXX_NOEXCEPT
108 _M_start
= __x
._M_start
;
109 _M_finish
= __x
._M_finish
;
110 _M_end_of_storage
= __x
._M_end_of_storage
;
114 _M_swap_data(_Vector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
116 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
117 // information used by TBAA.
118 _Vector_impl_data __tmp
;
119 __tmp
._M_copy_data(*this);
121 __x
._M_copy_data(__tmp
);
126 : public _Tp_alloc_type
, public _Vector_impl_data
128 _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
129 is_nothrow_default_constructible
<_Tp_alloc_type
>::value
)
133 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
134 : _Tp_alloc_type(__a
)
137 #if __cplusplus >= 201103L
138 // Not defaulted, to enforce noexcept(true) even when
139 // !is_nothrow_move_constructible<_Tp_alloc_type>.
140 _Vector_impl(_Vector_impl
&& __x
) noexcept
141 : _Tp_alloc_type(std::move(__x
)), _Vector_impl_data(std::move(__x
))
144 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
145 : _Tp_alloc_type(std::move(__a
))
148 _Vector_impl(_Tp_alloc_type
&& __a
, _Vector_impl
&& __rv
) noexcept
149 : _Tp_alloc_type(std::move(__a
)), _Vector_impl_data(std::move(__rv
))
153 #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
154 template<typename
= _Tp_alloc_type
>
157 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
158 ::size_type size_type
;
160 static void _S_shrink(_Vector_impl
&, size_type
) { }
161 static void _S_on_dealloc(_Vector_impl
&) { }
163 typedef _Vector_impl
& _Reinit
;
167 _Grow(_Vector_impl
&, size_type
) { }
168 void _M_grew(size_type
) { }
172 // Enable ASan annotations for memory obtained from std::allocator.
173 template<typename _Up
>
174 struct _Asan
<allocator
<_Up
> >
176 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>
177 ::size_type size_type
;
179 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
180 // mark end of valid region as __curr instead of __prev.
182 _S_adjust(_Vector_impl
& __impl
, pointer __prev
, pointer __curr
)
184 __sanitizer_annotate_contiguous_container(__impl
._M_start
,
185 __impl
._M_end_of_storage
, __prev
, __curr
);
189 _S_grow(_Vector_impl
& __impl
, size_type __n
)
190 { _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_finish
+ __n
); }
193 _S_shrink(_Vector_impl
& __impl
, size_type __n
)
194 { _S_adjust(__impl
, __impl
._M_finish
+ __n
, __impl
._M_finish
); }
197 _S_on_dealloc(_Vector_impl
& __impl
)
200 _S_adjust(__impl
, __impl
._M_finish
, __impl
._M_end_of_storage
);
203 // Used on reallocation to tell ASan unused capacity is invalid.
206 explicit _Reinit(_Vector_impl
& __impl
) : _M_impl(__impl
)
208 // Mark unused capacity as valid again before deallocating it.
209 _S_on_dealloc(_M_impl
);
214 // Mark unused capacity as invalid after reallocation.
215 if (_M_impl
._M_start
)
216 _S_adjust(_M_impl
, _M_impl
._M_end_of_storage
,
220 _Vector_impl
& _M_impl
;
222 #if __cplusplus >= 201103L
223 _Reinit(const _Reinit
&) = delete;
224 _Reinit
& operator=(const _Reinit
&) = delete;
228 // Tell ASan when unused capacity is initialized to be valid.
231 _Grow(_Vector_impl
& __impl
, size_type __n
)
232 : _M_impl(__impl
), _M_n(__n
)
233 { _S_grow(_M_impl
, __n
); }
235 ~_Grow() { if (_M_n
) _S_shrink(_M_impl
, _M_n
); }
237 void _M_grew(size_type __n
) { _M_n
-= __n
; }
239 #if __cplusplus >= 201103L
240 _Grow(const _Grow
&) = delete;
241 _Grow
& operator=(const _Grow
&) = delete;
244 _Vector_impl
& _M_impl
;
249 #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
250 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
251 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
252 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
253 typename _Base::_Vector_impl::template _Asan<>::_Grow \
254 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
255 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
256 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
257 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
258 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
259 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
260 #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
261 #define _GLIBCXX_ASAN_ANNOTATE_REINIT
262 #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
263 #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
264 #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
265 #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
266 #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
270 typedef _Alloc allocator_type
;
273 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
274 { return this->_M_impl
; }
276 const _Tp_alloc_type
&
277 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
278 { return this->_M_impl
; }
281 get_allocator() const _GLIBCXX_NOEXCEPT
282 { return allocator_type(_M_get_Tp_allocator()); }
284 #if __cplusplus >= 201103L
285 _Vector_base() = default;
290 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
293 // Kept for ABI compatibility.
294 #if !_GLIBCXX_INLINE_VERSION
295 _Vector_base(size_t __n
)
297 { _M_create_storage(__n
); }
300 _Vector_base(size_t __n
, const allocator_type
& __a
)
302 { _M_create_storage(__n
); }
304 #if __cplusplus >= 201103L
305 _Vector_base(_Vector_base
&&) = default;
307 // Kept for ABI compatibility.
308 # if !_GLIBCXX_INLINE_VERSION
309 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
310 : _M_impl(std::move(__a
)) { }
312 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
315 if (__x
.get_allocator() == __a
)
316 this->_M_impl
._M_swap_data(__x
._M_impl
);
319 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
320 _M_create_storage(__n
);
325 _Vector_base(const allocator_type
& __a
, _Vector_base
&& __x
)
326 : _M_impl(_Tp_alloc_type(__a
), std::move(__x
._M_impl
))
330 ~_Vector_base() _GLIBCXX_NOEXCEPT
332 _M_deallocate(_M_impl
._M_start
,
333 _M_impl
._M_end_of_storage
- _M_impl
._M_start
);
337 _Vector_impl _M_impl
;
340 _M_allocate(size_t __n
)
342 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
343 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
347 _M_deallocate(pointer __p
, size_t __n
)
349 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
351 _Tr::deallocate(_M_impl
, __p
, __n
);
356 _M_create_storage(size_t __n
)
358 this->_M_impl
._M_start
= this->_M_allocate(__n
);
359 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
360 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
365 * @brief A standard container which offers fixed time access to
366 * individual elements in any order.
370 * @tparam _Tp Type of element.
371 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
373 * Meets the requirements of a <a href="tables.html#65">container</a>, a
374 * <a href="tables.html#66">reversible container</a>, and a
375 * <a href="tables.html#67">sequence</a>, including the
376 * <a href="tables.html#68">optional sequence requirements</a> with the
377 * %exception of @c push_front and @c pop_front.
379 * In some terminology a %vector can be described as a dynamic
380 * C-style array, it offers fast and efficient access to individual
381 * elements in any order and saves the user from worrying about
382 * memory and size allocation. Subscripting ( @c [] ) access is
383 * also provided as with C-style arrays.
385 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
386 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
388 #ifdef _GLIBCXX_CONCEPT_CHECKS
389 // Concept requirements.
390 typedef typename
_Alloc::value_type _Alloc_value_type
;
391 # if __cplusplus < 201103L
392 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
394 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
397 #if __cplusplus >= 201103L
398 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
399 "std::vector must have a non-const, non-volatile value_type");
400 # if __cplusplus > 201703L || defined __STRICT_ANSI__
401 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
402 "std::vector must have the same value_type as its allocator");
406 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
407 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
408 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
411 typedef _Tp value_type
;
412 typedef typename
_Base::pointer pointer
;
413 typedef typename
_Alloc_traits::const_pointer const_pointer
;
414 typedef typename
_Alloc_traits::reference reference
;
415 typedef typename
_Alloc_traits::const_reference const_reference
;
416 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
417 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
419 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
420 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
421 typedef size_t size_type
;
422 typedef ptrdiff_t difference_type
;
423 typedef _Alloc allocator_type
;
426 #if __cplusplus >= 201103L
427 static constexpr bool
428 _S_nothrow_relocate(true_type
)
430 return noexcept(std::__relocate_a(std::declval
<pointer
>(),
431 std::declval
<pointer
>(),
432 std::declval
<pointer
>(),
433 std::declval
<_Tp_alloc_type
&>()));
436 static constexpr bool
437 _S_nothrow_relocate(false_type
)
440 static constexpr bool
443 // Instantiating std::__relocate_a might cause an error outside the
444 // immediate context (in __relocate_object_a's noexcept-specifier),
445 // so only do it if we know the type can be move-inserted into *this.
446 return _S_nothrow_relocate(__is_move_insertable
<_Tp_alloc_type
>{});
450 _S_do_relocate(pointer __first
, pointer __last
, pointer __result
,
451 _Tp_alloc_type
& __alloc
, true_type
) noexcept
453 return std::__relocate_a(__first
, __last
, __result
, __alloc
);
457 _S_do_relocate(pointer
, pointer
, pointer __result
,
458 _Tp_alloc_type
&, false_type
) noexcept
462 _S_relocate(pointer __first
, pointer __last
, pointer __result
,
463 _Tp_alloc_type
& __alloc
) noexcept
465 using __do_it
= __bool_constant
<_S_use_relocate()>;
466 return _S_do_relocate(__first
, __last
, __result
, __alloc
, __do_it
{});
471 using _Base::_M_allocate
;
472 using _Base::_M_deallocate
;
473 using _Base::_M_impl
;
474 using _Base::_M_get_Tp_allocator
;
477 // [23.2.4.1] construct/copy/destroy
478 // (assign() and get_allocator() are also listed in this section)
481 * @brief Creates a %vector with no elements.
483 #if __cplusplus >= 201103L
490 * @brief Creates a %vector with no elements.
491 * @param __a An allocator object.
494 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
497 #if __cplusplus >= 201103L
499 * @brief Creates a %vector with default constructed elements.
500 * @param __n The number of elements to initially create.
501 * @param __a An allocator.
503 * This constructor fills the %vector with @a __n default
504 * constructed elements.
507 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
508 : _Base(_S_check_init_len(__n
, __a
), __a
)
509 { _M_default_initialize(__n
); }
512 * @brief Creates a %vector with copies of an exemplar element.
513 * @param __n The number of elements to initially create.
514 * @param __value An element to copy.
515 * @param __a An allocator.
517 * This constructor fills the %vector with @a __n copies of @a __value.
519 vector(size_type __n
, const value_type
& __value
,
520 const allocator_type
& __a
= allocator_type())
521 : _Base(_S_check_init_len(__n
, __a
), __a
)
522 { _M_fill_initialize(__n
, __value
); }
525 * @brief Creates a %vector with copies of an exemplar element.
526 * @param __n The number of elements to initially create.
527 * @param __value An element to copy.
528 * @param __a An allocator.
530 * This constructor fills the %vector with @a __n copies of @a __value.
533 vector(size_type __n
, const value_type
& __value
= value_type(),
534 const allocator_type
& __a
= allocator_type())
535 : _Base(_S_check_init_len(__n
, __a
), __a
)
536 { _M_fill_initialize(__n
, __value
); }
540 * @brief %Vector copy constructor.
541 * @param __x A %vector of identical element and allocator types.
543 * All the elements of @a __x are copied, but any unused capacity in
544 * @a __x will not be copied
545 * (i.e. capacity() == size() in the new %vector).
547 * The newly-created %vector uses a copy of the allocator object used
548 * by @a __x (unless the allocator traits dictate a different object).
550 vector(const vector
& __x
)
552 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
554 this->_M_impl
._M_finish
=
555 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
556 this->_M_impl
._M_start
,
557 _M_get_Tp_allocator());
560 #if __cplusplus >= 201103L
562 * @brief %Vector move constructor.
564 * The newly-created %vector contains the exact contents of the
566 * The contents of the moved instance are a valid, but unspecified
569 vector(vector
&&) noexcept
= default;
571 /// Copy constructor with alternative allocator
572 vector(const vector
& __x
, const allocator_type
& __a
)
573 : _Base(__x
.size(), __a
)
575 this->_M_impl
._M_finish
=
576 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
577 this->_M_impl
._M_start
,
578 _M_get_Tp_allocator());
582 vector(vector
&& __rv
, const allocator_type
& __m
, true_type
) noexcept
583 : _Base(__m
, std::move(__rv
))
586 vector(vector
&& __rv
, const allocator_type
& __m
, false_type
)
589 if (__rv
.get_allocator() == __m
)
590 this->_M_impl
._M_swap_data(__rv
._M_impl
);
591 else if (!__rv
.empty())
593 this->_M_create_storage(__rv
.size());
594 this->_M_impl
._M_finish
=
595 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
596 this->_M_impl
._M_start
,
597 _M_get_Tp_allocator());
603 /// Move constructor with alternative allocator
604 vector(vector
&& __rv
, const allocator_type
& __m
)
606 vector(std::declval
<vector
&&>(), std::declval
<const allocator_type
&>(),
607 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
608 : vector(std::move(__rv
), __m
, typename
_Alloc_traits::is_always_equal
{})
612 * @brief Builds a %vector from an initializer list.
613 * @param __l An initializer_list.
614 * @param __a An allocator.
616 * Create a %vector consisting of copies of the elements in the
617 * initializer_list @a __l.
619 * This will call the element type's copy constructor N times
620 * (where N is @a __l.size()) and do no memory reallocation.
622 vector(initializer_list
<value_type
> __l
,
623 const allocator_type
& __a
= allocator_type())
626 _M_range_initialize(__l
.begin(), __l
.end(),
627 random_access_iterator_tag());
632 * @brief Builds a %vector from a range.
633 * @param __first An input iterator.
634 * @param __last An input iterator.
635 * @param __a An allocator.
637 * Create a %vector consisting of copies of the elements from
640 * If the iterators are forward, bidirectional, or
641 * random-access, then this will call the elements' copy
642 * constructor N times (where N is distance(first,last)) and do
643 * no memory reallocation. But if only input iterators are
644 * used, then this will do at most 2N calls to the copy
645 * constructor, and logN memory reallocations.
647 #if __cplusplus >= 201103L
648 template<typename _InputIterator
,
649 typename
= std::_RequireInputIter
<_InputIterator
>>
650 vector(_InputIterator __first
, _InputIterator __last
,
651 const allocator_type
& __a
= allocator_type())
654 _M_range_initialize(__first
, __last
,
655 std::__iterator_category(__first
));
658 template<typename _InputIterator
>
659 vector(_InputIterator __first
, _InputIterator __last
,
660 const allocator_type
& __a
= allocator_type())
663 // Check whether it's an integral type. If so, it's not an iterator.
664 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
665 _M_initialize_dispatch(__first
, __last
, _Integral());
670 * The dtor only erases the elements, and note that if the
671 * elements themselves are pointers, the pointed-to memory is
672 * not touched in any way. Managing the pointer is the user's
675 ~vector() _GLIBCXX_NOEXCEPT
677 std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
678 _M_get_Tp_allocator());
679 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
;
683 * @brief %Vector assignment operator.
684 * @param __x A %vector of identical element and allocator types.
686 * All the elements of @a __x are copied, but any unused capacity in
687 * @a __x will not be copied.
689 * Whether the allocator is copied depends on the allocator traits.
692 operator=(const vector
& __x
);
694 #if __cplusplus >= 201103L
696 * @brief %Vector move assignment operator.
697 * @param __x A %vector of identical element and allocator types.
699 * The contents of @a __x are moved into this %vector (without copying,
700 * if the allocators permit it).
701 * Afterwards @a __x is a valid, but unspecified %vector.
703 * Whether the allocator is moved depends on the allocator traits.
706 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
708 constexpr bool __move_storage
=
709 _Alloc_traits::_S_propagate_on_move_assign()
710 || _Alloc_traits::_S_always_equal();
711 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
716 * @brief %Vector list assignment operator.
717 * @param __l An initializer_list.
719 * This function fills a %vector with copies of the elements in the
720 * initializer list @a __l.
722 * Note that the assignment completely changes the %vector and
723 * that the resulting %vector's size is the same as the number
724 * of elements assigned.
727 operator=(initializer_list
<value_type
> __l
)
729 this->_M_assign_aux(__l
.begin(), __l
.end(),
730 random_access_iterator_tag());
736 * @brief Assigns a given value to a %vector.
737 * @param __n Number of elements to be assigned.
738 * @param __val Value to be assigned.
740 * This function fills a %vector with @a __n copies of the given
741 * value. Note that the assignment completely changes the
742 * %vector and that the resulting %vector's size is the same as
743 * the number of elements assigned.
746 assign(size_type __n
, const value_type
& __val
)
747 { _M_fill_assign(__n
, __val
); }
750 * @brief Assigns a range to a %vector.
751 * @param __first An input iterator.
752 * @param __last An input iterator.
754 * This function fills a %vector with copies of the elements in the
755 * range [__first,__last).
757 * Note that the assignment completely changes the %vector and
758 * that the resulting %vector's size is the same as the number
759 * of elements assigned.
761 #if __cplusplus >= 201103L
762 template<typename _InputIterator
,
763 typename
= std::_RequireInputIter
<_InputIterator
>>
765 assign(_InputIterator __first
, _InputIterator __last
)
766 { _M_assign_dispatch(__first
, __last
, __false_type()); }
768 template<typename _InputIterator
>
770 assign(_InputIterator __first
, _InputIterator __last
)
772 // Check whether it's an integral type. If so, it's not an iterator.
773 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
774 _M_assign_dispatch(__first
, __last
, _Integral());
778 #if __cplusplus >= 201103L
780 * @brief Assigns an initializer list to a %vector.
781 * @param __l An initializer_list.
783 * This function fills a %vector with copies of the elements in the
784 * initializer list @a __l.
786 * Note that the assignment completely changes the %vector and
787 * that the resulting %vector's size is the same as the number
788 * of elements assigned.
791 assign(initializer_list
<value_type
> __l
)
793 this->_M_assign_aux(__l
.begin(), __l
.end(),
794 random_access_iterator_tag());
798 /// Get a copy of the memory allocation object.
799 using _Base::get_allocator
;
803 * Returns a read/write iterator that points to the first
804 * element in the %vector. Iteration is done in ordinary
808 begin() _GLIBCXX_NOEXCEPT
809 { return iterator(this->_M_impl
._M_start
); }
812 * Returns a read-only (constant) iterator that points to the
813 * first element in the %vector. Iteration is done in ordinary
817 begin() const _GLIBCXX_NOEXCEPT
818 { return const_iterator(this->_M_impl
._M_start
); }
821 * Returns a read/write iterator that points one past the last
822 * element in the %vector. Iteration is done in ordinary
826 end() _GLIBCXX_NOEXCEPT
827 { return iterator(this->_M_impl
._M_finish
); }
830 * Returns a read-only (constant) iterator that points one past
831 * the last element in the %vector. Iteration is done in
832 * ordinary element order.
835 end() const _GLIBCXX_NOEXCEPT
836 { return const_iterator(this->_M_impl
._M_finish
); }
839 * Returns a read/write reverse iterator that points to the
840 * last element in the %vector. Iteration is done in reverse
844 rbegin() _GLIBCXX_NOEXCEPT
845 { return reverse_iterator(end()); }
848 * Returns a read-only (constant) reverse iterator that points
849 * to the last element in the %vector. Iteration is done in
850 * reverse element order.
852 const_reverse_iterator
853 rbegin() const _GLIBCXX_NOEXCEPT
854 { return const_reverse_iterator(end()); }
857 * Returns a read/write reverse iterator that points to one
858 * before the first element in the %vector. Iteration is done
859 * in reverse element order.
862 rend() _GLIBCXX_NOEXCEPT
863 { return reverse_iterator(begin()); }
866 * Returns a read-only (constant) reverse iterator that points
867 * to one before the first element in the %vector. Iteration
868 * is done in reverse element order.
870 const_reverse_iterator
871 rend() const _GLIBCXX_NOEXCEPT
872 { return const_reverse_iterator(begin()); }
874 #if __cplusplus >= 201103L
876 * Returns a read-only (constant) iterator that points to the
877 * first element in the %vector. Iteration is done in ordinary
881 cbegin() const noexcept
882 { return const_iterator(this->_M_impl
._M_start
); }
885 * Returns a read-only (constant) iterator that points one past
886 * the last element in the %vector. Iteration is done in
887 * ordinary element order.
890 cend() const noexcept
891 { return const_iterator(this->_M_impl
._M_finish
); }
894 * Returns a read-only (constant) reverse iterator that points
895 * to the last element in the %vector. Iteration is done in
896 * reverse element order.
898 const_reverse_iterator
899 crbegin() const noexcept
900 { return const_reverse_iterator(end()); }
903 * Returns a read-only (constant) reverse iterator that points
904 * to one before the first element in the %vector. Iteration
905 * is done in reverse element order.
907 const_reverse_iterator
908 crend() const noexcept
909 { return const_reverse_iterator(begin()); }
912 // [23.2.4.2] capacity
913 /** Returns the number of elements in the %vector. */
915 size() const _GLIBCXX_NOEXCEPT
916 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
918 /** Returns the size() of the largest possible %vector. */
920 max_size() const _GLIBCXX_NOEXCEPT
921 { return _S_max_size(_M_get_Tp_allocator()); }
923 #if __cplusplus >= 201103L
925 * @brief Resizes the %vector to the specified number of elements.
926 * @param __new_size Number of elements the %vector should contain.
928 * This function will %resize the %vector to the specified
929 * number of elements. If the number is smaller than the
930 * %vector's current size the %vector is truncated, otherwise
931 * default constructed elements are appended.
934 resize(size_type __new_size
)
936 if (__new_size
> size())
937 _M_default_append(__new_size
- size());
938 else if (__new_size
< size())
939 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
943 * @brief Resizes the %vector to the specified number of elements.
944 * @param __new_size Number of elements the %vector should contain.
945 * @param __x Data with which new elements should be populated.
947 * This function will %resize the %vector to the specified
948 * number of elements. If the number is smaller than the
949 * %vector's current size the %vector is truncated, otherwise
950 * the %vector is extended and new elements are populated with
954 resize(size_type __new_size
, const value_type
& __x
)
956 if (__new_size
> size())
957 _M_fill_insert(end(), __new_size
- size(), __x
);
958 else if (__new_size
< size())
959 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
963 * @brief Resizes the %vector to the specified number of elements.
964 * @param __new_size Number of elements the %vector should contain.
965 * @param __x Data with which new elements should be populated.
967 * This function will %resize the %vector to the specified
968 * number of elements. If the number is smaller than the
969 * %vector's current size the %vector is truncated, otherwise
970 * the %vector is extended and new elements are populated with
974 resize(size_type __new_size
, value_type __x
= value_type())
976 if (__new_size
> size())
977 _M_fill_insert(end(), __new_size
- size(), __x
);
978 else if (__new_size
< size())
979 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
983 #if __cplusplus >= 201103L
984 /** A non-binding request to reduce capacity() to size(). */
987 { _M_shrink_to_fit(); }
991 * Returns the total number of elements that the %vector can
992 * hold before needing to allocate more memory.
995 capacity() const _GLIBCXX_NOEXCEPT
996 { return size_type(this->_M_impl
._M_end_of_storage
997 - this->_M_impl
._M_start
); }
1000 * Returns true if the %vector is empty. (Thus begin() would
1003 _GLIBCXX_NODISCARD
bool
1004 empty() const _GLIBCXX_NOEXCEPT
1005 { return begin() == end(); }
1008 * @brief Attempt to preallocate enough memory for specified number of
1010 * @param __n Number of elements required.
1011 * @throw std::length_error If @a n exceeds @c max_size().
1013 * This function attempts to reserve enough memory for the
1014 * %vector to hold the specified number of elements. If the
1015 * number requested is more than max_size(), length_error is
1018 * The advantage of this function is that if optimal code is a
1019 * necessity and the user can determine the number of elements
1020 * that will be required, the user can reserve the memory in
1021 * %advance, and thus prevent a possible reallocation of memory
1022 * and copying of %vector data.
1025 reserve(size_type __n
);
1029 * @brief Subscript access to the data contained in the %vector.
1030 * @param __n The index of the element for which data should be
1032 * @return Read/write reference to data.
1034 * This operator allows for easy, array-style, data access.
1035 * Note that data access with this operator is unchecked and
1036 * out_of_range lookups are not defined. (For checked lookups
1040 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1042 __glibcxx_requires_subscript(__n
);
1043 return *(this->_M_impl
._M_start
+ __n
);
1047 * @brief Subscript access to the data contained in the %vector.
1048 * @param __n The index of the element for which data should be
1050 * @return Read-only (constant) reference to data.
1052 * This operator allows for easy, array-style, data access.
1053 * Note that data access with this operator is unchecked and
1054 * out_of_range lookups are not defined. (For checked lookups
1058 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1060 __glibcxx_requires_subscript(__n
);
1061 return *(this->_M_impl
._M_start
+ __n
);
1065 /// Safety check used only from at().
1067 _M_range_check(size_type __n
) const
1069 if (__n
>= this->size())
1070 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1071 "(which is %zu) >= this->size() "
1078 * @brief Provides access to the data contained in the %vector.
1079 * @param __n The index of the element for which data should be
1081 * @return Read/write reference to data.
1082 * @throw std::out_of_range If @a __n is an invalid index.
1084 * This function provides for safer data access. The parameter
1085 * is first checked that it is in the range of the vector. The
1086 * function throws out_of_range if the check fails.
1091 _M_range_check(__n
);
1092 return (*this)[__n
];
1096 * @brief Provides access to the data contained in the %vector.
1097 * @param __n The index of the element for which data should be
1099 * @return Read-only (constant) reference to data.
1100 * @throw std::out_of_range If @a __n is an invalid index.
1102 * This function provides for safer data access. The parameter
1103 * is first checked that it is in the range of the vector. The
1104 * function throws out_of_range if the check fails.
1107 at(size_type __n
) const
1109 _M_range_check(__n
);
1110 return (*this)[__n
];
1114 * Returns a read/write reference to the data at the first
1115 * element of the %vector.
1118 front() _GLIBCXX_NOEXCEPT
1120 __glibcxx_requires_nonempty();
1125 * Returns a read-only (constant) reference to the data at the first
1126 * element of the %vector.
1129 front() const _GLIBCXX_NOEXCEPT
1131 __glibcxx_requires_nonempty();
1136 * Returns a read/write reference to the data at the last
1137 * element of the %vector.
1140 back() _GLIBCXX_NOEXCEPT
1142 __glibcxx_requires_nonempty();
1143 return *(end() - 1);
1147 * Returns a read-only (constant) reference to the data at the
1148 * last element of the %vector.
1151 back() const _GLIBCXX_NOEXCEPT
1153 __glibcxx_requires_nonempty();
1154 return *(end() - 1);
1157 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1158 // DR 464. Suggestion for new member functions in standard containers.
1161 * Returns a pointer such that [data(), data() + size()) is a valid
1162 * range. For a non-empty %vector, data() == &front().
1165 data() _GLIBCXX_NOEXCEPT
1166 { return _M_data_ptr(this->_M_impl
._M_start
); }
1169 data() const _GLIBCXX_NOEXCEPT
1170 { return _M_data_ptr(this->_M_impl
._M_start
); }
1172 // [23.2.4.3] modifiers
1174 * @brief Add data to the end of the %vector.
1175 * @param __x Data to be added.
1177 * This is a typical stack operation. The function creates an
1178 * element at the end of the %vector and assigns the given data
1179 * to it. Due to the nature of a %vector this operation can be
1180 * done in constant time if the %vector has preallocated space
1184 push_back(const value_type
& __x
)
1186 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
1188 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1189 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
1191 ++this->_M_impl
._M_finish
;
1192 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1195 _M_realloc_insert(end(), __x
);
1198 #if __cplusplus >= 201103L
1200 push_back(value_type
&& __x
)
1201 { emplace_back(std::move(__x
)); }
1203 template<typename
... _Args
>
1204 #if __cplusplus > 201402L
1209 emplace_back(_Args
&&... __args
);
1213 * @brief Removes last element.
1215 * This is a typical stack operation. It shrinks the %vector by one.
1217 * Note that no data is returned, and if the last element's
1218 * data is needed, it should be retrieved before pop_back() is
1222 pop_back() _GLIBCXX_NOEXCEPT
1224 __glibcxx_requires_nonempty();
1225 --this->_M_impl
._M_finish
;
1226 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
1227 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1230 #if __cplusplus >= 201103L
1232 * @brief Inserts an object in %vector before specified iterator.
1233 * @param __position A const_iterator into the %vector.
1234 * @param __args Arguments.
1235 * @return An iterator that points to the inserted data.
1237 * This function will insert an object of type T constructed
1238 * with T(std::forward<Args>(args)...) before the specified location.
1239 * Note that this kind of operation could be expensive for a %vector
1240 * and if it is frequently used the user should consider using
1243 template<typename
... _Args
>
1245 emplace(const_iterator __position
, _Args
&&... __args
)
1246 { return _M_emplace_aux(__position
, std::forward
<_Args
>(__args
)...); }
1249 * @brief Inserts given value into %vector before specified iterator.
1250 * @param __position A const_iterator into the %vector.
1251 * @param __x Data to be inserted.
1252 * @return An iterator that points to the inserted data.
1254 * This function will insert a copy of the given value before
1255 * the specified location. Note that this kind of operation
1256 * could be expensive for a %vector and if it is frequently
1257 * used the user should consider using std::list.
1260 insert(const_iterator __position
, const value_type
& __x
);
1263 * @brief Inserts given value into %vector before specified iterator.
1264 * @param __position An iterator into the %vector.
1265 * @param __x Data to be inserted.
1266 * @return An iterator that points to the inserted data.
1268 * This function will insert a copy of the given value before
1269 * the specified location. Note that this kind of operation
1270 * could be expensive for a %vector and if it is frequently
1271 * used the user should consider using std::list.
1274 insert(iterator __position
, const value_type
& __x
);
1277 #if __cplusplus >= 201103L
1279 * @brief Inserts given rvalue into %vector before specified iterator.
1280 * @param __position A const_iterator into the %vector.
1281 * @param __x Data to be inserted.
1282 * @return An iterator that points to the inserted data.
1284 * This function will insert a copy of the given rvalue before
1285 * the specified location. Note that this kind of operation
1286 * could be expensive for a %vector and if it is frequently
1287 * used the user should consider using std::list.
1290 insert(const_iterator __position
, value_type
&& __x
)
1291 { return _M_insert_rval(__position
, std::move(__x
)); }
1294 * @brief Inserts an initializer_list into the %vector.
1295 * @param __position An iterator into the %vector.
1296 * @param __l An initializer_list.
1298 * This function will insert copies of the data in the
1299 * initializer_list @a l into the %vector before the location
1300 * specified by @a position.
1302 * Note that this kind of operation could be expensive for a
1303 * %vector and if it is frequently used the user should
1304 * consider using std::list.
1307 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1309 auto __offset
= __position
- cbegin();
1310 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1311 std::random_access_iterator_tag());
1312 return begin() + __offset
;
1316 #if __cplusplus >= 201103L
1318 * @brief Inserts a number of copies of given data into the %vector.
1319 * @param __position A const_iterator into the %vector.
1320 * @param __n Number of elements to be inserted.
1321 * @param __x Data to be inserted.
1322 * @return An iterator that points to the inserted data.
1324 * This function will insert a specified number of copies of
1325 * the given data before the location specified by @a position.
1327 * Note that this kind of operation could be expensive for a
1328 * %vector and if it is frequently used the user should
1329 * consider using std::list.
1332 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1334 difference_type __offset
= __position
- cbegin();
1335 _M_fill_insert(begin() + __offset
, __n
, __x
);
1336 return begin() + __offset
;
1340 * @brief Inserts a number of copies of given data into the %vector.
1341 * @param __position An iterator into the %vector.
1342 * @param __n Number of elements to be inserted.
1343 * @param __x Data to be inserted.
1345 * This function will insert a specified number of copies of
1346 * the given data before the location specified by @a position.
1348 * Note that this kind of operation could be expensive for a
1349 * %vector and if it is frequently used the user should
1350 * consider using std::list.
1353 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1354 { _M_fill_insert(__position
, __n
, __x
); }
1357 #if __cplusplus >= 201103L
1359 * @brief Inserts a range into the %vector.
1360 * @param __position A const_iterator into the %vector.
1361 * @param __first An input iterator.
1362 * @param __last An input iterator.
1363 * @return An iterator that points to the inserted data.
1365 * This function will insert copies of the data in the range
1366 * [__first,__last) into the %vector before the location specified
1369 * Note that this kind of operation could be expensive for a
1370 * %vector and if it is frequently used the user should
1371 * consider using std::list.
1373 template<typename _InputIterator
,
1374 typename
= std::_RequireInputIter
<_InputIterator
>>
1376 insert(const_iterator __position
, _InputIterator __first
,
1377 _InputIterator __last
)
1379 difference_type __offset
= __position
- cbegin();
1380 _M_insert_dispatch(begin() + __offset
,
1381 __first
, __last
, __false_type());
1382 return begin() + __offset
;
1386 * @brief Inserts a range into the %vector.
1387 * @param __position An iterator into the %vector.
1388 * @param __first An input iterator.
1389 * @param __last An input iterator.
1391 * This function will insert copies of the data in the range
1392 * [__first,__last) into the %vector before the location specified
1395 * Note that this kind of operation could be expensive for a
1396 * %vector and if it is frequently used the user should
1397 * consider using std::list.
1399 template<typename _InputIterator
>
1401 insert(iterator __position
, _InputIterator __first
,
1402 _InputIterator __last
)
1404 // Check whether it's an integral type. If so, it's not an iterator.
1405 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1406 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1411 * @brief Remove element at given position.
1412 * @param __position Iterator pointing to element to be erased.
1413 * @return An iterator pointing to the next element (or end()).
1415 * This function will erase the element at the given position and thus
1416 * shorten the %vector by one.
1418 * Note This operation could be expensive and if it is
1419 * frequently used the user should consider using std::list.
1420 * The user is also cautioned that this function only erases
1421 * the element, and that if the element is itself a pointer,
1422 * the pointed-to memory is not touched in any way. Managing
1423 * the pointer is the user's responsibility.
1426 #if __cplusplus >= 201103L
1427 erase(const_iterator __position
)
1428 { return _M_erase(begin() + (__position
- cbegin())); }
1430 erase(iterator __position
)
1431 { return _M_erase(__position
); }
1435 * @brief Remove a range of elements.
1436 * @param __first Iterator pointing to the first element to be erased.
1437 * @param __last Iterator pointing to one past the last element to be
1439 * @return An iterator pointing to the element pointed to by @a __last
1440 * prior to erasing (or end()).
1442 * This function will erase the elements in the range
1443 * [__first,__last) and shorten the %vector accordingly.
1445 * Note This operation could be expensive and if it is
1446 * frequently used the user should consider using std::list.
1447 * The user is also cautioned that this function only erases
1448 * the elements, and that if the elements themselves are
1449 * pointers, the pointed-to memory is not touched in any way.
1450 * Managing the pointer is the user's responsibility.
1453 #if __cplusplus >= 201103L
1454 erase(const_iterator __first
, const_iterator __last
)
1456 const auto __beg
= begin();
1457 const auto __cbeg
= cbegin();
1458 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1461 erase(iterator __first
, iterator __last
)
1462 { return _M_erase(__first
, __last
); }
1466 * @brief Swaps data with another %vector.
1467 * @param __x A %vector of the same element and allocator types.
1469 * This exchanges the elements between two vectors in constant time.
1470 * (Three pointers, so it should be quite fast.)
1471 * Note that the global std::swap() function is specialized such that
1472 * std::swap(v1,v2) will feed to this function.
1474 * Whether the allocators are swapped depends on the allocator traits.
1477 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1479 #if __cplusplus >= 201103L
1480 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1481 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1483 this->_M_impl
._M_swap_data(__x
._M_impl
);
1484 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1485 __x
._M_get_Tp_allocator());
1489 * Erases all the elements. Note that this function only erases the
1490 * elements, and that if the elements themselves are pointers, the
1491 * pointed-to memory is not touched in any way. Managing the pointer is
1492 * the user's responsibility.
1495 clear() _GLIBCXX_NOEXCEPT
1496 { _M_erase_at_end(this->_M_impl
._M_start
); }
1500 * Memory expansion handler. Uses the member allocation function to
1501 * obtain @a n bytes of memory, and then copies [first,last) into it.
1503 template<typename _ForwardIterator
>
1505 _M_allocate_and_copy(size_type __n
,
1506 _ForwardIterator __first
, _ForwardIterator __last
)
1508 pointer __result
= this->_M_allocate(__n
);
1511 std::__uninitialized_copy_a(__first
, __last
, __result
,
1512 _M_get_Tp_allocator());
1517 _M_deallocate(__result
, __n
);
1518 __throw_exception_again
;
1523 // Internal constructor functions follow.
1525 // Called by the range constructor to implement [23.1.1]/9
1527 #if __cplusplus < 201103L
1528 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1529 // 438. Ambiguity in the "do the right thing" clause
1530 template<typename _Integer
>
1532 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1534 this->_M_impl
._M_start
= _M_allocate(_S_check_init_len(
1535 static_cast<size_type
>(__n
), _M_get_Tp_allocator()));
1536 this->_M_impl
._M_end_of_storage
=
1537 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1538 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1541 // Called by the range constructor to implement [23.1.1]/9
1542 template<typename _InputIterator
>
1544 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1547 _M_range_initialize(__first
, __last
,
1548 std::__iterator_category(__first
));
1552 // Called by the second initialize_dispatch above
1553 template<typename _InputIterator
>
1555 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1556 std::input_iterator_tag
)
1559 for (; __first
!= __last
; ++__first
)
1560 #if __cplusplus >= 201103L
1561 emplace_back(*__first
);
1563 push_back(*__first
);
1567 __throw_exception_again
;
1571 // Called by the second initialize_dispatch above
1572 template<typename _ForwardIterator
>
1574 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1575 std::forward_iterator_tag
)
1577 const size_type __n
= std::distance(__first
, __last
);
1578 this->_M_impl
._M_start
1579 = this->_M_allocate(_S_check_init_len(__n
, _M_get_Tp_allocator()));
1580 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1581 this->_M_impl
._M_finish
=
1582 std::__uninitialized_copy_a(__first
, __last
,
1583 this->_M_impl
._M_start
,
1584 _M_get_Tp_allocator());
1587 // Called by the first initialize_dispatch above and by the
1588 // vector(n,value,a) constructor.
1590 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1592 this->_M_impl
._M_finish
=
1593 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1594 _M_get_Tp_allocator());
1597 #if __cplusplus >= 201103L
1598 // Called by the vector(n) constructor.
1600 _M_default_initialize(size_type __n
)
1602 this->_M_impl
._M_finish
=
1603 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1604 _M_get_Tp_allocator());
1608 // Internal assign functions follow. The *_aux functions do the actual
1609 // assignment work for the range versions.
1611 // Called by the range assign to implement [23.1.1]/9
1613 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1614 // 438. Ambiguity in the "do the right thing" clause
1615 template<typename _Integer
>
1617 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1618 { _M_fill_assign(__n
, __val
); }
1620 // Called by the range assign to implement [23.1.1]/9
1621 template<typename _InputIterator
>
1623 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1625 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1627 // Called by the second assign_dispatch above
1628 template<typename _InputIterator
>
1630 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1631 std::input_iterator_tag
);
1633 // Called by the second assign_dispatch above
1634 template<typename _ForwardIterator
>
1636 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1637 std::forward_iterator_tag
);
1639 // Called by assign(n,t), and the range assign when it turns out
1640 // to be the same thing.
1642 _M_fill_assign(size_type __n
, const value_type
& __val
);
1644 // Internal insert functions follow.
1646 // Called by the range insert to implement [23.1.1]/9
1648 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1649 // 438. Ambiguity in the "do the right thing" clause
1650 template<typename _Integer
>
1652 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1654 { _M_fill_insert(__pos
, __n
, __val
); }
1656 // Called by the range insert to implement [23.1.1]/9
1657 template<typename _InputIterator
>
1659 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1660 _InputIterator __last
, __false_type
)
1662 _M_range_insert(__pos
, __first
, __last
,
1663 std::__iterator_category(__first
));
1666 // Called by the second insert_dispatch above
1667 template<typename _InputIterator
>
1669 _M_range_insert(iterator __pos
, _InputIterator __first
,
1670 _InputIterator __last
, std::input_iterator_tag
);
1672 // Called by the second insert_dispatch above
1673 template<typename _ForwardIterator
>
1675 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1676 _ForwardIterator __last
, std::forward_iterator_tag
);
1678 // Called by insert(p,n,x), and the range insert when it turns out to be
1681 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1683 #if __cplusplus >= 201103L
1684 // Called by resize(n).
1686 _M_default_append(size_type __n
);
1692 #if __cplusplus < 201103L
1693 // Called by insert(p,x)
1695 _M_insert_aux(iterator __position
, const value_type
& __x
);
1698 _M_realloc_insert(iterator __position
, const value_type
& __x
);
1700 // A value_type object constructed with _Alloc_traits::construct()
1701 // and destroyed with _Alloc_traits::destroy().
1702 struct _Temporary_value
1704 template<typename
... _Args
>
1706 _Temporary_value(vector
* __vec
, _Args
&&... __args
) : _M_this(__vec
)
1708 _Alloc_traits::construct(_M_this
->_M_impl
, _M_ptr(),
1709 std::forward
<_Args
>(__args
)...);
1713 { _Alloc_traits::destroy(_M_this
->_M_impl
, _M_ptr()); }
1716 _M_val() { return *_M_ptr(); }
1720 _M_ptr() { return reinterpret_cast<_Tp
*>(&__buf
); }
1723 typename aligned_storage
<sizeof(_Tp
), alignof(_Tp
)>::type __buf
;
1726 // Called by insert(p,x) and other functions when insertion needs to
1727 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
1728 template<typename _Arg
>
1730 _M_insert_aux(iterator __position
, _Arg
&& __arg
);
1732 template<typename
... _Args
>
1734 _M_realloc_insert(iterator __position
, _Args
&&... __args
);
1736 // Either move-construct at the end, or forward to _M_insert_aux.
1738 _M_insert_rval(const_iterator __position
, value_type
&& __v
);
1740 // Try to emplace at the end, otherwise forward to _M_insert_aux.
1741 template<typename
... _Args
>
1743 _M_emplace_aux(const_iterator __position
, _Args
&&... __args
);
1745 // Emplacing an rvalue of the correct type can use _M_insert_rval.
1747 _M_emplace_aux(const_iterator __position
, value_type
&& __v
)
1748 { return _M_insert_rval(__position
, std::move(__v
)); }
1751 // Called by _M_fill_insert, _M_insert_aux etc.
1753 _M_check_len(size_type __n
, const char* __s
) const
1755 if (max_size() - size() < __n
)
1756 __throw_length_error(__N(__s
));
1758 const size_type __len
= size() + (std::max
)(size(), __n
);
1759 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1762 // Called by constructors to check initial size.
1764 _S_check_init_len(size_type __n
, const allocator_type
& __a
)
1766 if (__n
> _S_max_size(_Tp_alloc_type(__a
)))
1767 __throw_length_error(
1768 __N("cannot create std::vector larger than max_size()"));
1773 _S_max_size(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
1775 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
1776 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
1777 // (even if std::allocator_traits::max_size says we can).
1778 const size_t __diffmax
1779 = __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
1780 const size_t __allocmax
= _Alloc_traits::max_size(__a
);
1781 return (std::min
)(__diffmax
, __allocmax
);
1784 // Internal erase functions follow.
1786 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1789 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1791 if (size_type __n
= this->_M_impl
._M_finish
- __pos
)
1793 std::_Destroy(__pos
, this->_M_impl
._M_finish
,
1794 _M_get_Tp_allocator());
1795 this->_M_impl
._M_finish
= __pos
;
1796 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n
);
1801 _M_erase(iterator __position
);
1804 _M_erase(iterator __first
, iterator __last
);
1806 #if __cplusplus >= 201103L
1808 // Constant-time move assignment when source object's memory can be
1809 // moved, either because the source's allocator will move too
1810 // or because the allocators are equal.
1812 _M_move_assign(vector
&& __x
, true_type
) noexcept
1814 vector
__tmp(get_allocator());
1815 this->_M_impl
._M_swap_data(__x
._M_impl
);
1816 __tmp
._M_impl
._M_swap_data(__x
._M_impl
);
1817 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1820 // Do move assignment when it might not be possible to move source
1821 // object's memory, resulting in a linear-time operation.
1823 _M_move_assign(vector
&& __x
, false_type
)
1825 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1826 _M_move_assign(std::move(__x
), true_type());
1829 // The rvalue's allocator cannot be moved and is not equal,
1830 // so we need to individually move each element.
1831 this->_M_assign_aux(std::make_move_iterator(__x
.begin()),
1832 std::make_move_iterator(__x
.end()),
1833 std::random_access_iterator_tag());
1839 template<typename _Up
>
1841 _M_data_ptr(_Up
* __ptr
) const _GLIBCXX_NOEXCEPT
1844 #if __cplusplus >= 201103L
1845 template<typename _Ptr
>
1846 typename
std::pointer_traits
<_Ptr
>::element_type
*
1847 _M_data_ptr(_Ptr __ptr
) const
1848 { return empty() ? nullptr : std::__to_address(__ptr
); }
1850 template<typename _Up
>
1852 _M_data_ptr(_Up
* __ptr
) _GLIBCXX_NOEXCEPT
1855 template<typename _Ptr
>
1857 _M_data_ptr(_Ptr __ptr
)
1858 { return empty() ? (value_type
*)0 : __ptr
.operator->(); }
1860 template<typename _Ptr
>
1862 _M_data_ptr(_Ptr __ptr
) const
1863 { return empty() ? (const value_type
*)0 : __ptr
.operator->(); }
1867 #if __cpp_deduction_guides >= 201606
1868 template<typename _InputIterator
, typename _ValT
1869 = typename iterator_traits
<_InputIterator
>::value_type
,
1870 typename _Allocator
= allocator
<_ValT
>,
1871 typename
= _RequireInputIter
<_InputIterator
>,
1872 typename
= _RequireAllocator
<_Allocator
>>
1873 vector(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1874 -> vector
<_ValT
, _Allocator
>;
1878 * @brief Vector equality comparison.
1879 * @param __x A %vector.
1880 * @param __y A %vector of the same type as @a __x.
1881 * @return True iff the size and elements of the vectors are equal.
1883 * This is an equivalence relation. It is linear in the size of the
1884 * vectors. Vectors are considered equivalent if their sizes are equal,
1885 * and if corresponding elements compare equal.
1887 template<typename _Tp
, typename _Alloc
>
1889 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1890 { return (__x
.size() == __y
.size()
1891 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1894 * @brief Vector ordering relation.
1895 * @param __x A %vector.
1896 * @param __y A %vector of the same type as @a __x.
1897 * @return True iff @a __x is lexicographically less than @a __y.
1899 * This is a total ordering relation. It is linear in the size of the
1900 * vectors. The elements must be comparable with @c <.
1902 * See std::lexicographical_compare() for how the determination is made.
1904 template<typename _Tp
, typename _Alloc
>
1906 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1907 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1908 __y
.begin(), __y
.end()); }
1910 /// Based on operator==
1911 template<typename _Tp
, typename _Alloc
>
1913 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1914 { return !(__x
== __y
); }
1916 /// Based on operator<
1917 template<typename _Tp
, typename _Alloc
>
1919 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1920 { return __y
< __x
; }
1922 /// Based on operator<
1923 template<typename _Tp
, typename _Alloc
>
1925 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1926 { return !(__y
< __x
); }
1928 /// Based on operator<
1929 template<typename _Tp
, typename _Alloc
>
1931 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1932 { return !(__x
< __y
); }
1934 /// See std::vector::swap().
1935 template<typename _Tp
, typename _Alloc
>
1937 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1938 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1941 _GLIBCXX_END_NAMESPACE_CONTAINER
1943 #if __cplusplus >= 201703L
1944 namespace __detail::__variant
1946 template<typename
> struct _Never_valueless_alt
; // see <variant>
1948 // Provide the strong exception-safety guarantee when emplacing a
1949 // vector into a variant, but only if move assignment cannot throw.
1950 template<typename _Tp
, typename _Alloc
>
1951 struct _Never_valueless_alt
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
1952 : std::is_nothrow_move_assignable
<_GLIBCXX_STD_C::vector
<_Tp
, _Alloc
>>
1954 } // namespace __detail::__variant
1957 _GLIBCXX_END_NAMESPACE_VERSION
1960 #endif /* _STL_VECTOR_H */