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 namespace std
_GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 /// See bits/stl_deque.h's _Deque_base for an explanation.
71 template<typename _Tp
, typename _Alloc
>
74 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
75 rebind
<_Tp
>::other _Tp_alloc_type
;
76 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
80 : public _Tp_alloc_type
84 pointer _M_end_of_storage
;
87 : _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
90 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
91 : _Tp_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage()
94 #if __cplusplus >= 201103L
95 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
96 : _Tp_alloc_type(std::move(__a
)),
97 _M_start(), _M_finish(), _M_end_of_storage()
101 void _M_swap_data(_Vector_impl
& __x
) _GLIBCXX_NOEXCEPT
103 std::swap(_M_start
, __x
._M_start
);
104 std::swap(_M_finish
, __x
._M_finish
);
105 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
110 typedef _Alloc allocator_type
;
113 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
114 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
116 const _Tp_alloc_type
&
117 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
118 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
121 get_allocator() const _GLIBCXX_NOEXCEPT
122 { return allocator_type(_M_get_Tp_allocator()); }
127 _Vector_base(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
130 _Vector_base(size_t __n
)
132 { _M_create_storage(__n
); }
134 _Vector_base(size_t __n
, const allocator_type
& __a
)
136 { _M_create_storage(__n
); }
138 #if __cplusplus >= 201103L
139 _Vector_base(_Tp_alloc_type
&& __a
) noexcept
140 : _M_impl(std::move(__a
)) { }
142 _Vector_base(_Vector_base
&& __x
) noexcept
143 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
144 { this->_M_impl
._M_swap_data(__x
._M_impl
); }
146 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
149 if (__x
.get_allocator() == __a
)
150 this->_M_impl
._M_swap_data(__x
._M_impl
);
153 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
154 _M_create_storage(__n
);
159 ~_Vector_base() _GLIBCXX_NOEXCEPT
160 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
161 - this->_M_impl
._M_start
); }
164 _Vector_impl _M_impl
;
167 _M_allocate(size_t __n
)
169 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
170 return __n
!= 0 ? _Tr::allocate(_M_impl
, __n
) : pointer();
174 _M_deallocate(pointer __p
, size_t __n
)
176 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tr
;
178 _Tr::deallocate(_M_impl
, __p
, __n
);
183 _M_create_storage(size_t __n
)
185 this->_M_impl
._M_start
= this->_M_allocate(__n
);
186 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
187 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
193 * @brief A standard container which offers fixed time access to
194 * individual elements in any order.
198 * @tparam _Tp Type of element.
199 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
201 * Meets the requirements of a <a href="tables.html#65">container</a>, a
202 * <a href="tables.html#66">reversible container</a>, and a
203 * <a href="tables.html#67">sequence</a>, including the
204 * <a href="tables.html#68">optional sequence requirements</a> with the
205 * %exception of @c push_front and @c pop_front.
207 * In some terminology a %vector can be described as a dynamic
208 * C-style array, it offers fast and efficient access to individual
209 * elements in any order and saves the user from worrying about
210 * memory and size allocation. Subscripting ( @c [] ) access is
211 * also provided as with C-style arrays.
213 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
214 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
216 // Concept requirements.
217 typedef typename
_Alloc::value_type _Alloc_value_type
;
218 #if __cplusplus < 201103L
219 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
221 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
223 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
224 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
225 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
228 typedef _Tp value_type
;
229 typedef typename
_Base::pointer pointer
;
230 typedef typename
_Alloc_traits::const_pointer const_pointer
;
231 typedef typename
_Alloc_traits::reference reference
;
232 typedef typename
_Alloc_traits::const_reference const_reference
;
233 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
234 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
236 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
237 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
238 typedef size_t size_type
;
239 typedef ptrdiff_t difference_type
;
240 typedef _Alloc allocator_type
;
243 using _Base::_M_allocate
;
244 using _Base::_M_deallocate
;
245 using _Base::_M_impl
;
246 using _Base::_M_get_Tp_allocator
;
249 // [23.2.4.1] construct/copy/destroy
250 // (assign() and get_allocator() are also listed in this section)
253 * @brief Creates a %vector with no elements.
256 #if __cplusplus >= 201103L
257 noexcept(is_nothrow_default_constructible
<_Alloc
>::value
)
262 * @brief Creates a %vector with no elements.
263 * @param __a An allocator object.
266 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
269 #if __cplusplus >= 201103L
271 * @brief Creates a %vector with default constructed elements.
272 * @param __n The number of elements to initially create.
273 * @param __a An allocator.
275 * This constructor fills the %vector with @a __n default
276 * constructed elements.
279 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
281 { _M_default_initialize(__n
); }
284 * @brief Creates a %vector with copies of an exemplar element.
285 * @param __n The number of elements to initially create.
286 * @param __value An element to copy.
287 * @param __a An allocator.
289 * This constructor fills the %vector with @a __n copies of @a __value.
291 vector(size_type __n
, const value_type
& __value
,
292 const allocator_type
& __a
= allocator_type())
294 { _M_fill_initialize(__n
, __value
); }
297 * @brief Creates a %vector with copies of an exemplar element.
298 * @param __n The number of elements to initially create.
299 * @param __value An element to copy.
300 * @param __a An allocator.
302 * This constructor fills the %vector with @a __n copies of @a __value.
305 vector(size_type __n
, const value_type
& __value
= value_type(),
306 const allocator_type
& __a
= allocator_type())
308 { _M_fill_initialize(__n
, __value
); }
312 * @brief %Vector copy constructor.
313 * @param __x A %vector of identical element and allocator types.
315 * The newly-created %vector uses a copy of the allocation
316 * object used by @a __x. All the elements of @a __x are copied,
317 * but any extra memory in
318 * @a __x (for fast expansion) will not be copied.
320 vector(const vector
& __x
)
322 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
324 this->_M_impl
._M_finish
=
325 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
326 this->_M_impl
._M_start
,
327 _M_get_Tp_allocator());
330 #if __cplusplus >= 201103L
332 * @brief %Vector move constructor.
333 * @param __x A %vector of identical element and allocator types.
335 * The newly-created %vector contains the exact contents of @a __x.
336 * The contents of @a __x are a valid, but unspecified %vector.
338 vector(vector
&& __x
) noexcept
339 : _Base(std::move(__x
)) { }
341 /// Copy constructor with alternative allocator
342 vector(const vector
& __x
, const allocator_type
& __a
)
343 : _Base(__x
.size(), __a
)
345 this->_M_impl
._M_finish
=
346 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
347 this->_M_impl
._M_start
,
348 _M_get_Tp_allocator());
351 /// Move constructor with alternative allocator
352 vector(vector
&& __rv
, const allocator_type
& __m
)
353 noexcept(_Alloc_traits::_S_always_equal())
354 : _Base(std::move(__rv
), __m
)
356 if (__rv
.get_allocator() != __m
)
358 this->_M_impl
._M_finish
=
359 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
360 this->_M_impl
._M_start
,
361 _M_get_Tp_allocator());
367 * @brief Builds a %vector from an initializer list.
368 * @param __l An initializer_list.
369 * @param __a An allocator.
371 * Create a %vector consisting of copies of the elements in the
372 * initializer_list @a __l.
374 * This will call the element type's copy constructor N times
375 * (where N is @a __l.size()) and do no memory reallocation.
377 vector(initializer_list
<value_type
> __l
,
378 const allocator_type
& __a
= allocator_type())
381 _M_range_initialize(__l
.begin(), __l
.end(),
382 random_access_iterator_tag());
387 * @brief Builds a %vector from a range.
388 * @param __first An input iterator.
389 * @param __last An input iterator.
390 * @param __a An allocator.
392 * Create a %vector consisting of copies of the elements from
395 * If the iterators are forward, bidirectional, or
396 * random-access, then this will call the elements' copy
397 * constructor N times (where N is distance(first,last)) and do
398 * no memory reallocation. But if only input iterators are
399 * used, then this will do at most 2N calls to the copy
400 * constructor, and logN memory reallocations.
402 #if __cplusplus >= 201103L
403 template<typename _InputIterator
,
404 typename
= std::_RequireInputIter
<_InputIterator
>>
405 vector(_InputIterator __first
, _InputIterator __last
,
406 const allocator_type
& __a
= allocator_type())
408 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
410 template<typename _InputIterator
>
411 vector(_InputIterator __first
, _InputIterator __last
,
412 const allocator_type
& __a
= allocator_type())
415 // Check whether it's an integral type. If so, it's not an iterator.
416 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
417 _M_initialize_dispatch(__first
, __last
, _Integral());
422 * The dtor only erases the elements, and note that if the
423 * elements themselves are pointers, the pointed-to memory is
424 * not touched in any way. Managing the pointer is the user's
427 ~vector() _GLIBCXX_NOEXCEPT
428 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
429 _M_get_Tp_allocator()); }
432 * @brief %Vector assignment operator.
433 * @param __x A %vector of identical element and allocator types.
435 * All the elements of @a __x are copied, but any extra memory in
436 * @a __x (for fast expansion) will not be copied. Unlike the
437 * copy constructor, the allocator object is not copied.
440 operator=(const vector
& __x
);
442 #if __cplusplus >= 201103L
444 * @brief %Vector move assignment operator.
445 * @param __x A %vector of identical element and allocator types.
447 * The contents of @a __x are moved into this %vector (without copying,
448 * if the allocators permit it).
449 * @a __x is a valid, but unspecified %vector.
452 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
454 constexpr bool __move_storage
=
455 _Alloc_traits::_S_propagate_on_move_assign()
456 || _Alloc_traits::_S_always_equal();
457 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
462 * @brief %Vector list assignment operator.
463 * @param __l An initializer_list.
465 * This function fills a %vector with copies of the elements in the
466 * initializer list @a __l.
468 * Note that the assignment completely changes the %vector and
469 * that the resulting %vector's size is the same as the number
470 * of elements assigned. Old data may be lost.
473 operator=(initializer_list
<value_type
> __l
)
475 this->_M_assign_aux(__l
.begin(), __l
.end(),
476 random_access_iterator_tag());
482 * @brief Assigns a given value to a %vector.
483 * @param __n Number of elements to be assigned.
484 * @param __val Value to be assigned.
486 * This function fills a %vector with @a __n copies of the given
487 * value. Note that the assignment completely changes the
488 * %vector and that the resulting %vector's size is the same as
489 * the number of elements assigned. Old data may be lost.
492 assign(size_type __n
, const value_type
& __val
)
493 { _M_fill_assign(__n
, __val
); }
496 * @brief Assigns a range to a %vector.
497 * @param __first An input iterator.
498 * @param __last An input iterator.
500 * This function fills a %vector with copies of the elements in the
501 * range [__first,__last).
503 * Note that the assignment completely changes the %vector and
504 * that the resulting %vector's size is the same as the number
505 * of elements assigned. Old data may be lost.
507 #if __cplusplus >= 201103L
508 template<typename _InputIterator
,
509 typename
= std::_RequireInputIter
<_InputIterator
>>
511 assign(_InputIterator __first
, _InputIterator __last
)
512 { _M_assign_dispatch(__first
, __last
, __false_type()); }
514 template<typename _InputIterator
>
516 assign(_InputIterator __first
, _InputIterator __last
)
518 // Check whether it's an integral type. If so, it's not an iterator.
519 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
520 _M_assign_dispatch(__first
, __last
, _Integral());
524 #if __cplusplus >= 201103L
526 * @brief Assigns an initializer list to a %vector.
527 * @param __l An initializer_list.
529 * This function fills a %vector with copies of the elements in the
530 * initializer list @a __l.
532 * Note that the assignment completely changes the %vector and
533 * that the resulting %vector's size is the same as the number
534 * of elements assigned. Old data may be lost.
537 assign(initializer_list
<value_type
> __l
)
539 this->_M_assign_aux(__l
.begin(), __l
.end(),
540 random_access_iterator_tag());
544 /// Get a copy of the memory allocation object.
545 using _Base::get_allocator
;
549 * Returns a read/write iterator that points to the first
550 * element in the %vector. Iteration is done in ordinary
554 begin() _GLIBCXX_NOEXCEPT
555 { return iterator(this->_M_impl
._M_start
); }
558 * Returns a read-only (constant) iterator that points to the
559 * first element in the %vector. Iteration is done in ordinary
563 begin() const _GLIBCXX_NOEXCEPT
564 { return const_iterator(this->_M_impl
._M_start
); }
567 * Returns a read/write iterator that points one past the last
568 * element in the %vector. Iteration is done in ordinary
572 end() _GLIBCXX_NOEXCEPT
573 { return iterator(this->_M_impl
._M_finish
); }
576 * Returns a read-only (constant) iterator that points one past
577 * the last element in the %vector. Iteration is done in
578 * ordinary element order.
581 end() const _GLIBCXX_NOEXCEPT
582 { return const_iterator(this->_M_impl
._M_finish
); }
585 * Returns a read/write reverse iterator that points to the
586 * last element in the %vector. Iteration is done in reverse
590 rbegin() _GLIBCXX_NOEXCEPT
591 { return reverse_iterator(end()); }
594 * Returns a read-only (constant) reverse iterator that points
595 * to the last element in the %vector. Iteration is done in
596 * reverse element order.
598 const_reverse_iterator
599 rbegin() const _GLIBCXX_NOEXCEPT
600 { return const_reverse_iterator(end()); }
603 * Returns a read/write reverse iterator that points to one
604 * before the first element in the %vector. Iteration is done
605 * in reverse element order.
608 rend() _GLIBCXX_NOEXCEPT
609 { return reverse_iterator(begin()); }
612 * Returns a read-only (constant) reverse iterator that points
613 * to one before the first element in the %vector. Iteration
614 * is done in reverse element order.
616 const_reverse_iterator
617 rend() const _GLIBCXX_NOEXCEPT
618 { return const_reverse_iterator(begin()); }
620 #if __cplusplus >= 201103L
622 * Returns a read-only (constant) iterator that points to the
623 * first element in the %vector. Iteration is done in ordinary
627 cbegin() const noexcept
628 { return const_iterator(this->_M_impl
._M_start
); }
631 * Returns a read-only (constant) iterator that points one past
632 * the last element in the %vector. Iteration is done in
633 * ordinary element order.
636 cend() const noexcept
637 { return const_iterator(this->_M_impl
._M_finish
); }
640 * Returns a read-only (constant) reverse iterator that points
641 * to the last element in the %vector. Iteration is done in
642 * reverse element order.
644 const_reverse_iterator
645 crbegin() const noexcept
646 { return const_reverse_iterator(end()); }
649 * Returns a read-only (constant) reverse iterator that points
650 * to one before the first element in the %vector. Iteration
651 * is done in reverse element order.
653 const_reverse_iterator
654 crend() const noexcept
655 { return const_reverse_iterator(begin()); }
658 // [23.2.4.2] capacity
659 /** Returns the number of elements in the %vector. */
661 size() const _GLIBCXX_NOEXCEPT
662 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
664 /** Returns the size() of the largest possible %vector. */
666 max_size() const _GLIBCXX_NOEXCEPT
667 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
669 #if __cplusplus >= 201103L
671 * @brief Resizes the %vector to the specified number of elements.
672 * @param __new_size Number of elements the %vector should contain.
674 * This function will %resize the %vector to the specified
675 * number of elements. If the number is smaller than the
676 * %vector's current size the %vector is truncated, otherwise
677 * default constructed elements are appended.
680 resize(size_type __new_size
)
682 if (__new_size
> size())
683 _M_default_append(__new_size
- size());
684 else if (__new_size
< size())
685 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
689 * @brief Resizes the %vector to the specified number of elements.
690 * @param __new_size Number of elements the %vector should contain.
691 * @param __x Data with which new elements should be populated.
693 * This function will %resize the %vector to the specified
694 * number of elements. If the number is smaller than the
695 * %vector's current size the %vector is truncated, otherwise
696 * the %vector is extended and new elements are populated with
700 resize(size_type __new_size
, const value_type
& __x
)
702 if (__new_size
> size())
703 _M_fill_insert(end(), __new_size
- size(), __x
);
704 else if (__new_size
< size())
705 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
709 * @brief Resizes the %vector to the specified number of elements.
710 * @param __new_size Number of elements the %vector should contain.
711 * @param __x Data with which new elements should be populated.
713 * This function will %resize the %vector to the specified
714 * number of elements. If the number is smaller than the
715 * %vector's current size the %vector is truncated, otherwise
716 * the %vector is extended and new elements are populated with
720 resize(size_type __new_size
, value_type __x
= value_type())
722 if (__new_size
> size())
723 _M_fill_insert(end(), __new_size
- size(), __x
);
724 else if (__new_size
< size())
725 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
729 #if __cplusplus >= 201103L
730 /** A non-binding request to reduce capacity() to size(). */
733 { _M_shrink_to_fit(); }
737 * Returns the total number of elements that the %vector can
738 * hold before needing to allocate more memory.
741 capacity() const _GLIBCXX_NOEXCEPT
742 { return size_type(this->_M_impl
._M_end_of_storage
743 - this->_M_impl
._M_start
); }
746 * Returns true if the %vector is empty. (Thus begin() would
750 empty() const _GLIBCXX_NOEXCEPT
751 { return begin() == end(); }
754 * @brief Attempt to preallocate enough memory for specified number of
756 * @param __n Number of elements required.
757 * @throw std::length_error If @a n exceeds @c max_size().
759 * This function attempts to reserve enough memory for the
760 * %vector to hold the specified number of elements. If the
761 * number requested is more than max_size(), length_error is
764 * The advantage of this function is that if optimal code is a
765 * necessity and the user can determine the number of elements
766 * that will be required, the user can reserve the memory in
767 * %advance, and thus prevent a possible reallocation of memory
768 * and copying of %vector data.
771 reserve(size_type __n
);
775 * @brief Subscript access to the data contained in the %vector.
776 * @param __n The index of the element for which data should be
778 * @return Read/write reference to data.
780 * This operator allows for easy, array-style, data access.
781 * Note that data access with this operator is unchecked and
782 * out_of_range lookups are not defined. (For checked lookups
786 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
787 { return *(this->_M_impl
._M_start
+ __n
); }
790 * @brief Subscript access to the data contained in the %vector.
791 * @param __n The index of the element for which data should be
793 * @return Read-only (constant) reference to data.
795 * This operator allows for easy, array-style, data access.
796 * Note that data access with this operator is unchecked and
797 * out_of_range lookups are not defined. (For checked lookups
801 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
802 { return *(this->_M_impl
._M_start
+ __n
); }
805 /// Safety check used only from at().
807 _M_range_check(size_type __n
) const
809 if (__n
>= this->size())
810 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
811 "(which is %zu) >= this->size() "
818 * @brief Provides access to the data contained in the %vector.
819 * @param __n The index of the element for which data should be
821 * @return Read/write reference to data.
822 * @throw std::out_of_range If @a __n is an invalid index.
824 * This function provides for safer data access. The parameter
825 * is first checked that it is in the range of the vector. The
826 * function throws out_of_range if the check fails.
836 * @brief Provides access to the data contained in the %vector.
837 * @param __n The index of the element for which data should be
839 * @return Read-only (constant) reference to data.
840 * @throw std::out_of_range If @a __n is an invalid index.
842 * This function provides for safer data access. The parameter
843 * is first checked that it is in the range of the vector. The
844 * function throws out_of_range if the check fails.
847 at(size_type __n
) const
854 * Returns a read/write reference to the data at the first
855 * element of the %vector.
858 front() _GLIBCXX_NOEXCEPT
862 * Returns a read-only (constant) reference to the data at the first
863 * element of the %vector.
866 front() const _GLIBCXX_NOEXCEPT
870 * Returns a read/write reference to the data at the last
871 * element of the %vector.
874 back() _GLIBCXX_NOEXCEPT
875 { return *(end() - 1); }
878 * Returns a read-only (constant) reference to the data at the
879 * last element of the %vector.
882 back() const _GLIBCXX_NOEXCEPT
883 { return *(end() - 1); }
885 // _GLIBCXX_RESOLVE_LIB_DEFECTS
886 // DR 464. Suggestion for new member functions in standard containers.
889 * Returns a pointer such that [data(), data() + size()) is a valid
890 * range. For a non-empty %vector, data() == &front().
892 #if __cplusplus >= 201103L
897 data() _GLIBCXX_NOEXCEPT
898 { return _M_data_ptr(this->_M_impl
._M_start
); }
900 #if __cplusplus >= 201103L
905 data() const _GLIBCXX_NOEXCEPT
906 { return _M_data_ptr(this->_M_impl
._M_start
); }
908 // [23.2.4.3] modifiers
910 * @brief Add data to the end of the %vector.
911 * @param __x Data to be added.
913 * This is a typical stack operation. The function creates an
914 * element at the end of the %vector and assigns the given data
915 * to it. Due to the nature of a %vector this operation can be
916 * done in constant time if the %vector has preallocated space
920 push_back(const value_type
& __x
)
922 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
924 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
926 ++this->_M_impl
._M_finish
;
929 #if __cplusplus >= 201103L
930 _M_emplace_back_aux(__x
);
932 _M_insert_aux(end(), __x
);
936 #if __cplusplus >= 201103L
938 push_back(value_type
&& __x
)
939 { emplace_back(std::move(__x
)); }
941 template<typename
... _Args
>
943 emplace_back(_Args
&&... __args
);
947 * @brief Removes last element.
949 * This is a typical stack operation. It shrinks the %vector by one.
951 * Note that no data is returned, and if the last element's
952 * data is needed, it should be retrieved before pop_back() is
956 pop_back() _GLIBCXX_NOEXCEPT
958 --this->_M_impl
._M_finish
;
959 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
962 #if __cplusplus >= 201103L
964 * @brief Inserts an object in %vector before specified iterator.
965 * @param __position A const_iterator into the %vector.
966 * @param __args Arguments.
967 * @return An iterator that points to the inserted data.
969 * This function will insert an object of type T constructed
970 * with T(std::forward<Args>(args)...) before the specified location.
971 * Note that this kind of operation could be expensive for a %vector
972 * and if it is frequently used the user should consider using
975 template<typename
... _Args
>
977 emplace(const_iterator __position
, _Args
&&... __args
);
980 * @brief Inserts given value into %vector before specified iterator.
981 * @param __position A const_iterator into the %vector.
982 * @param __x Data to be inserted.
983 * @return An iterator that points to the inserted data.
985 * This function will insert a copy of the given value before
986 * the specified location. Note that this kind of operation
987 * could be expensive for a %vector and if it is frequently
988 * used the user should consider using std::list.
991 insert(const_iterator __position
, const value_type
& __x
);
994 * @brief Inserts given value into %vector before specified iterator.
995 * @param __position An iterator into the %vector.
996 * @param __x Data to be inserted.
997 * @return An iterator that points to the inserted data.
999 * This function will insert a copy of the given value before
1000 * the specified location. Note that this kind of operation
1001 * could be expensive for a %vector and if it is frequently
1002 * used the user should consider using std::list.
1005 insert(iterator __position
, const value_type
& __x
);
1008 #if __cplusplus >= 201103L
1010 * @brief Inserts given rvalue into %vector before specified iterator.
1011 * @param __position A const_iterator into the %vector.
1012 * @param __x Data to be inserted.
1013 * @return An iterator that points to the inserted data.
1015 * This function will insert a copy of the given rvalue before
1016 * the specified location. Note that this kind of operation
1017 * could be expensive for a %vector and if it is frequently
1018 * used the user should consider using std::list.
1021 insert(const_iterator __position
, value_type
&& __x
)
1022 { return emplace(__position
, std::move(__x
)); }
1025 * @brief Inserts an initializer_list into the %vector.
1026 * @param __position An iterator into the %vector.
1027 * @param __l An initializer_list.
1029 * This function will insert copies of the data in the
1030 * initializer_list @a l into the %vector before the location
1031 * specified by @a position.
1033 * Note that this kind of operation could be expensive for a
1034 * %vector and if it is frequently used the user should
1035 * consider using std::list.
1038 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1040 auto __offset
= __position
- cbegin();
1041 _M_range_insert(begin() + __offset
, __l
.begin(), __l
.end(),
1042 std::random_access_iterator_tag());
1043 return begin() + __offset
;
1047 #if __cplusplus >= 201103L
1049 * @brief Inserts a number of copies of given data into the %vector.
1050 * @param __position A const_iterator into the %vector.
1051 * @param __n Number of elements to be inserted.
1052 * @param __x Data to be inserted.
1053 * @return An iterator that points to the inserted data.
1055 * This function will insert a specified number of copies of
1056 * the given data before the location specified by @a position.
1058 * Note that this kind of operation could be expensive for a
1059 * %vector and if it is frequently used the user should
1060 * consider using std::list.
1063 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1065 difference_type __offset
= __position
- cbegin();
1066 _M_fill_insert(begin() + __offset
, __n
, __x
);
1067 return begin() + __offset
;
1071 * @brief Inserts a number of copies of given data into the %vector.
1072 * @param __position An iterator into the %vector.
1073 * @param __n Number of elements to be inserted.
1074 * @param __x Data to be inserted.
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(iterator __position
, size_type __n
, const value_type
& __x
)
1085 { _M_fill_insert(__position
, __n
, __x
); }
1088 #if __cplusplus >= 201103L
1090 * @brief Inserts a range into the %vector.
1091 * @param __position A const_iterator into the %vector.
1092 * @param __first An input iterator.
1093 * @param __last An input iterator.
1094 * @return An iterator that points to the inserted data.
1096 * This function will insert copies of the data in the range
1097 * [__first,__last) into the %vector before the location specified
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.
1104 template<typename _InputIterator
,
1105 typename
= std::_RequireInputIter
<_InputIterator
>>
1107 insert(const_iterator __position
, _InputIterator __first
,
1108 _InputIterator __last
)
1110 difference_type __offset
= __position
- cbegin();
1111 _M_insert_dispatch(begin() + __offset
,
1112 __first
, __last
, __false_type());
1113 return begin() + __offset
;
1117 * @brief Inserts a range into the %vector.
1118 * @param __position An iterator into the %vector.
1119 * @param __first An input iterator.
1120 * @param __last An input iterator.
1122 * This function will insert copies of the data in the range
1123 * [__first,__last) into the %vector before the location specified
1126 * Note that this kind of operation could be expensive for a
1127 * %vector and if it is frequently used the user should
1128 * consider using std::list.
1130 template<typename _InputIterator
>
1132 insert(iterator __position
, _InputIterator __first
,
1133 _InputIterator __last
)
1135 // Check whether it's an integral type. If so, it's not an iterator.
1136 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1137 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1142 * @brief Remove element at given position.
1143 * @param __position Iterator pointing to element to be erased.
1144 * @return An iterator pointing to the next element (or end()).
1146 * This function will erase the element at the given position and thus
1147 * shorten the %vector by one.
1149 * Note This operation could be expensive and if it is
1150 * frequently used the user should consider using std::list.
1151 * The user is also cautioned that this function only erases
1152 * the element, and that if the element is itself a pointer,
1153 * the pointed-to memory is not touched in any way. Managing
1154 * the pointer is the user's responsibility.
1157 #if __cplusplus >= 201103L
1158 erase(const_iterator __position
)
1159 { return _M_erase(begin() + (__position
- cbegin())); }
1161 erase(iterator __position
)
1162 { return _M_erase(__position
); }
1166 * @brief Remove a range of elements.
1167 * @param __first Iterator pointing to the first element to be erased.
1168 * @param __last Iterator pointing to one past the last element to be
1170 * @return An iterator pointing to the element pointed to by @a __last
1171 * prior to erasing (or end()).
1173 * This function will erase the elements in the range
1174 * [__first,__last) and shorten the %vector accordingly.
1176 * Note This operation could be expensive and if it is
1177 * frequently used the user should consider using std::list.
1178 * The user is also cautioned that this function only erases
1179 * the elements, and that if the elements themselves are
1180 * pointers, the pointed-to memory is not touched in any way.
1181 * Managing the pointer is the user's responsibility.
1184 #if __cplusplus >= 201103L
1185 erase(const_iterator __first
, const_iterator __last
)
1187 const auto __beg
= begin();
1188 const auto __cbeg
= cbegin();
1189 return _M_erase(__beg
+ (__first
- __cbeg
), __beg
+ (__last
- __cbeg
));
1192 erase(iterator __first
, iterator __last
)
1193 { return _M_erase(__first
, __last
); }
1197 * @brief Swaps data with another %vector.
1198 * @param __x A %vector of the same element and allocator types.
1200 * This exchanges the elements between two vectors in constant time.
1201 * (Three pointers, so it should be quite fast.)
1202 * Note that the global std::swap() function is specialized such that
1203 * std::swap(v1,v2) will feed to this function.
1206 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1208 this->_M_impl
._M_swap_data(__x
._M_impl
);
1209 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1210 __x
._M_get_Tp_allocator());
1214 * Erases all the elements. Note that this function only erases the
1215 * elements, and that if the elements themselves are pointers, the
1216 * pointed-to memory is not touched in any way. Managing the pointer is
1217 * the user's responsibility.
1220 clear() _GLIBCXX_NOEXCEPT
1221 { _M_erase_at_end(this->_M_impl
._M_start
); }
1225 * Memory expansion handler. Uses the member allocation function to
1226 * obtain @a n bytes of memory, and then copies [first,last) into it.
1228 template<typename _ForwardIterator
>
1230 _M_allocate_and_copy(size_type __n
,
1231 _ForwardIterator __first
, _ForwardIterator __last
)
1233 pointer __result
= this->_M_allocate(__n
);
1236 std::__uninitialized_copy_a(__first
, __last
, __result
,
1237 _M_get_Tp_allocator());
1242 _M_deallocate(__result
, __n
);
1243 __throw_exception_again
;
1248 // Internal constructor functions follow.
1250 // Called by the range constructor to implement [23.1.1]/9
1252 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1253 // 438. Ambiguity in the "do the right thing" clause
1254 template<typename _Integer
>
1256 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1258 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1259 this->_M_impl
._M_end_of_storage
=
1260 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1261 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1264 // Called by the range constructor to implement [23.1.1]/9
1265 template<typename _InputIterator
>
1267 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1270 typedef typename
std::iterator_traits
<_InputIterator
>::
1271 iterator_category _IterCategory
;
1272 _M_range_initialize(__first
, __last
, _IterCategory());
1275 // Called by the second initialize_dispatch above
1276 template<typename _InputIterator
>
1278 _M_range_initialize(_InputIterator __first
,
1279 _InputIterator __last
, std::input_iterator_tag
)
1281 for (; __first
!= __last
; ++__first
)
1282 #if __cplusplus >= 201103L
1283 emplace_back(*__first
);
1285 push_back(*__first
);
1289 // Called by the second initialize_dispatch above
1290 template<typename _ForwardIterator
>
1292 _M_range_initialize(_ForwardIterator __first
,
1293 _ForwardIterator __last
, std::forward_iterator_tag
)
1295 const size_type __n
= std::distance(__first
, __last
);
1296 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1297 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1298 this->_M_impl
._M_finish
=
1299 std::__uninitialized_copy_a(__first
, __last
,
1300 this->_M_impl
._M_start
,
1301 _M_get_Tp_allocator());
1304 // Called by the first initialize_dispatch above and by the
1305 // vector(n,value,a) constructor.
1307 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1309 this->_M_impl
._M_finish
=
1310 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1311 _M_get_Tp_allocator());
1314 #if __cplusplus >= 201103L
1315 // Called by the vector(n) constructor.
1317 _M_default_initialize(size_type __n
)
1319 this->_M_impl
._M_finish
=
1320 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1321 _M_get_Tp_allocator());
1325 // Internal assign functions follow. The *_aux functions do the actual
1326 // assignment work for the range versions.
1328 // Called by the range assign to implement [23.1.1]/9
1330 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1331 // 438. Ambiguity in the "do the right thing" clause
1332 template<typename _Integer
>
1334 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1335 { _M_fill_assign(__n
, __val
); }
1337 // Called by the range assign to implement [23.1.1]/9
1338 template<typename _InputIterator
>
1340 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1342 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1344 // Called by the second assign_dispatch above
1345 template<typename _InputIterator
>
1347 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1348 std::input_iterator_tag
);
1350 // Called by the second assign_dispatch above
1351 template<typename _ForwardIterator
>
1353 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1354 std::forward_iterator_tag
);
1356 // Called by assign(n,t), and the range assign when it turns out
1357 // to be the same thing.
1359 _M_fill_assign(size_type __n
, const value_type
& __val
);
1361 // Internal insert functions follow.
1363 // Called by the range insert to implement [23.1.1]/9
1365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1366 // 438. Ambiguity in the "do the right thing" clause
1367 template<typename _Integer
>
1369 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1371 { _M_fill_insert(__pos
, __n
, __val
); }
1373 // Called by the range insert to implement [23.1.1]/9
1374 template<typename _InputIterator
>
1376 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1377 _InputIterator __last
, __false_type
)
1379 _M_range_insert(__pos
, __first
, __last
,
1380 std::__iterator_category(__first
));
1383 // Called by the second insert_dispatch above
1384 template<typename _InputIterator
>
1386 _M_range_insert(iterator __pos
, _InputIterator __first
,
1387 _InputIterator __last
, std::input_iterator_tag
);
1389 // Called by the second insert_dispatch above
1390 template<typename _ForwardIterator
>
1392 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1393 _ForwardIterator __last
, std::forward_iterator_tag
);
1395 // Called by insert(p,n,x), and the range insert when it turns out to be
1398 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1400 #if __cplusplus >= 201103L
1401 // Called by resize(n).
1403 _M_default_append(size_type __n
);
1409 // Called by insert(p,x)
1410 #if __cplusplus < 201103L
1412 _M_insert_aux(iterator __position
, const value_type
& __x
);
1414 template<typename
... _Args
>
1416 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1418 template<typename
... _Args
>
1420 _M_emplace_back_aux(_Args
&&... __args
);
1423 // Called by the latter.
1425 _M_check_len(size_type __n
, const char* __s
) const
1427 if (max_size() - size() < __n
)
1428 __throw_length_error(__N(__s
));
1430 const size_type __len
= size() + std::max(size(), __n
);
1431 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1434 // Internal erase functions follow.
1436 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1439 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1441 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1442 this->_M_impl
._M_finish
= __pos
;
1446 _M_erase(iterator __position
);
1449 _M_erase(iterator __first
, iterator __last
);
1451 #if __cplusplus >= 201103L
1453 // Constant-time move assignment when source object's memory can be
1454 // moved, either because the source's allocator will move too
1455 // or because the allocators are equal.
1457 _M_move_assign(vector
&& __x
, std::true_type
) noexcept
1459 vector
__tmp(get_allocator());
1460 this->_M_impl
._M_swap_data(__tmp
._M_impl
);
1461 this->_M_impl
._M_swap_data(__x
._M_impl
);
1462 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1465 // Do move assignment when it might not be possible to move source
1466 // object's memory, resulting in a linear-time operation.
1468 _M_move_assign(vector
&& __x
, std::false_type
)
1470 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1471 _M_move_assign(std::move(__x
), std::true_type());
1474 // The rvalue's allocator cannot be moved and is not equal,
1475 // so we need to individually move each element.
1476 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1477 std::__make_move_if_noexcept_iterator(__x
.end()));
1483 #if __cplusplus >= 201103L
1484 template<typename _Up
>
1486 _M_data_ptr(_Up
* __ptr
) const
1489 template<typename _Ptr
>
1490 typename
std::pointer_traits
<_Ptr
>::element_type
*
1491 _M_data_ptr(_Ptr __ptr
) const
1492 { return empty() ? nullptr : std::__addressof(*__ptr
); }
1494 template<typename _Ptr
>
1496 _M_data_ptr(_Ptr __ptr
) const
1503 * @brief Vector equality comparison.
1504 * @param __x A %vector.
1505 * @param __y A %vector of the same type as @a __x.
1506 * @return True iff the size and elements of the vectors are equal.
1508 * This is an equivalence relation. It is linear in the size of the
1509 * vectors. Vectors are considered equivalent if their sizes are equal,
1510 * and if corresponding elements compare equal.
1512 template<typename _Tp
, typename _Alloc
>
1514 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1515 { return (__x
.size() == __y
.size()
1516 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1519 * @brief Vector ordering relation.
1520 * @param __x A %vector.
1521 * @param __y A %vector of the same type as @a __x.
1522 * @return True iff @a __x is lexicographically less than @a __y.
1524 * This is a total ordering relation. It is linear in the size of the
1525 * vectors. The elements must be comparable with @c <.
1527 * See std::lexicographical_compare() for how the determination is made.
1529 template<typename _Tp
, typename _Alloc
>
1531 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1532 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1533 __y
.begin(), __y
.end()); }
1535 /// Based on operator==
1536 template<typename _Tp
, typename _Alloc
>
1538 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1539 { return !(__x
== __y
); }
1541 /// Based on operator<
1542 template<typename _Tp
, typename _Alloc
>
1544 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1545 { return __y
< __x
; }
1547 /// Based on operator<
1548 template<typename _Tp
, typename _Alloc
>
1550 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1551 { return !(__y
< __x
); }
1553 /// Based on operator<
1554 template<typename _Tp
, typename _Alloc
>
1556 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1557 { return !(__x
< __y
); }
1559 /// See std::vector::swap().
1560 template<typename _Tp
, typename _Alloc
>
1562 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1563 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1566 _GLIBCXX_END_NAMESPACE_CONTAINER
1569 #endif /* _STL_VECTOR_H */