1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001-2014 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(0), _M_finish(0), _M_end_of_storage(0)
90 _Vector_impl(_Tp_alloc_type
const& __a
) _GLIBCXX_NOEXCEPT
91 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
94 #if __cplusplus >= 201103L
95 _Vector_impl(_Tp_alloc_type
&& __a
) noexcept
96 : _Tp_alloc_type(std::move(__a
)),
97 _M_start(0), _M_finish(0), _M_end_of_storage(0)
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
) : 0;
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 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
219 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
221 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
222 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
223 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
226 typedef _Tp value_type
;
227 typedef typename
_Base::pointer pointer
;
228 typedef typename
_Alloc_traits::const_pointer const_pointer
;
229 typedef typename
_Alloc_traits::reference reference
;
230 typedef typename
_Alloc_traits::const_reference const_reference
;
231 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
232 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
234 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
235 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
236 typedef size_t size_type
;
237 typedef ptrdiff_t difference_type
;
238 typedef _Alloc allocator_type
;
241 using _Base::_M_allocate
;
242 using _Base::_M_deallocate
;
243 using _Base::_M_impl
;
244 using _Base::_M_get_Tp_allocator
;
247 // [23.2.4.1] construct/copy/destroy
248 // (assign() and get_allocator() are also listed in this section)
251 * @brief Creates a %vector with no elements.
254 #if __cplusplus >= 201103L
255 noexcept(is_nothrow_default_constructible
<_Alloc
>::value
)
260 * @brief Creates a %vector with no elements.
261 * @param __a An allocator object.
264 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
267 #if __cplusplus >= 201103L
269 * @brief Creates a %vector with default constructed elements.
270 * @param __n The number of elements to initially create.
271 * @param __a An allocator.
273 * This constructor fills the %vector with @a __n default
274 * constructed elements.
277 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
279 { _M_default_initialize(__n
); }
282 * @brief Creates a %vector with copies of an exemplar element.
283 * @param __n The number of elements to initially create.
284 * @param __value An element to copy.
285 * @param __a An allocator.
287 * This constructor fills the %vector with @a __n copies of @a __value.
289 vector(size_type __n
, const value_type
& __value
,
290 const allocator_type
& __a
= allocator_type())
292 { _M_fill_initialize(__n
, __value
); }
295 * @brief Creates a %vector with copies of an exemplar element.
296 * @param __n The number of elements to initially create.
297 * @param __value An element to copy.
298 * @param __a An allocator.
300 * This constructor fills the %vector with @a __n copies of @a __value.
303 vector(size_type __n
, const value_type
& __value
= value_type(),
304 const allocator_type
& __a
= allocator_type())
306 { _M_fill_initialize(__n
, __value
); }
310 * @brief %Vector copy constructor.
311 * @param __x A %vector of identical element and allocator types.
313 * The newly-created %vector uses a copy of the allocation
314 * object used by @a __x. All the elements of @a __x are copied,
315 * but any extra memory in
316 * @a __x (for fast expansion) will not be copied.
318 vector(const vector
& __x
)
320 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
321 { this->_M_impl
._M_finish
=
322 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
323 this->_M_impl
._M_start
,
324 _M_get_Tp_allocator());
327 #if __cplusplus >= 201103L
329 * @brief %Vector move constructor.
330 * @param __x A %vector of identical element and allocator types.
332 * The newly-created %vector contains the exact contents of @a __x.
333 * The contents of @a __x are a valid, but unspecified %vector.
335 vector(vector
&& __x
) noexcept
336 : _Base(std::move(__x
)) { }
338 /// Copy constructor with alternative allocator
339 vector(const vector
& __x
, const allocator_type
& __a
)
340 : _Base(__x
.size(), __a
)
341 { this->_M_impl
._M_finish
=
342 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
343 this->_M_impl
._M_start
,
344 _M_get_Tp_allocator());
347 /// Move constructor with alternative allocator
348 vector(vector
&& __rv
, const allocator_type
& __m
)
349 noexcept(_Alloc_traits::_S_always_equal())
350 : _Base(std::move(__rv
), __m
)
352 if (__rv
.get_allocator() != __m
)
354 this->_M_impl
._M_finish
=
355 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
356 this->_M_impl
._M_start
,
357 _M_get_Tp_allocator());
363 * @brief Builds a %vector from an initializer list.
364 * @param __l An initializer_list.
365 * @param __a An allocator.
367 * Create a %vector consisting of copies of the elements in the
368 * initializer_list @a __l.
370 * This will call the element type's copy constructor N times
371 * (where N is @a __l.size()) and do no memory reallocation.
373 vector(initializer_list
<value_type
> __l
,
374 const allocator_type
& __a
= allocator_type())
377 _M_range_initialize(__l
.begin(), __l
.end(),
378 random_access_iterator_tag());
383 * @brief Builds a %vector from a range.
384 * @param __first An input iterator.
385 * @param __last An input iterator.
386 * @param __a An allocator.
388 * Create a %vector consisting of copies of the elements from
391 * If the iterators are forward, bidirectional, or
392 * random-access, then this will call the elements' copy
393 * constructor N times (where N is distance(first,last)) and do
394 * no memory reallocation. But if only input iterators are
395 * used, then this will do at most 2N calls to the copy
396 * constructor, and logN memory reallocations.
398 #if __cplusplus >= 201103L
399 template<typename _InputIterator
,
400 typename
= std::_RequireInputIter
<_InputIterator
>>
401 vector(_InputIterator __first
, _InputIterator __last
,
402 const allocator_type
& __a
= allocator_type())
404 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
406 template<typename _InputIterator
>
407 vector(_InputIterator __first
, _InputIterator __last
,
408 const allocator_type
& __a
= allocator_type())
411 // Check whether it's an integral type. If so, it's not an iterator.
412 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
413 _M_initialize_dispatch(__first
, __last
, _Integral());
418 * The dtor only erases the elements, and note that if the
419 * elements themselves are pointers, the pointed-to memory is
420 * not touched in any way. Managing the pointer is the user's
423 ~vector() _GLIBCXX_NOEXCEPT
424 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
425 _M_get_Tp_allocator()); }
428 * @brief %Vector assignment operator.
429 * @param __x A %vector of identical element and allocator types.
431 * All the elements of @a __x are copied, but any extra memory in
432 * @a __x (for fast expansion) will not be copied. Unlike the
433 * copy constructor, the allocator object is not copied.
436 operator=(const vector
& __x
);
438 #if __cplusplus >= 201103L
440 * @brief %Vector move assignment operator.
441 * @param __x A %vector of identical element and allocator types.
443 * The contents of @a __x are moved into this %vector (without copying,
444 * if the allocators permit it).
445 * @a __x is a valid, but unspecified %vector.
448 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
450 constexpr bool __move_storage
=
451 _Alloc_traits::_S_propagate_on_move_assign()
452 || _Alloc_traits::_S_always_equal();
453 _M_move_assign(std::move(__x
),
454 integral_constant
<bool, __move_storage
>());
459 * @brief %Vector list assignment operator.
460 * @param __l An initializer_list.
462 * This function fills a %vector with copies of the elements in the
463 * initializer list @a __l.
465 * Note that the assignment completely changes the %vector and
466 * that the resulting %vector's size is the same as the number
467 * of elements assigned. Old data may be lost.
470 operator=(initializer_list
<value_type
> __l
)
472 this->assign(__l
.begin(), __l
.end());
478 * @brief Assigns a given value to a %vector.
479 * @param __n Number of elements to be assigned.
480 * @param __val Value to be assigned.
482 * This function fills a %vector with @a __n copies of the given
483 * value. Note that the assignment completely changes the
484 * %vector and that the resulting %vector's size is the same as
485 * the number of elements assigned. Old data may be lost.
488 assign(size_type __n
, const value_type
& __val
)
489 { _M_fill_assign(__n
, __val
); }
492 * @brief Assigns a range to a %vector.
493 * @param __first An input iterator.
494 * @param __last An input iterator.
496 * This function fills a %vector with copies of the elements in the
497 * range [__first,__last).
499 * Note that the assignment completely changes the %vector and
500 * that the resulting %vector's size is the same as the number
501 * of elements assigned. Old data may be lost.
503 #if __cplusplus >= 201103L
504 template<typename _InputIterator
,
505 typename
= std::_RequireInputIter
<_InputIterator
>>
507 assign(_InputIterator __first
, _InputIterator __last
)
508 { _M_assign_dispatch(__first
, __last
, __false_type()); }
510 template<typename _InputIterator
>
512 assign(_InputIterator __first
, _InputIterator __last
)
514 // Check whether it's an integral type. If so, it's not an iterator.
515 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
516 _M_assign_dispatch(__first
, __last
, _Integral());
520 #if __cplusplus >= 201103L
522 * @brief Assigns an initializer list to a %vector.
523 * @param __l An initializer_list.
525 * This function fills a %vector with copies of the elements in the
526 * initializer list @a __l.
528 * Note that the assignment completely changes the %vector and
529 * that the resulting %vector's size is the same as the number
530 * of elements assigned. Old data may be lost.
533 assign(initializer_list
<value_type
> __l
)
534 { this->assign(__l
.begin(), __l
.end()); }
537 /// Get a copy of the memory allocation object.
538 using _Base::get_allocator
;
542 * Returns a read/write iterator that points to the first
543 * element in the %vector. Iteration is done in ordinary
547 begin() _GLIBCXX_NOEXCEPT
548 { return iterator(this->_M_impl
._M_start
); }
551 * Returns a read-only (constant) iterator that points to the
552 * first element in the %vector. Iteration is done in ordinary
556 begin() const _GLIBCXX_NOEXCEPT
557 { return const_iterator(this->_M_impl
._M_start
); }
560 * Returns a read/write iterator that points one past the last
561 * element in the %vector. Iteration is done in ordinary
565 end() _GLIBCXX_NOEXCEPT
566 { return iterator(this->_M_impl
._M_finish
); }
569 * Returns a read-only (constant) iterator that points one past
570 * the last element in the %vector. Iteration is done in
571 * ordinary element order.
574 end() const _GLIBCXX_NOEXCEPT
575 { return const_iterator(this->_M_impl
._M_finish
); }
578 * Returns a read/write reverse iterator that points to the
579 * last element in the %vector. Iteration is done in reverse
583 rbegin() _GLIBCXX_NOEXCEPT
584 { return reverse_iterator(end()); }
587 * Returns a read-only (constant) reverse iterator that points
588 * to the last element in the %vector. Iteration is done in
589 * reverse element order.
591 const_reverse_iterator
592 rbegin() const _GLIBCXX_NOEXCEPT
593 { return const_reverse_iterator(end()); }
596 * Returns a read/write reverse iterator that points to one
597 * before the first element in the %vector. Iteration is done
598 * in reverse element order.
601 rend() _GLIBCXX_NOEXCEPT
602 { return reverse_iterator(begin()); }
605 * Returns a read-only (constant) reverse iterator that points
606 * to one before the first element in the %vector. Iteration
607 * is done in reverse element order.
609 const_reverse_iterator
610 rend() const _GLIBCXX_NOEXCEPT
611 { return const_reverse_iterator(begin()); }
613 #if __cplusplus >= 201103L
615 * Returns a read-only (constant) iterator that points to the
616 * first element in the %vector. Iteration is done in ordinary
620 cbegin() const noexcept
621 { return const_iterator(this->_M_impl
._M_start
); }
624 * Returns a read-only (constant) iterator that points one past
625 * the last element in the %vector. Iteration is done in
626 * ordinary element order.
629 cend() const noexcept
630 { return const_iterator(this->_M_impl
._M_finish
); }
633 * Returns a read-only (constant) reverse iterator that points
634 * to the last element in the %vector. Iteration is done in
635 * reverse element order.
637 const_reverse_iterator
638 crbegin() const noexcept
639 { return const_reverse_iterator(end()); }
642 * Returns a read-only (constant) reverse iterator that points
643 * to one before the first element in the %vector. Iteration
644 * is done in reverse element order.
646 const_reverse_iterator
647 crend() const noexcept
648 { return const_reverse_iterator(begin()); }
651 // [23.2.4.2] capacity
652 /** Returns the number of elements in the %vector. */
654 size() const _GLIBCXX_NOEXCEPT
655 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
657 /** Returns the size() of the largest possible %vector. */
659 max_size() const _GLIBCXX_NOEXCEPT
660 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
662 #if __cplusplus >= 201103L
664 * @brief Resizes the %vector to the specified number of elements.
665 * @param __new_size Number of elements the %vector should contain.
667 * This function will %resize the %vector to the specified
668 * number of elements. If the number is smaller than the
669 * %vector's current size the %vector is truncated, otherwise
670 * default constructed elements are appended.
673 resize(size_type __new_size
)
675 if (__new_size
> size())
676 _M_default_append(__new_size
- size());
677 else if (__new_size
< size())
678 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
682 * @brief Resizes the %vector to the specified number of elements.
683 * @param __new_size Number of elements the %vector should contain.
684 * @param __x Data with which new elements should be populated.
686 * This function will %resize the %vector to the specified
687 * number of elements. If the number is smaller than the
688 * %vector's current size the %vector is truncated, otherwise
689 * the %vector is extended and new elements are populated with
693 resize(size_type __new_size
, const value_type
& __x
)
695 if (__new_size
> size())
696 insert(end(), __new_size
- size(), __x
);
697 else if (__new_size
< size())
698 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
702 * @brief Resizes the %vector to the specified number of elements.
703 * @param __new_size Number of elements the %vector should contain.
704 * @param __x Data with which new elements should be populated.
706 * This function will %resize the %vector to the specified
707 * number of elements. If the number is smaller than the
708 * %vector's current size the %vector is truncated, otherwise
709 * the %vector is extended and new elements are populated with
713 resize(size_type __new_size
, value_type __x
= value_type())
715 if (__new_size
> size())
716 insert(end(), __new_size
- size(), __x
);
717 else if (__new_size
< size())
718 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
722 #if __cplusplus >= 201103L
723 /** A non-binding request to reduce capacity() to size(). */
726 { _M_shrink_to_fit(); }
730 * Returns the total number of elements that the %vector can
731 * hold before needing to allocate more memory.
734 capacity() const _GLIBCXX_NOEXCEPT
735 { return size_type(this->_M_impl
._M_end_of_storage
736 - this->_M_impl
._M_start
); }
739 * Returns true if the %vector is empty. (Thus begin() would
743 empty() const _GLIBCXX_NOEXCEPT
744 { return begin() == end(); }
747 * @brief Attempt to preallocate enough memory for specified number of
749 * @param __n Number of elements required.
750 * @throw std::length_error If @a n exceeds @c max_size().
752 * This function attempts to reserve enough memory for the
753 * %vector to hold the specified number of elements. If the
754 * number requested is more than max_size(), length_error is
757 * The advantage of this function is that if optimal code is a
758 * necessity and the user can determine the number of elements
759 * that will be required, the user can reserve the memory in
760 * %advance, and thus prevent a possible reallocation of memory
761 * and copying of %vector data.
764 reserve(size_type __n
);
768 * @brief Subscript access to the data contained in the %vector.
769 * @param __n The index of the element for which data should be
771 * @return Read/write reference to data.
773 * This operator allows for easy, array-style, data access.
774 * Note that data access with this operator is unchecked and
775 * out_of_range lookups are not defined. (For checked lookups
779 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
780 { return *(this->_M_impl
._M_start
+ __n
); }
783 * @brief Subscript access to the data contained in the %vector.
784 * @param __n The index of the element for which data should be
786 * @return Read-only (constant) reference to data.
788 * This operator allows for easy, array-style, data access.
789 * Note that data access with this operator is unchecked and
790 * out_of_range lookups are not defined. (For checked lookups
794 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
795 { return *(this->_M_impl
._M_start
+ __n
); }
798 /// Safety check used only from at().
800 _M_range_check(size_type __n
) const
802 if (__n
>= this->size())
803 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
804 "(which is %zu) >= this->size() "
811 * @brief Provides access to the data contained in the %vector.
812 * @param __n The index of the element for which data should be
814 * @return Read/write reference to data.
815 * @throw std::out_of_range If @a __n is an invalid index.
817 * This function provides for safer data access. The parameter
818 * is first checked that it is in the range of the vector. The
819 * function throws out_of_range if the check fails.
829 * @brief Provides access to the data contained in the %vector.
830 * @param __n The index of the element for which data should be
832 * @return Read-only (constant) reference to data.
833 * @throw std::out_of_range If @a __n is an invalid index.
835 * This function provides for safer data access. The parameter
836 * is first checked that it is in the range of the vector. The
837 * function throws out_of_range if the check fails.
840 at(size_type __n
) const
847 * Returns a read/write reference to the data at the first
848 * element of the %vector.
851 front() _GLIBCXX_NOEXCEPT
855 * Returns a read-only (constant) reference to the data at the first
856 * element of the %vector.
859 front() const _GLIBCXX_NOEXCEPT
863 * Returns a read/write reference to the data at the last
864 * element of the %vector.
867 back() _GLIBCXX_NOEXCEPT
868 { return *(end() - 1); }
871 * Returns a read-only (constant) reference to the data at the
872 * last element of the %vector.
875 back() const _GLIBCXX_NOEXCEPT
876 { return *(end() - 1); }
878 // _GLIBCXX_RESOLVE_LIB_DEFECTS
879 // DR 464. Suggestion for new member functions in standard containers.
882 * Returns a pointer such that [data(), data() + size()) is a valid
883 * range. For a non-empty %vector, data() == &front().
885 #if __cplusplus >= 201103L
890 data() _GLIBCXX_NOEXCEPT
891 { return _M_data_ptr(this->_M_impl
._M_start
); }
893 #if __cplusplus >= 201103L
898 data() const _GLIBCXX_NOEXCEPT
899 { return _M_data_ptr(this->_M_impl
._M_start
); }
901 // [23.2.4.3] modifiers
903 * @brief Add data to the end of the %vector.
904 * @param __x Data to be added.
906 * This is a typical stack operation. The function creates an
907 * element at the end of the %vector and assigns the given data
908 * to it. Due to the nature of a %vector this operation can be
909 * done in constant time if the %vector has preallocated space
913 push_back(const value_type
& __x
)
915 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
917 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
919 ++this->_M_impl
._M_finish
;
922 #if __cplusplus >= 201103L
923 _M_emplace_back_aux(__x
);
925 _M_insert_aux(end(), __x
);
929 #if __cplusplus >= 201103L
931 push_back(value_type
&& __x
)
932 { emplace_back(std::move(__x
)); }
934 template<typename
... _Args
>
936 emplace_back(_Args
&&... __args
);
940 * @brief Removes last element.
942 * This is a typical stack operation. It shrinks the %vector by one.
944 * Note that no data is returned, and if the last element's
945 * data is needed, it should be retrieved before pop_back() is
949 pop_back() _GLIBCXX_NOEXCEPT
951 --this->_M_impl
._M_finish
;
952 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
955 #if __cplusplus >= 201103L
957 * @brief Inserts an object in %vector before specified iterator.
958 * @param __position A const_iterator into the %vector.
959 * @param __args Arguments.
960 * @return An iterator that points to the inserted data.
962 * This function will insert an object of type T constructed
963 * with T(std::forward<Args>(args)...) before the specified location.
964 * Note that this kind of operation could be expensive for a %vector
965 * and if it is frequently used the user should consider using
968 template<typename
... _Args
>
970 emplace(const_iterator __position
, _Args
&&... __args
);
973 * @brief Inserts given value into %vector before specified iterator.
974 * @param __position A const_iterator into the %vector.
975 * @param __x Data to be inserted.
976 * @return An iterator that points to the inserted data.
978 * This function will insert a copy of the given value before
979 * the specified location. Note that this kind of operation
980 * could be expensive for a %vector and if it is frequently
981 * used the user should consider using std::list.
984 insert(const_iterator __position
, const value_type
& __x
);
987 * @brief Inserts given value into %vector before specified iterator.
988 * @param __position An iterator into the %vector.
989 * @param __x Data to be inserted.
990 * @return An iterator that points to the inserted data.
992 * This function will insert a copy of the given value before
993 * the specified location. Note that this kind of operation
994 * could be expensive for a %vector and if it is frequently
995 * used the user should consider using std::list.
998 insert(iterator __position
, const value_type
& __x
);
1001 #if __cplusplus >= 201103L
1003 * @brief Inserts given rvalue into %vector before specified iterator.
1004 * @param __position A const_iterator into the %vector.
1005 * @param __x Data to be inserted.
1006 * @return An iterator that points to the inserted data.
1008 * This function will insert a copy of the given rvalue before
1009 * the specified location. Note that this kind of operation
1010 * could be expensive for a %vector and if it is frequently
1011 * used the user should consider using std::list.
1014 insert(const_iterator __position
, value_type
&& __x
)
1015 { return emplace(__position
, std::move(__x
)); }
1018 * @brief Inserts an initializer_list into the %vector.
1019 * @param __position An iterator into the %vector.
1020 * @param __l An initializer_list.
1022 * This function will insert copies of the data in the
1023 * initializer_list @a l into the %vector before the location
1024 * specified by @a position.
1026 * Note that this kind of operation could be expensive for a
1027 * %vector and if it is frequently used the user should
1028 * consider using std::list.
1031 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1032 { return this->insert(__position
, __l
.begin(), __l
.end()); }
1035 #if __cplusplus >= 201103L
1037 * @brief Inserts a number of copies of given data into the %vector.
1038 * @param __position A const_iterator into the %vector.
1039 * @param __n Number of elements to be inserted.
1040 * @param __x Data to be inserted.
1041 * @return An iterator that points to the inserted data.
1043 * This function will insert a specified number of copies of
1044 * the given data before the location specified by @a position.
1046 * Note that this kind of operation could be expensive for a
1047 * %vector and if it is frequently used the user should
1048 * consider using std::list.
1051 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1053 difference_type __offset
= __position
- cbegin();
1054 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1055 return begin() + __offset
;
1059 * @brief Inserts a number of copies of given data into the %vector.
1060 * @param __position An iterator into the %vector.
1061 * @param __n Number of elements to be inserted.
1062 * @param __x Data to be inserted.
1064 * This function will insert a specified number of copies of
1065 * the given data before the location specified by @a position.
1067 * Note that this kind of operation could be expensive for a
1068 * %vector and if it is frequently used the user should
1069 * consider using std::list.
1072 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1073 { _M_fill_insert(__position
, __n
, __x
); }
1076 #if __cplusplus >= 201103L
1078 * @brief Inserts a range into the %vector.
1079 * @param __position A const_iterator into the %vector.
1080 * @param __first An input iterator.
1081 * @param __last An input iterator.
1082 * @return An iterator that points to the inserted data.
1084 * This function will insert copies of the data in the range
1085 * [__first,__last) into the %vector before the location specified
1088 * Note that this kind of operation could be expensive for a
1089 * %vector and if it is frequently used the user should
1090 * consider using std::list.
1092 template<typename _InputIterator
,
1093 typename
= std::_RequireInputIter
<_InputIterator
>>
1095 insert(const_iterator __position
, _InputIterator __first
,
1096 _InputIterator __last
)
1098 difference_type __offset
= __position
- cbegin();
1099 _M_insert_dispatch(__position
._M_const_cast(),
1100 __first
, __last
, __false_type());
1101 return begin() + __offset
;
1105 * @brief Inserts a range into the %vector.
1106 * @param __position An iterator into the %vector.
1107 * @param __first An input iterator.
1108 * @param __last An input iterator.
1110 * This function will insert copies of the data in the range
1111 * [__first,__last) into the %vector before the location specified
1114 * Note that this kind of operation could be expensive for a
1115 * %vector and if it is frequently used the user should
1116 * consider using std::list.
1118 template<typename _InputIterator
>
1120 insert(iterator __position
, _InputIterator __first
,
1121 _InputIterator __last
)
1123 // Check whether it's an integral type. If so, it's not an iterator.
1124 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1125 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1130 * @brief Remove element at given position.
1131 * @param __position Iterator pointing to element to be erased.
1132 * @return An iterator pointing to the next element (or end()).
1134 * This function will erase the element at the given position and thus
1135 * shorten the %vector by one.
1137 * Note This operation could be expensive and if it is
1138 * frequently used the user should consider using std::list.
1139 * The user is also cautioned that this function only erases
1140 * the element, and that if the element is itself a pointer,
1141 * the pointed-to memory is not touched in any way. Managing
1142 * the pointer is the user's responsibility.
1145 #if __cplusplus >= 201103L
1146 erase(const_iterator __position
)
1148 erase(iterator __position
)
1150 { return _M_erase(__position
._M_const_cast()); }
1153 * @brief Remove a range of elements.
1154 * @param __first Iterator pointing to the first element to be erased.
1155 * @param __last Iterator pointing to one past the last element to be
1157 * @return An iterator pointing to the element pointed to by @a __last
1158 * prior to erasing (or end()).
1160 * This function will erase the elements in the range
1161 * [__first,__last) and shorten the %vector accordingly.
1163 * Note This operation could be expensive and if it is
1164 * frequently used the user should consider using std::list.
1165 * The user is also cautioned that this function only erases
1166 * the elements, and that if the elements themselves are
1167 * pointers, the pointed-to memory is not touched in any way.
1168 * Managing the pointer is the user's responsibility.
1171 #if __cplusplus >= 201103L
1172 erase(const_iterator __first
, const_iterator __last
)
1174 erase(iterator __first
, iterator __last
)
1176 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1179 * @brief Swaps data with another %vector.
1180 * @param __x A %vector of the same element and allocator types.
1182 * This exchanges the elements between two vectors in constant time.
1183 * (Three pointers, so it should be quite fast.)
1184 * Note that the global std::swap() function is specialized such that
1185 * std::swap(v1,v2) will feed to this function.
1189 #if __cplusplus >= 201103L
1190 noexcept(_Alloc_traits::_S_nothrow_swap())
1193 this->_M_impl
._M_swap_data(__x
._M_impl
);
1194 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1195 __x
._M_get_Tp_allocator());
1199 * Erases all the elements. Note that this function only erases the
1200 * elements, and that if the elements themselves are pointers, the
1201 * pointed-to memory is not touched in any way. Managing the pointer is
1202 * the user's responsibility.
1205 clear() _GLIBCXX_NOEXCEPT
1206 { _M_erase_at_end(this->_M_impl
._M_start
); }
1210 * Memory expansion handler. Uses the member allocation function to
1211 * obtain @a n bytes of memory, and then copies [first,last) into it.
1213 template<typename _ForwardIterator
>
1215 _M_allocate_and_copy(size_type __n
,
1216 _ForwardIterator __first
, _ForwardIterator __last
)
1218 pointer __result
= this->_M_allocate(__n
);
1221 std::__uninitialized_copy_a(__first
, __last
, __result
,
1222 _M_get_Tp_allocator());
1227 _M_deallocate(__result
, __n
);
1228 __throw_exception_again
;
1233 // Internal constructor functions follow.
1235 // Called by the range constructor to implement [23.1.1]/9
1237 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1238 // 438. Ambiguity in the "do the right thing" clause
1239 template<typename _Integer
>
1241 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1243 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1244 this->_M_impl
._M_end_of_storage
=
1245 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1246 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1249 // Called by the range constructor to implement [23.1.1]/9
1250 template<typename _InputIterator
>
1252 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1255 typedef typename
std::iterator_traits
<_InputIterator
>::
1256 iterator_category _IterCategory
;
1257 _M_range_initialize(__first
, __last
, _IterCategory());
1260 // Called by the second initialize_dispatch above
1261 template<typename _InputIterator
>
1263 _M_range_initialize(_InputIterator __first
,
1264 _InputIterator __last
, std::input_iterator_tag
)
1266 for (; __first
!= __last
; ++__first
)
1267 #if __cplusplus >= 201103L
1268 emplace_back(*__first
);
1270 push_back(*__first
);
1274 // Called by the second initialize_dispatch above
1275 template<typename _ForwardIterator
>
1277 _M_range_initialize(_ForwardIterator __first
,
1278 _ForwardIterator __last
, std::forward_iterator_tag
)
1280 const size_type __n
= std::distance(__first
, __last
);
1281 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1282 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1283 this->_M_impl
._M_finish
=
1284 std::__uninitialized_copy_a(__first
, __last
,
1285 this->_M_impl
._M_start
,
1286 _M_get_Tp_allocator());
1289 // Called by the first initialize_dispatch above and by the
1290 // vector(n,value,a) constructor.
1292 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1294 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1295 _M_get_Tp_allocator());
1296 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1299 #if __cplusplus >= 201103L
1300 // Called by the vector(n) constructor.
1302 _M_default_initialize(size_type __n
)
1304 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1305 _M_get_Tp_allocator());
1306 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1310 // Internal assign functions follow. The *_aux functions do the actual
1311 // assignment work for the range versions.
1313 // Called by the range assign to implement [23.1.1]/9
1315 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1316 // 438. Ambiguity in the "do the right thing" clause
1317 template<typename _Integer
>
1319 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1320 { _M_fill_assign(__n
, __val
); }
1322 // Called by the range assign to implement [23.1.1]/9
1323 template<typename _InputIterator
>
1325 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1328 typedef typename
std::iterator_traits
<_InputIterator
>::
1329 iterator_category _IterCategory
;
1330 _M_assign_aux(__first
, __last
, _IterCategory());
1333 // Called by the second assign_dispatch above
1334 template<typename _InputIterator
>
1336 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1337 std::input_iterator_tag
);
1339 // Called by the second assign_dispatch above
1340 template<typename _ForwardIterator
>
1342 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1343 std::forward_iterator_tag
);
1345 // Called by assign(n,t), and the range assign when it turns out
1346 // to be the same thing.
1348 _M_fill_assign(size_type __n
, const value_type
& __val
);
1351 // Internal insert functions follow.
1353 // Called by the range insert 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_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1361 { _M_fill_insert(__pos
, __n
, __val
); }
1363 // Called by the range insert to implement [23.1.1]/9
1364 template<typename _InputIterator
>
1366 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1367 _InputIterator __last
, __false_type
)
1369 typedef typename
std::iterator_traits
<_InputIterator
>::
1370 iterator_category _IterCategory
;
1371 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1374 // Called by the second insert_dispatch above
1375 template<typename _InputIterator
>
1377 _M_range_insert(iterator __pos
, _InputIterator __first
,
1378 _InputIterator __last
, std::input_iterator_tag
);
1380 // Called by the second insert_dispatch above
1381 template<typename _ForwardIterator
>
1383 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1384 _ForwardIterator __last
, std::forward_iterator_tag
);
1386 // Called by insert(p,n,x), and the range insert when it turns out to be
1389 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1391 #if __cplusplus >= 201103L
1392 // Called by resize(n).
1394 _M_default_append(size_type __n
);
1400 // Called by insert(p,x)
1401 #if __cplusplus < 201103L
1403 _M_insert_aux(iterator __position
, const value_type
& __x
);
1405 template<typename
... _Args
>
1407 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1409 template<typename
... _Args
>
1411 _M_emplace_back_aux(_Args
&&... __args
);
1414 // Called by the latter.
1416 _M_check_len(size_type __n
, const char* __s
) const
1418 if (max_size() - size() < __n
)
1419 __throw_length_error(__N(__s
));
1421 const size_type __len
= size() + std::max(size(), __n
);
1422 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1425 // Internal erase functions follow.
1427 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1430 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1432 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1433 this->_M_impl
._M_finish
= __pos
;
1437 _M_erase(iterator __position
);
1440 _M_erase(iterator __first
, iterator __last
);
1442 #if __cplusplus >= 201103L
1444 // Constant-time move assignment when source object's memory can be
1445 // moved, either because the source's allocator will move too
1446 // or because the allocators are equal.
1448 _M_move_assign(vector
&& __x
, std::true_type
) noexcept
1450 vector
__tmp(get_allocator());
1451 this->_M_impl
._M_swap_data(__tmp
._M_impl
);
1452 this->_M_impl
._M_swap_data(__x
._M_impl
);
1453 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
1456 // Do move assignment when it might not be possible to move source
1457 // object's memory, resulting in a linear-time operation.
1459 _M_move_assign(vector
&& __x
, std::false_type
)
1461 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1462 _M_move_assign(std::move(__x
), std::true_type());
1465 // The rvalue's allocator cannot be moved and is not equal,
1466 // so we need to individually move each element.
1467 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1468 std::__make_move_if_noexcept_iterator(__x
.end()));
1474 #if __cplusplus >= 201103L
1475 template<typename _Up
>
1477 _M_data_ptr(_Up
* __ptr
) const
1480 template<typename _Ptr
>
1481 typename
std::pointer_traits
<_Ptr
>::element_type
*
1482 _M_data_ptr(_Ptr __ptr
) const
1483 { return empty() ? nullptr : std::__addressof(*__ptr
); }
1485 template<typename _Ptr
>
1487 _M_data_ptr(_Ptr __ptr
) const
1494 * @brief Vector equality comparison.
1495 * @param __x A %vector.
1496 * @param __y A %vector of the same type as @a __x.
1497 * @return True iff the size and elements of the vectors are equal.
1499 * This is an equivalence relation. It is linear in the size of the
1500 * vectors. Vectors are considered equivalent if their sizes are equal,
1501 * and if corresponding elements compare equal.
1503 template<typename _Tp
, typename _Alloc
>
1505 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1506 { return (__x
.size() == __y
.size()
1507 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1510 * @brief Vector ordering relation.
1511 * @param __x A %vector.
1512 * @param __y A %vector of the same type as @a __x.
1513 * @return True iff @a __x is lexicographically less than @a __y.
1515 * This is a total ordering relation. It is linear in the size of the
1516 * vectors. The elements must be comparable with @c <.
1518 * See std::lexicographical_compare() for how the determination is made.
1520 template<typename _Tp
, typename _Alloc
>
1522 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1523 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1524 __y
.begin(), __y
.end()); }
1526 /// Based on operator==
1527 template<typename _Tp
, typename _Alloc
>
1529 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1530 { return !(__x
== __y
); }
1532 /// Based on operator<
1533 template<typename _Tp
, typename _Alloc
>
1535 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1536 { return __y
< __x
; }
1538 /// Based on operator<
1539 template<typename _Tp
, typename _Alloc
>
1541 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1542 { return !(__y
< __x
); }
1544 /// Based on operator<
1545 template<typename _Tp
, typename _Alloc
>
1547 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1548 { return !(__x
< __y
); }
1550 /// See std::vector::swap().
1551 template<typename _Tp
, typename _Alloc
>
1553 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1556 _GLIBCXX_END_NAMESPACE_CONTAINER
1559 #endif /* _STL_VECTOR_H */