1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 namespace std
_GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
72 /// See bits/stl_deque.h's _Deque_base for an explanation.
73 template<typename _Tp
, typename _Alloc
>
76 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
77 rebind
<_Tp
>::other _Tp_alloc_type
;
78 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
82 : public _Tp_alloc_type
86 pointer _M_end_of_storage
;
89 : _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
92 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
93 : _Tp_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage()
96 #if __cplusplus >= 201103L
97 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
98 : _Tp_alloc_type(std::move(__a
)),
99 _M_start(), _M_finish(), _M_end_of_storage()
103 void _M_swap_data(_Vector_impl
& __x
) _GLIBCXX_NOEXCEPT
105 std::swap(_M_start
, __x
._M_start
);
106 std::swap(_M_finish
, __x
._M_finish
);
107 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
112 typedef _Alloc allocator_type
;
115 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
116 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
118 const _Tp_alloc_type
&
119 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
120 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
123 get_allocator() const _GLIBCXX_NOEXCEPT
124 { return allocator_type(_M_get_Tp_allocator()); }
129 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
132 _Vector_base(size_t __n
)
134 { _M_create_storage(__n
); }
136 _Vector_base(size_t __n
, const allocator_type
& __a
)
138 { _M_create_storage(__n
); }
140 #if __cplusplus >= 201103L
141 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
142 : _M_impl(std::move(__a
)) { }
144 _Vector_base(_Vector_base
&& __x
) noexcept
145 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
146 { this->_M_impl
._M_swap_data(__x
._M_impl
); }
148 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
151 if (__x
.get_allocator() == __a
)
152 this->_M_impl
._M_swap_data(__x
._M_impl
);
155 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
156 _M_create_storage(__n
);
161 ~_Vector_base() _GLIBCXX_NOEXCEPT
162 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
163 - this->_M_impl
._M_start
); }
166 _Vector_impl _M_impl
;
169 _M_allocate(size_t __n
)
171 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
172 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
176 _M_deallocate(pointer __p
, size_t __n
)
178 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
180 _Tr::deallocate(_M_impl
, __p
, __n
);
185 _M_create_storage(size_t __n
)
187 this->_M_impl
._M_start
= this->_M_allocate(__n
);
188 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
189 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
195 * @brief A standard container which offers fixed time access to
196 * individual elements in any order.
200 * @tparam _Tp Type of element.
201 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
203 * Meets the requirements of a <a href="tables.html#65">container</a>, a
204 * <a href="tables.html#66">reversible container</a>, and a
205 * <a href="tables.html#67">sequence</a>, including the
206 * <a href="tables.html#68">optional sequence requirements</a> with the
207 * %exception of @c push_front and @c pop_front.
209 * In some terminology a %vector can be described as a dynamic
210 * C-style array, it offers fast and efficient access to individual
211 * elements in any order and saves the user from worrying about
212 * memory and size allocation. Subscripting ( @c [] ) access is
213 * also provided as with C-style arrays.
215 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
216 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
218 // Concept requirements.
219 typedef typename
_Alloc::value_type _Alloc_value_type
;
220 #if __cplusplus < 201103L
221 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
223 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
225 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
226 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
227 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
230 typedef _Tp value_type
;
231 typedef typename
_Base::pointer pointer
;
232 typedef typename
_Alloc_traits::const_pointer const_pointer
;
233 typedef typename
_Alloc_traits::reference reference
;
234 typedef typename
_Alloc_traits::const_reference const_reference
;
235 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
236 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
238 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
239 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
240 typedef size_t size_type
;
241 typedef ptrdiff_t difference_type
;
242 typedef _Alloc allocator_type
;
245 using _Base::_M_allocate
;
246 using _Base::_M_deallocate
;
247 using _Base::_M_impl
;
248 using _Base::_M_get_Tp_allocator
;
251 // [23.2.4.1] construct/copy/destroy
252 // (assign() and get_allocator() are also listed in this section)
255 * @brief Creates a %vector with no elements.
258 #if __cplusplus >= 201103L
259 noexcept(is_nothrow_default_constructible
<_Alloc
>::value
)
264 * @brief Creates a %vector with no elements.
265 * @param __a An allocator object.
268 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
271 #if __cplusplus >= 201103L
273 * @brief Creates a %vector with default constructed elements.
274 * @param __n The number of elements to initially create.
275 * @param __a An allocator.
277 * This constructor fills the %vector with @a __n default
278 * constructed elements.
281 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
283 { _M_default_initialize(__n
); }
286 * @brief Creates a %vector with copies of an exemplar element.
287 * @param __n The number of elements to initially create.
288 * @param __value An element to copy.
289 * @param __a An allocator.
291 * This constructor fills the %vector with @a __n copies of @a __value.
293 vector(size_type __n
, const value_type
& __value
,
294 const allocator_type
& __a
= allocator_type())
296 { _M_fill_initialize(__n
, __value
); }
299 * @brief Creates a %vector with copies of an exemplar element.
300 * @param __n The number of elements to initially create.
301 * @param __value An element to copy.
302 * @param __a An allocator.
304 * This constructor fills the %vector with @a __n copies of @a __value.
307 vector(size_type __n
, const value_type
& __value
= value_type(),
308 const allocator_type
& __a
= allocator_type())
310 { _M_fill_initialize(__n
, __value
); }
314 * @brief %Vector copy constructor.
315 * @param __x A %vector of identical element and allocator types.
317 * The newly-created %vector uses a copy of the allocation
318 * object used by @a __x. All the elements of @a __x are copied,
319 * but any extra memory in
320 * @a __x (for fast expansion) will not be copied.
322 vector(const vector
& __x
)
324 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
326 this->_M_impl
._M_finish
=
327 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
328 this->_M_impl
._M_start
,
329 _M_get_Tp_allocator());
332 #if __cplusplus >= 201103L
334 * @brief %Vector move constructor.
335 * @param __x A %vector of identical element and allocator types.
337 * The newly-created %vector contains the exact contents of @a __x.
338 * The contents of @a __x are a valid, but unspecified %vector.
340 vector(vector
&& __x
) noexcept
341 : _Base(std::move(__x
)) { }
343 /// Copy constructor with alternative allocator
344 vector(const vector
& __x
, const allocator_type
& __a
)
345 : _Base(__x
.size(), __a
)
347 this->_M_impl
._M_finish
=
348 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
349 this->_M_impl
._M_start
,
350 _M_get_Tp_allocator());
353 /// Move constructor with alternative allocator
354 vector(vector
&& __rv
, const allocator_type
& __m
)
355 noexcept(_Alloc_traits::_S_always_equal())
356 : _Base(std::move(__rv
), __m
)
358 if (__rv
.get_allocator() != __m
)
360 this->_M_impl
._M_finish
=
361 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
362 this->_M_impl
._M_start
,
363 _M_get_Tp_allocator());
369 * @brief Builds a %vector from an initializer list.
370 * @param __l An initializer_list.
371 * @param __a An allocator.
373 * Create a %vector consisting of copies of the elements in the
374 * initializer_list @a __l.
376 * This will call the element type's copy constructor N times
377 * (where N is @a __l.size()) and do no memory reallocation.
379 vector(initializer_list
<value_type
> __l
,
380 const allocator_type
& __a
= allocator_type())
383 _M_range_initialize(__l
.begin(), __l
.end(),
384 random_access_iterator_tag());
389 * @brief Builds a %vector from a range.
390 * @param __first An input iterator.
391 * @param __last An input iterator.
392 * @param __a An allocator.
394 * Create a %vector consisting of copies of the elements from
397 * If the iterators are forward, bidirectional, or
398 * random-access, then this will call the elements' copy
399 * constructor N times (where N is distance(first,last)) and do
400 * no memory reallocation. But if only input iterators are
401 * used, then this will do at most 2N calls to the copy
402 * constructor, and logN memory reallocations.
404 #if __cplusplus >= 201103L
405 template<typename _InputIterator
,
406 typename
= std::_RequireInputIter
<_InputIterator
>>
407 vector(_InputIterator __first
, _InputIterator __last
,
408 const allocator_type
& __a
= allocator_type())
410 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
412 template<typename _InputIterator
>
413 vector(_InputIterator __first
, _InputIterator __last
,
414 const allocator_type
& __a
= allocator_type())
417 // Check whether it's an integral type. If so, it's not an iterator.
418 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
419 _M_initialize_dispatch(__first
, __last
, _Integral());
424 * The dtor only erases the elements, and note that if the
425 * elements themselves are pointers, the pointed-to memory is
426 * not touched in any way. Managing the pointer is the user's
429 ~vector() _GLIBCXX_NOEXCEPT
430 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
431 _M_get_Tp_allocator()); }
434 * @brief %Vector assignment operator.
435 * @param __x A %vector of identical element and allocator types.
437 * All the elements of @a __x are copied, but any extra memory in
438 * @a __x (for fast expansion) will not be copied. Unlike the
439 * copy constructor, the allocator object is not copied.
442 operator=(const vector
& __x
);
444 #if __cplusplus >= 201103L
446 * @brief %Vector move assignment operator.
447 * @param __x A %vector of identical element and allocator types.
449 * The contents of @a __x are moved into this %vector (without copying,
450 * if the allocators permit it).
451 * @a __x is a valid, but unspecified %vector.
454 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
456 constexpr bool __move_storage
=
457 _Alloc_traits::_S_propagate_on_move_assign()
458 || _Alloc_traits::_S_always_equal();
459 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
464 * @brief %Vector list assignment operator.
465 * @param __l An initializer_list.
467 * This function fills a %vector with copies of the elements in the
468 * initializer list @a __l.
470 * Note that the assignment completely changes the %vector and
471 * that the resulting %vector's size is the same as the number
472 * of elements assigned. Old data may be lost.
475 operator=(initializer_list
<value_type
> __l
)
477 this->_M_assign_aux(__l
.begin(), __l
.end(),
478 random_access_iterator_tag());
484 * @brief Assigns a given value to a %vector.
485 * @param __n Number of elements to be assigned.
486 * @param __val Value to be assigned.
488 * This function fills a %vector with @a __n copies of the given
489 * value. Note that the assignment completely changes the
490 * %vector and that the resulting %vector's size is the same as
491 * the number of elements assigned. Old data may be lost.
494 assign(size_type __n
, const value_type
& __val
)
495 { _M_fill_assign(__n
, __val
); }
498 * @brief Assigns a range to a %vector.
499 * @param __first An input iterator.
500 * @param __last An input iterator.
502 * This function fills a %vector with copies of the elements in the
503 * range [__first,__last).
505 * Note that the assignment completely changes the %vector and
506 * that the resulting %vector's size is the same as the number
507 * of elements assigned. Old data may be lost.
509 #if __cplusplus >= 201103L
510 template<typename _InputIterator
,
511 typename
= std::_RequireInputIter
<_InputIterator
>>
513 assign(_InputIterator __first
, _InputIterator __last
)
514 { _M_assign_dispatch(__first
, __last
, __false_type()); }
516 template<typename _InputIterator
>
518 assign(_InputIterator __first
, _InputIterator __last
)
520 // Check whether it's an integral type. If so, it's not an iterator.
521 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
522 _M_assign_dispatch(__first
, __last
, _Integral());
526 #if __cplusplus >= 201103L
528 * @brief Assigns an initializer list to a %vector.
529 * @param __l An initializer_list.
531 * This function fills a %vector with copies of the elements in the
532 * initializer list @a __l.
534 * Note that the assignment completely changes the %vector and
535 * that the resulting %vector's size is the same as the number
536 * of elements assigned. Old data may be lost.
539 assign(initializer_list
<value_type
> __l
)
541 this->_M_assign_aux(__l
.begin(), __l
.end(),
542 random_access_iterator_tag());
546 /// Get a copy of the memory allocation object.
547 using _Base::get_allocator
;
551 * Returns a read/write iterator that points to the first
552 * element in the %vector. Iteration is done in ordinary
556 begin() _GLIBCXX_NOEXCEPT
557 { return iterator(this->_M_impl
._M_start
); }
560 * Returns a read-only (constant) iterator that points to the
561 * first element in the %vector. Iteration is done in ordinary
565 begin() const _GLIBCXX_NOEXCEPT
566 { return const_iterator(this->_M_impl
._M_start
); }
569 * Returns a read/write iterator that points one past the last
570 * element in the %vector. Iteration is done in ordinary
574 end() _GLIBCXX_NOEXCEPT
575 { return iterator(this->_M_impl
._M_finish
); }
578 * Returns a read-only (constant) iterator that points one past
579 * the last element in the %vector. Iteration is done in
580 * ordinary element order.
583 end() const _GLIBCXX_NOEXCEPT
584 { return const_iterator(this->_M_impl
._M_finish
); }
587 * Returns a read/write reverse iterator that points to the
588 * last element in the %vector. Iteration is done in reverse
592 rbegin() _GLIBCXX_NOEXCEPT
593 { return reverse_iterator(end()); }
596 * Returns a read-only (constant) reverse iterator that points
597 * to the last element in the %vector. Iteration is done in
598 * reverse element order.
600 const_reverse_iterator
601 rbegin() const _GLIBCXX_NOEXCEPT
602 { return const_reverse_iterator(end()); }
605 * Returns a read/write reverse iterator that points to one
606 * before the first element in the %vector. Iteration is done
607 * in reverse element order.
610 rend() _GLIBCXX_NOEXCEPT
611 { return reverse_iterator(begin()); }
614 * Returns a read-only (constant) reverse iterator that points
615 * to one before the first element in the %vector. Iteration
616 * is done in reverse element order.
618 const_reverse_iterator
619 rend() const _GLIBCXX_NOEXCEPT
620 { return const_reverse_iterator(begin()); }
622 #if __cplusplus >= 201103L
624 * Returns a read-only (constant) iterator that points to the
625 * first element in the %vector. Iteration is done in ordinary
629 cbegin() const noexcept
630 { return const_iterator(this->_M_impl
._M_start
); }
633 * Returns a read-only (constant) iterator that points one past
634 * the last element in the %vector. Iteration is done in
635 * ordinary element order.
638 cend() const noexcept
639 { return const_iterator(this->_M_impl
._M_finish
); }
642 * Returns a read-only (constant) reverse iterator that points
643 * to the last element in the %vector. Iteration is done in
644 * reverse element order.
646 const_reverse_iterator
647 crbegin() const noexcept
648 { return const_reverse_iterator(end()); }
651 * Returns a read-only (constant) reverse iterator that points
652 * to one before the first element in the %vector. Iteration
653 * is done in reverse element order.
655 const_reverse_iterator
656 crend() const noexcept
657 { return const_reverse_iterator(begin()); }
660 // [23.2.4.2] capacity
661 /** Returns the number of elements in the %vector. */
663 size() const _GLIBCXX_NOEXCEPT
664 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
666 /** Returns the size() of the largest possible %vector. */
668 max_size() const _GLIBCXX_NOEXCEPT
669 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
671 #if __cplusplus >= 201103L
673 * @brief Resizes the %vector to the specified number of elements.
674 * @param __new_size Number of elements the %vector should contain.
676 * This function will %resize the %vector to the specified
677 * number of elements. If the number is smaller than the
678 * %vector's current size the %vector is truncated, otherwise
679 * default constructed elements are appended.
682 resize(size_type __new_size
)
684 if (__new_size
> size())
685 _M_default_append(__new_size
- size());
686 else if (__new_size
< size())
687 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
691 * @brief Resizes the %vector to the specified number of elements.
692 * @param __new_size Number of elements the %vector should contain.
693 * @param __x Data with which new elements should be populated.
695 * This function will %resize the %vector to the specified
696 * number of elements. If the number is smaller than the
697 * %vector's current size the %vector is truncated, otherwise
698 * the %vector is extended and new elements are populated with
702 resize(size_type __new_size
, const value_type
& __x
)
704 if (__new_size
> size())
705 _M_fill_insert(end(), __new_size
- size(), __x
);
706 else if (__new_size
< size())
707 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
711 * @brief Resizes the %vector to the specified number of elements.
712 * @param __new_size Number of elements the %vector should contain.
713 * @param __x Data with which new elements should be populated.
715 * This function will %resize the %vector to the specified
716 * number of elements. If the number is smaller than the
717 * %vector's current size the %vector is truncated, otherwise
718 * the %vector is extended and new elements are populated with
722 resize(size_type __new_size
, value_type __x
= value_type())
724 if (__new_size
> size())
725 _M_fill_insert(end(), __new_size
- size(), __x
);
726 else if (__new_size
< size())
727 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
731 #if __cplusplus >= 201103L
732 /** A non-binding request to reduce capacity() to size(). */
735 { _M_shrink_to_fit(); }
739 * Returns the total number of elements that the %vector can
740 * hold before needing to allocate more memory.
743 capacity() const _GLIBCXX_NOEXCEPT
744 { return size_type(this->_M_impl
._M_end_of_storage
745 - this->_M_impl
._M_start
); }
748 * Returns true if the %vector is empty. (Thus begin() would
752 empty() const _GLIBCXX_NOEXCEPT
753 { return begin() == end(); }
756 * @brief Attempt to preallocate enough memory for specified number of
758 * @param __n Number of elements required.
759 * @throw std::length_error If @a n exceeds @c max_size().
761 * This function attempts to reserve enough memory for the
762 * %vector to hold the specified number of elements. If the
763 * number requested is more than max_size(), length_error is
766 * The advantage of this function is that if optimal code is a
767 * necessity and the user can determine the number of elements
768 * that will be required, the user can reserve the memory in
769 * %advance, and thus prevent a possible reallocation of memory
770 * and copying of %vector data.
773 reserve(size_type __n
);
777 * @brief Subscript access to the data contained in the %vector.
778 * @param __n The index of the element for which data should be
780 * @return Read/write reference to data.
782 * This operator allows for easy, array-style, data access.
783 * Note that data access with this operator is unchecked and
784 * out_of_range lookups are not defined. (For checked lookups
788 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
790 __glibcxx_requires_subscript(__n
);
791 return *(this->_M_impl
._M_start
+ __n
);
795 * @brief Subscript access to the data contained in the %vector.
796 * @param __n The index of the element for which data should be
798 * @return Read-only (constant) reference to data.
800 * This operator allows for easy, array-style, data access.
801 * Note that data access with this operator is unchecked and
802 * out_of_range lookups are not defined. (For checked lookups
806 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
808 __glibcxx_requires_subscript(__n
);
809 return *(this->_M_impl
._M_start
+ __n
);
813 /// Safety check used only from at().
815 _M_range_check(size_type __n
) const
817 if (__n
>= this->size())
818 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
819 "(which is %zu) >= this->size() "
826 * @brief Provides access to the data contained in the %vector.
827 * @param __n The index of the element for which data should be
829 * @return Read/write reference to data.
830 * @throw std::out_of_range If @a __n is an invalid index.
832 * This function provides for safer data access. The parameter
833 * is first checked that it is in the range of the vector. The
834 * function throws out_of_range if the check fails.
844 * @brief Provides access to the data contained in the %vector.
845 * @param __n The index of the element for which data should be
847 * @return Read-only (constant) reference to data.
848 * @throw std::out_of_range If @a __n is an invalid index.
850 * This function provides for safer data access. The parameter
851 * is first checked that it is in the range of the vector. The
852 * function throws out_of_range if the check fails.
855 at(size_type __n
) const
862 * Returns a read/write reference to the data at the first
863 * element of the %vector.
866 front() _GLIBCXX_NOEXCEPT
868 __glibcxx_requires_nonempty();
873 * Returns a read-only (constant) reference to the data at the first
874 * element of the %vector.
877 front() const _GLIBCXX_NOEXCEPT
879 __glibcxx_requires_nonempty();
884 * Returns a read/write reference to the data at the last
885 * element of the %vector.
888 back() _GLIBCXX_NOEXCEPT
890 __glibcxx_requires_nonempty();
895 * Returns a read-only (constant) reference to the data at the
896 * last element of the %vector.
899 back() const _GLIBCXX_NOEXCEPT
901 __glibcxx_requires_nonempty();
905 // _GLIBCXX_RESOLVE_LIB_DEFECTS
906 // DR 464. Suggestion for new member functions in standard containers.
909 * Returns a pointer such that [data(), data() + size()) is a valid
910 * range. For a non-empty %vector, data() == &front().
912 #if __cplusplus >= 201103L
917 data() _GLIBCXX_NOEXCEPT
918 { return _M_data_ptr(this->_M_impl
._M_start
); }
920 #if __cplusplus >= 201103L
925 data() const _GLIBCXX_NOEXCEPT
926 { return _M_data_ptr(this->_M_impl
._M_start
); }
928 // [23.2.4.3] modifiers
930 * @brief Add data to the end of the %vector.
931 * @param __x Data to be added.
933 * This is a typical stack operation. The function creates an
934 * element at the end of the %vector and assigns the given data
935 * to it. Due to the nature of a %vector this operation can be
936 * done in constant time if the %vector has preallocated space
940 push_back(const value_type
& __x
)
942 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
944 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
946 ++this->_M_impl
._M_finish
;
949 #if __cplusplus >= 201103L
950 _M_emplace_back_aux(__x
);
952 _M_insert_aux(end(), __x
);
956 #if __cplusplus >= 201103L
958 push_back(value_type
&& __x
)
959 { emplace_back(std::move(__x
)); }
961 template<typename
... _Args
>
963 emplace_back(_Args
&&... __args
);
967 * @brief Removes last element.
969 * This is a typical stack operation. It shrinks the %vector by one.
971 * Note that no data is returned, and if the last element's
972 * data is needed, it should be retrieved before pop_back() is
976 pop_back() _GLIBCXX_NOEXCEPT
978 __glibcxx_requires_nonempty();
979 --this->_M_impl
._M_finish
;
980 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
983 #if __cplusplus >= 201103L
985 * @brief Inserts an object in %vector before specified iterator.
986 * @param __position A const_iterator into the %vector.
987 * @param __args Arguments.
988 * @return An iterator that points to the inserted data.
990 * This function will insert an object of type T constructed
991 * with T(std::forward<Args>(args)...) before the specified location.
992 * Note that this kind of operation could be expensive for a %vector
993 * and if it is frequently used the user should consider using
996 template<typename
... _Args
>
998 emplace(const_iterator __position
, _Args
&&... __args
);
1001 * @brief Inserts given value into %vector before specified iterator.
1002 * @param __position A const_iterator into the %vector.
1003 * @param __x Data to be inserted.
1004 * @return An iterator that points to the inserted data.
1006 * This function will insert a copy of the given value before
1007 * the specified location. Note that this kind of operation
1008 * could be expensive for a %vector and if it is frequently
1009 * used the user should consider using std::list.
1012 insert(const_iterator __position
, const value_type
& __x
);
1015 * @brief Inserts given value into %vector before specified iterator.
1016 * @param __position An iterator into the %vector.
1017 * @param __x Data to be inserted.
1018 * @return An iterator that points to the inserted data.
1020 * This function will insert a copy of the given value before
1021 * the specified location. Note that this kind of operation
1022 * could be expensive for a %vector and if it is frequently
1023 * used the user should consider using std::list.
1026 insert(iterator __position
, const value_type
& __x
);
1029 #if __cplusplus >= 201103L
1031 * @brief Inserts given rvalue into %vector before specified iterator.
1032 * @param __position A const_iterator into the %vector.
1033 * @param __x Data to be inserted.
1034 * @return An iterator that points to the inserted data.
1036 * This function will insert a copy of the given rvalue before
1037 * the specified location. Note that this kind of operation
1038 * could be expensive for a %vector and if it is frequently
1039 * used the user should consider using std::list.
1042 insert(const_iterator __position
, value_type
&& __x
)
1043 { return emplace(__position
, std::move(__x
)); }
1046 * @brief Inserts an initializer_list into the %vector.
1047 * @param __position An iterator into the %vector.
1048 * @param __l An initializer_list.
1050 * This function will insert copies of the data in the
1051 * initializer_list @a l into the %vector before the location
1052 * specified by @a position.
1054 * Note that this kind of operation could be expensive for a
1055 * %vector and if it is frequently used the user should
1056 * consider using std::list.
1059 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1061 auto __offset
= __position
- cbegin();
1062 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1063 std::random_access_iterator_tag());
1064 return begin() + __offset
;
1068 #if __cplusplus >= 201103L
1070 * @brief Inserts a number of copies of given data into the %vector.
1071 * @param __position A const_iterator into the %vector.
1072 * @param __n Number of elements to be inserted.
1073 * @param __x Data to be inserted.
1074 * @return An iterator that points to the inserted data.
1076 * This function will insert a specified number of copies of
1077 * the given data before the location specified by @a position.
1079 * Note that this kind of operation could be expensive for a
1080 * %vector and if it is frequently used the user should
1081 * consider using std::list.
1084 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1086 difference_type __offset
= __position
- cbegin();
1087 _M_fill_insert(begin() + __offset
, __n
, __x
);
1088 return begin() + __offset
;
1092 * @brief Inserts a number of copies of given data into the %vector.
1093 * @param __position An iterator into the %vector.
1094 * @param __n Number of elements to be inserted.
1095 * @param __x Data to be inserted.
1097 * This function will insert a specified number of copies of
1098 * the given data before the location specified by @a position.
1100 * Note that this kind of operation could be expensive for a
1101 * %vector and if it is frequently used the user should
1102 * consider using std::list.
1105 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1106 { _M_fill_insert(__position
, __n
, __x
); }
1109 #if __cplusplus >= 201103L
1111 * @brief Inserts a range into the %vector.
1112 * @param __position A const_iterator into the %vector.
1113 * @param __first An input iterator.
1114 * @param __last An input iterator.
1115 * @return An iterator that points to the inserted data.
1117 * This function will insert copies of the data in the range
1118 * [__first,__last) into the %vector before the location specified
1121 * Note that this kind of operation could be expensive for a
1122 * %vector and if it is frequently used the user should
1123 * consider using std::list.
1125 template<typename _InputIterator
,
1126 typename
= std::_RequireInputIter
<_InputIterator
>>
1128 insert(const_iterator __position
, _InputIterator __first
,
1129 _InputIterator __last
)
1131 difference_type __offset
= __position
- cbegin();
1132 _M_insert_dispatch(begin() + __offset
,
1133 __first
, __last
, __false_type());
1134 return begin() + __offset
;
1138 * @brief Inserts a range into the %vector.
1139 * @param __position An iterator into the %vector.
1140 * @param __first An input iterator.
1141 * @param __last An input iterator.
1143 * This function will insert copies of the data in the range
1144 * [__first,__last) into the %vector before the location specified
1147 * Note that this kind of operation could be expensive for a
1148 * %vector and if it is frequently used the user should
1149 * consider using std::list.
1151 template<typename _InputIterator
>
1153 insert(iterator __position
, _InputIterator __first
,
1154 _InputIterator __last
)
1156 // Check whether it's an integral type. If so, it's not an iterator.
1157 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1158 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1163 * @brief Remove element at given position.
1164 * @param __position Iterator pointing to element to be erased.
1165 * @return An iterator pointing to the next element (or end()).
1167 * This function will erase the element at the given position and thus
1168 * shorten the %vector by one.
1170 * Note This operation could be expensive and if it is
1171 * frequently used the user should consider using std::list.
1172 * The user is also cautioned that this function only erases
1173 * the element, and that if the element is itself a pointer,
1174 * the pointed-to memory is not touched in any way. Managing
1175 * the pointer is the user's responsibility.
1178 #if __cplusplus >= 201103L
1179 erase(const_iterator __position
)
1180 { return _M_erase(begin() + (__position
- cbegin())); }
1182 erase(iterator __position
)
1183 { return _M_erase(__position
); }
1187 * @brief Remove a range of elements.
1188 * @param __first Iterator pointing to the first element to be erased.
1189 * @param __last Iterator pointing to one past the last element to be
1191 * @return An iterator pointing to the element pointed to by @a __last
1192 * prior to erasing (or end()).
1194 * This function will erase the elements in the range
1195 * [__first,__last) and shorten the %vector accordingly.
1197 * Note This operation could be expensive and if it is
1198 * frequently used the user should consider using std::list.
1199 * The user is also cautioned that this function only erases
1200 * the elements, and that if the elements themselves are
1201 * pointers, the pointed-to memory is not touched in any way.
1202 * Managing the pointer is the user's responsibility.
1205 #if __cplusplus >= 201103L
1206 erase(const_iterator __first
, const_iterator __last
)
1208 const auto __beg
= begin();
1209 const auto __cbeg
= cbegin();
1210 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1213 erase(iterator __first
, iterator __last
)
1214 { return _M_erase(__first
, __last
); }
1218 * @brief Swaps data with another %vector.
1219 * @param __x A %vector of the same element and allocator types.
1221 * This exchanges the elements between two vectors in constant time.
1222 * (Three pointers, so it should be quite fast.)
1223 * Note that the global std::swap() function is specialized such that
1224 * std::swap(v1,v2) will feed to this function.
1227 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1229 #if __cplusplus >= 201103L
1230 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1231 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1233 this->_M_impl
._M_swap_data(__x
._M_impl
);
1234 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1235 __x
._M_get_Tp_allocator());
1239 * Erases all the elements. Note that this function only erases the
1240 * elements, and that if the elements themselves are pointers, the
1241 * pointed-to memory is not touched in any way. Managing the pointer is
1242 * the user's responsibility.
1245 clear() _GLIBCXX_NOEXCEPT
1246 { _M_erase_at_end(this->_M_impl
._M_start
); }
1250 * Memory expansion handler. Uses the member allocation function to
1251 * obtain @a n bytes of memory, and then copies [first,last) into it.
1253 template<typename _ForwardIterator
>
1255 _M_allocate_and_copy(size_type __n
,
1256 _ForwardIterator __first
, _ForwardIterator __last
)
1258 pointer __result
= this->_M_allocate(__n
);
1261 std::__uninitialized_copy_a(__first
, __last
, __result
,
1262 _M_get_Tp_allocator());
1267 _M_deallocate(__result
, __n
);
1268 __throw_exception_again
;
1273 // Internal constructor functions follow.
1275 // Called by the range constructor to implement [23.1.1]/9
1277 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1278 // 438. Ambiguity in the "do the right thing" clause
1279 template<typename _Integer
>
1281 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1283 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1284 this->_M_impl
._M_end_of_storage
=
1285 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1286 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1289 // Called by the range constructor to implement [23.1.1]/9
1290 template<typename _InputIterator
>
1292 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1295 typedef typename
std::iterator_traits
<_InputIterator
>::
1296 iterator_category _IterCategory
;
1297 _M_range_initialize(__first
, __last
, _IterCategory());
1300 // Called by the second initialize_dispatch above
1301 template<typename _InputIterator
>
1303 _M_range_initialize(_InputIterator __first
,
1304 _InputIterator __last
, std::input_iterator_tag
)
1306 for (; __first
!= __last
; ++__first
)
1307 #if __cplusplus >= 201103L
1308 emplace_back(*__first
);
1310 push_back(*__first
);
1314 // Called by the second initialize_dispatch above
1315 template<typename _ForwardIterator
>
1317 _M_range_initialize(_ForwardIterator __first
,
1318 _ForwardIterator __last
, std::forward_iterator_tag
)
1320 const size_type __n
= std::distance(__first
, __last
);
1321 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1322 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1323 this->_M_impl
._M_finish
=
1324 std::__uninitialized_copy_a(__first
, __last
,
1325 this->_M_impl
._M_start
,
1326 _M_get_Tp_allocator());
1329 // Called by the first initialize_dispatch above and by the
1330 // vector(n,value,a) constructor.
1332 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1334 this->_M_impl
._M_finish
=
1335 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1336 _M_get_Tp_allocator());
1339 #if __cplusplus >= 201103L
1340 // Called by the vector(n) constructor.
1342 _M_default_initialize(size_type __n
)
1344 this->_M_impl
._M_finish
=
1345 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1346 _M_get_Tp_allocator());
1350 // Internal assign functions follow. The *_aux functions do the actual
1351 // assignment work for the range versions.
1353 // Called by the range assign to implement [23.1.1]/9
1355 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1356 // 438. Ambiguity in the "do the right thing" clause
1357 template<typename _Integer
>
1359 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1360 { _M_fill_assign(__n
, __val
); }
1362 // Called by the range assign to implement [23.1.1]/9
1363 template<typename _InputIterator
>
1365 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1367 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1369 // Called by the second assign_dispatch above
1370 template<typename _InputIterator
>
1372 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1373 std::input_iterator_tag
);
1375 // Called by the second assign_dispatch above
1376 template<typename _ForwardIterator
>
1378 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1379 std::forward_iterator_tag
);
1381 // Called by assign(n,t), and the range assign when it turns out
1382 // to be the same thing.
1384 _M_fill_assign(size_type __n
, const value_type
& __val
);
1386 // Internal insert functions follow.
1388 // Called by the range insert to implement [23.1.1]/9
1390 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1391 // 438. Ambiguity in the "do the right thing" clause
1392 template<typename _Integer
>
1394 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1396 { _M_fill_insert(__pos
, __n
, __val
); }
1398 // Called by the range insert to implement [23.1.1]/9
1399 template<typename _InputIterator
>
1401 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1402 _InputIterator __last
, __false_type
)
1404 _M_range_insert(__pos
, __first
, __last
,
1405 std::__iterator_category(__first
));
1408 // Called by the second insert_dispatch above
1409 template<typename _InputIterator
>
1411 _M_range_insert(iterator __pos
, _InputIterator __first
,
1412 _InputIterator __last
, std::input_iterator_tag
);
1414 // Called by the second insert_dispatch above
1415 template<typename _ForwardIterator
>
1417 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1418 _ForwardIterator __last
, std::forward_iterator_tag
);
1420 // Called by insert(p,n,x), and the range insert when it turns out to be
1423 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1425 #if __cplusplus >= 201103L
1426 // Called by resize(n).
1428 _M_default_append(size_type __n
);
1434 // Called by insert(p,x)
1435 #if __cplusplus < 201103L
1437 _M_insert_aux(iterator __position
, const value_type
& __x
);
1439 template<typename
... _Args
>
1441 _S_insert_aux_assign(iterator __pos
, _Args
&&... __args
)
1442 { *__pos
= _Tp(std::forward
<_Args
>(__args
)...); }
1445 _S_insert_aux_assign(iterator __pos
, _Tp
&& __arg
)
1446 { *__pos
= std::move(__arg
); }
1448 template<typename
... _Args
>
1450 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1452 template<typename
... _Args
>
1454 _M_emplace_back_aux(_Args
&&... __args
);
1457 // Called by the latter.
1459 _M_check_len(size_type __n
, const char* __s
) const
1461 if (max_size() - size() < __n
)
1462 __throw_length_error(__N(__s
));
1464 const size_type __len
= size() + std::max(size(), __n
);
1465 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1468 // Internal erase functions follow.
1470 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1473 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1475 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1476 this->_M_impl
._M_finish
= __pos
;
1480 _M_erase(iterator __position
);
1483 _M_erase(iterator __first
, iterator __last
);
1485 #if __cplusplus >= 201103L
1487 // Constant-time move assignment when source object's memory can be
1488 // moved, either because the source's allocator will move too
1489 // or because the allocators are equal.
1491 _M_move_assign(vector
&& __x
, std::true_type
) noexcept
1493 vector
__tmp(get_allocator());
1494 this->_M_impl
._M_swap_data(__tmp
._M_impl
);
1495 this->_M_impl
._M_swap_data(__x
._M_impl
);
1496 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1499 // Do move assignment when it might not be possible to move source
1500 // object's memory, resulting in a linear-time operation.
1502 _M_move_assign(vector
&& __x
, std::false_type
)
1504 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1505 _M_move_assign(std::move(__x
), std::true_type());
1508 // The rvalue's allocator cannot be moved and is not equal,
1509 // so we need to individually move each element.
1510 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1511 std::__make_move_if_noexcept_iterator(__x
.end()));
1517 #if __cplusplus >= 201103L
1518 template<typename _Up
>
1520 _M_data_ptr(_Up
* __ptr
) const
1523 template<typename _Ptr
>
1524 typename
std::pointer_traits
<_Ptr
>::element_type
*
1525 _M_data_ptr(_Ptr __ptr
) const
1526 { return empty() ? nullptr : std::__addressof(*__ptr
); }
1528 template<typename _Ptr
>
1530 _M_data_ptr(_Ptr __ptr
) const
1537 * @brief Vector equality comparison.
1538 * @param __x A %vector.
1539 * @param __y A %vector of the same type as @a __x.
1540 * @return True iff the size and elements of the vectors are equal.
1542 * This is an equivalence relation. It is linear in the size of the
1543 * vectors. Vectors are considered equivalent if their sizes are equal,
1544 * and if corresponding elements compare equal.
1546 template<typename _Tp
, typename _Alloc
>
1548 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1549 { return (__x
.size() == __y
.size()
1550 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1553 * @brief Vector ordering relation.
1554 * @param __x A %vector.
1555 * @param __y A %vector of the same type as @a __x.
1556 * @return True iff @a __x is lexicographically less than @a __y.
1558 * This is a total ordering relation. It is linear in the size of the
1559 * vectors. The elements must be comparable with @c <.
1561 * See std::lexicographical_compare() for how the determination is made.
1563 template<typename _Tp
, typename _Alloc
>
1565 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1566 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1567 __y
.begin(), __y
.end()); }
1569 /// Based on operator==
1570 template<typename _Tp
, typename _Alloc
>
1572 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1573 { return !(__x
== __y
); }
1575 /// Based on operator<
1576 template<typename _Tp
, typename _Alloc
>
1578 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1579 { return __y
< __x
; }
1581 /// Based on operator<
1582 template<typename _Tp
, typename _Alloc
>
1584 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1585 { return !(__y
< __x
); }
1587 /// Based on operator<
1588 template<typename _Tp
, typename _Alloc
>
1590 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1591 { return !(__x
< __y
); }
1593 /// See std::vector::swap().
1594 template<typename _Tp
, typename _Alloc
>
1596 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1597 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1600 _GLIBCXX_END_NAMESPACE_CONTAINER
1603 #endif /* _STL_VECTOR_H */