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
)
168 { return __n
!= 0 ? _M_impl
.allocate(__n
) : 0; }
171 _M_deallocate(pointer __p
, size_t __n
)
174 _M_impl
.deallocate(__p
, __n
);
179 _M_create_storage(size_t __n
)
181 this->_M_impl
._M_start
= this->_M_allocate(__n
);
182 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
183 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
189 * @brief A standard container which offers fixed time access to
190 * individual elements in any order.
194 * @tparam _Tp Type of element.
195 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
197 * Meets the requirements of a <a href="tables.html#65">container</a>, a
198 * <a href="tables.html#66">reversible container</a>, and a
199 * <a href="tables.html#67">sequence</a>, including the
200 * <a href="tables.html#68">optional sequence requirements</a> with the
201 * %exception of @c push_front and @c pop_front.
203 * In some terminology a %vector can be described as a dynamic
204 * C-style array, it offers fast and efficient access to individual
205 * elements in any order and saves the user from worrying about
206 * memory and size allocation. Subscripting ( @c [] ) access is
207 * also provided as with C-style arrays.
209 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
210 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
212 // Concept requirements.
213 typedef typename
_Alloc::value_type _Alloc_value_type
;
214 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
215 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
217 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
218 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
219 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
222 typedef _Tp value_type
;
223 typedef typename
_Base::pointer pointer
;
224 typedef typename
_Alloc_traits::const_pointer const_pointer
;
225 typedef typename
_Alloc_traits::reference reference
;
226 typedef typename
_Alloc_traits::const_reference const_reference
;
227 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
228 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
230 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
231 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
232 typedef size_t size_type
;
233 typedef ptrdiff_t difference_type
;
234 typedef _Alloc allocator_type
;
237 using _Base::_M_allocate
;
238 using _Base::_M_deallocate
;
239 using _Base::_M_impl
;
240 using _Base::_M_get_Tp_allocator
;
243 // [23.2.4.1] construct/copy/destroy
244 // (assign() and get_allocator() are also listed in this section)
247 * @brief Creates a %vector with no elements.
249 vector() _GLIBCXX_NOEXCEPT
: _Base() { }
252 * @brief Creates a %vector with no elements.
253 * @param __a An allocator object.
256 vector(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
259 #if __cplusplus >= 201103L
261 * @brief Creates a %vector with default constructed elements.
262 * @param __n The number of elements to initially create.
263 * @param __a An allocator.
265 * This constructor fills the %vector with @a __n default
266 * constructed elements.
269 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
271 { _M_default_initialize(__n
); }
274 * @brief Creates a %vector with copies of an exemplar element.
275 * @param __n The number of elements to initially create.
276 * @param __value An element to copy.
277 * @param __a An allocator.
279 * This constructor fills the %vector with @a __n copies of @a __value.
281 vector(size_type __n
, const value_type
& __value
,
282 const allocator_type
& __a
= allocator_type())
284 { _M_fill_initialize(__n
, __value
); }
287 * @brief Creates a %vector with copies of an exemplar element.
288 * @param __n The number of elements to initially create.
289 * @param __value An element to copy.
290 * @param __a An allocator.
292 * This constructor fills the %vector with @a __n copies of @a __value.
295 vector(size_type __n
, const value_type
& __value
= value_type(),
296 const allocator_type
& __a
= allocator_type())
298 { _M_fill_initialize(__n
, __value
); }
302 * @brief %Vector copy constructor.
303 * @param __x A %vector of identical element and allocator types.
305 * The newly-created %vector uses a copy of the allocation
306 * object used by @a __x. All the elements of @a __x are copied,
307 * but any extra memory in
308 * @a __x (for fast expansion) will not be copied.
310 vector(const vector
& __x
)
312 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
313 { this->_M_impl
._M_finish
=
314 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
315 this->_M_impl
._M_start
,
316 _M_get_Tp_allocator());
319 #if __cplusplus >= 201103L
321 * @brief %Vector move constructor.
322 * @param __x A %vector of identical element and allocator types.
324 * The newly-created %vector contains the exact contents of @a __x.
325 * The contents of @a __x are a valid, but unspecified %vector.
327 vector(vector
&& __x
) noexcept
328 : _Base(std::move(__x
)) { }
330 /// Copy constructor with alternative allocator
331 vector(const vector
& __x
, const allocator_type
& __a
)
332 : _Base(__x
.size(), __a
)
333 { this->_M_impl
._M_finish
=
334 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
335 this->_M_impl
._M_start
,
336 _M_get_Tp_allocator());
339 /// Move constructor with alternative allocator
340 vector(vector
&& __rv
, const allocator_type
& __m
)
341 noexcept(_Alloc_traits::_S_always_equal())
342 : _Base(std::move(__rv
), __m
)
344 if (__rv
.get_allocator() != __m
)
346 this->_M_impl
._M_finish
=
347 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
348 this->_M_impl
._M_start
,
349 _M_get_Tp_allocator());
355 * @brief Builds a %vector from an initializer list.
356 * @param __l An initializer_list.
357 * @param __a An allocator.
359 * Create a %vector consisting of copies of the elements in the
360 * initializer_list @a __l.
362 * This will call the element type's copy constructor N times
363 * (where N is @a __l.size()) and do no memory reallocation.
365 vector(initializer_list
<value_type
> __l
,
366 const allocator_type
& __a
= allocator_type())
369 _M_range_initialize(__l
.begin(), __l
.end(),
370 random_access_iterator_tag());
375 * @brief Builds a %vector from a range.
376 * @param __first An input iterator.
377 * @param __last An input iterator.
378 * @param __a An allocator.
380 * Create a %vector consisting of copies of the elements from
383 * If the iterators are forward, bidirectional, or
384 * random-access, then this will call the elements' copy
385 * constructor N times (where N is distance(first,last)) and do
386 * no memory reallocation. But if only input iterators are
387 * used, then this will do at most 2N calls to the copy
388 * constructor, and logN memory reallocations.
390 #if __cplusplus >= 201103L
391 template<typename _InputIterator
,
392 typename
= std::_RequireInputIter
<_InputIterator
>>
393 vector(_InputIterator __first
, _InputIterator __last
,
394 const allocator_type
& __a
= allocator_type())
396 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
398 template<typename _InputIterator
>
399 vector(_InputIterator __first
, _InputIterator __last
,
400 const allocator_type
& __a
= allocator_type())
403 // Check whether it's an integral type. If so, it's not an iterator.
404 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
405 _M_initialize_dispatch(__first
, __last
, _Integral());
410 * The dtor only erases the elements, and note that if the
411 * elements themselves are pointers, the pointed-to memory is
412 * not touched in any way. Managing the pointer is the user's
415 ~vector() _GLIBCXX_NOEXCEPT
416 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
417 _M_get_Tp_allocator()); }
420 * @brief %Vector assignment operator.
421 * @param __x A %vector of identical element and allocator types.
423 * All the elements of @a __x are copied, but any extra memory in
424 * @a __x (for fast expansion) will not be copied. Unlike the
425 * copy constructor, the allocator object is not copied.
428 operator=(const vector
& __x
);
430 #if __cplusplus >= 201103L
432 * @brief %Vector move assignment operator.
433 * @param __x A %vector of identical element and allocator types.
435 * The contents of @a __x are moved into this %vector (without copying,
436 * if the allocators permit it).
437 * @a __x is a valid, but unspecified %vector.
440 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
442 constexpr bool __move_storage
=
443 _Alloc_traits::_S_propagate_on_move_assign()
444 || _Alloc_traits::_S_always_equal();
445 _M_move_assign(std::move(__x
),
446 integral_constant
<bool, __move_storage
>());
451 * @brief %Vector list assignment operator.
452 * @param __l An initializer_list.
454 * This function fills a %vector with copies of the elements in the
455 * initializer list @a __l.
457 * Note that the assignment completely changes the %vector and
458 * that the resulting %vector's size is the same as the number
459 * of elements assigned. Old data may be lost.
462 operator=(initializer_list
<value_type
> __l
)
464 this->assign(__l
.begin(), __l
.end());
470 * @brief Assigns a given value to a %vector.
471 * @param __n Number of elements to be assigned.
472 * @param __val Value to be assigned.
474 * This function fills a %vector with @a __n copies of the given
475 * value. Note that the assignment completely changes the
476 * %vector and that the resulting %vector's size is the same as
477 * the number of elements assigned. Old data may be lost.
480 assign(size_type __n
, const value_type
& __val
)
481 { _M_fill_assign(__n
, __val
); }
484 * @brief Assigns a range to a %vector.
485 * @param __first An input iterator.
486 * @param __last An input iterator.
488 * This function fills a %vector with copies of the elements in the
489 * range [__first,__last).
491 * Note that the assignment completely changes the %vector and
492 * that the resulting %vector's size is the same as the number
493 * of elements assigned. Old data may be lost.
495 #if __cplusplus >= 201103L
496 template<typename _InputIterator
,
497 typename
= std::_RequireInputIter
<_InputIterator
>>
499 assign(_InputIterator __first
, _InputIterator __last
)
500 { _M_assign_dispatch(__first
, __last
, __false_type()); }
502 template<typename _InputIterator
>
504 assign(_InputIterator __first
, _InputIterator __last
)
506 // Check whether it's an integral type. If so, it's not an iterator.
507 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
508 _M_assign_dispatch(__first
, __last
, _Integral());
512 #if __cplusplus >= 201103L
514 * @brief Assigns an initializer list to a %vector.
515 * @param __l An initializer_list.
517 * This function fills a %vector with copies of the elements in the
518 * initializer list @a __l.
520 * Note that the assignment completely changes the %vector and
521 * that the resulting %vector's size is the same as the number
522 * of elements assigned. Old data may be lost.
525 assign(initializer_list
<value_type
> __l
)
526 { this->assign(__l
.begin(), __l
.end()); }
529 /// Get a copy of the memory allocation object.
530 using _Base::get_allocator
;
534 * Returns a read/write iterator that points to the first
535 * element in the %vector. Iteration is done in ordinary
539 begin() _GLIBCXX_NOEXCEPT
540 { return iterator(this->_M_impl
._M_start
); }
543 * Returns a read-only (constant) iterator that points to the
544 * first element in the %vector. Iteration is done in ordinary
548 begin() const _GLIBCXX_NOEXCEPT
549 { return const_iterator(this->_M_impl
._M_start
); }
552 * Returns a read/write iterator that points one past the last
553 * element in the %vector. Iteration is done in ordinary
557 end() _GLIBCXX_NOEXCEPT
558 { return iterator(this->_M_impl
._M_finish
); }
561 * Returns a read-only (constant) iterator that points one past
562 * the last element in the %vector. Iteration is done in
563 * ordinary element order.
566 end() const _GLIBCXX_NOEXCEPT
567 { return const_iterator(this->_M_impl
._M_finish
); }
570 * Returns a read/write reverse iterator that points to the
571 * last element in the %vector. Iteration is done in reverse
575 rbegin() _GLIBCXX_NOEXCEPT
576 { return reverse_iterator(end()); }
579 * Returns a read-only (constant) reverse iterator that points
580 * to the last element in the %vector. Iteration is done in
581 * reverse element order.
583 const_reverse_iterator
584 rbegin() const _GLIBCXX_NOEXCEPT
585 { return const_reverse_iterator(end()); }
588 * Returns a read/write reverse iterator that points to one
589 * before the first element in the %vector. Iteration is done
590 * in reverse element order.
593 rend() _GLIBCXX_NOEXCEPT
594 { return reverse_iterator(begin()); }
597 * Returns a read-only (constant) reverse iterator that points
598 * to one before the first element in the %vector. Iteration
599 * is done in reverse element order.
601 const_reverse_iterator
602 rend() const _GLIBCXX_NOEXCEPT
603 { return const_reverse_iterator(begin()); }
605 #if __cplusplus >= 201103L
607 * Returns a read-only (constant) iterator that points to the
608 * first element in the %vector. Iteration is done in ordinary
612 cbegin() const noexcept
613 { return const_iterator(this->_M_impl
._M_start
); }
616 * Returns a read-only (constant) iterator that points one past
617 * the last element in the %vector. Iteration is done in
618 * ordinary element order.
621 cend() const noexcept
622 { return const_iterator(this->_M_impl
._M_finish
); }
625 * Returns a read-only (constant) reverse iterator that points
626 * to the last element in the %vector. Iteration is done in
627 * reverse element order.
629 const_reverse_iterator
630 crbegin() const noexcept
631 { return const_reverse_iterator(end()); }
634 * Returns a read-only (constant) reverse iterator that points
635 * to one before the first element in the %vector. Iteration
636 * is done in reverse element order.
638 const_reverse_iterator
639 crend() const noexcept
640 { return const_reverse_iterator(begin()); }
643 // [23.2.4.2] capacity
644 /** Returns the number of elements in the %vector. */
646 size() const _GLIBCXX_NOEXCEPT
647 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
649 /** Returns the size() of the largest possible %vector. */
651 max_size() const _GLIBCXX_NOEXCEPT
652 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
654 #if __cplusplus >= 201103L
656 * @brief Resizes the %vector to the specified number of elements.
657 * @param __new_size Number of elements the %vector should contain.
659 * This function will %resize the %vector to the specified
660 * number of elements. If the number is smaller than the
661 * %vector's current size the %vector is truncated, otherwise
662 * default constructed elements are appended.
665 resize(size_type __new_size
)
667 if (__new_size
> size())
668 _M_default_append(__new_size
- size());
669 else if (__new_size
< size())
670 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
674 * @brief Resizes the %vector to the specified number of elements.
675 * @param __new_size Number of elements the %vector should contain.
676 * @param __x Data with which new elements should be populated.
678 * This function will %resize the %vector to the specified
679 * number of elements. If the number is smaller than the
680 * %vector's current size the %vector is truncated, otherwise
681 * the %vector is extended and new elements are populated with
685 resize(size_type __new_size
, const value_type
& __x
)
687 if (__new_size
> size())
688 insert(end(), __new_size
- size(), __x
);
689 else if (__new_size
< size())
690 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
694 * @brief Resizes the %vector to the specified number of elements.
695 * @param __new_size Number of elements the %vector should contain.
696 * @param __x Data with which new elements should be populated.
698 * This function will %resize the %vector to the specified
699 * number of elements. If the number is smaller than the
700 * %vector's current size the %vector is truncated, otherwise
701 * the %vector is extended and new elements are populated with
705 resize(size_type __new_size
, value_type __x
= value_type())
707 if (__new_size
> size())
708 insert(end(), __new_size
- size(), __x
);
709 else if (__new_size
< size())
710 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
714 #if __cplusplus >= 201103L
715 /** A non-binding request to reduce capacity() to size(). */
718 { _M_shrink_to_fit(); }
722 * Returns the total number of elements that the %vector can
723 * hold before needing to allocate more memory.
726 capacity() const _GLIBCXX_NOEXCEPT
727 { return size_type(this->_M_impl
._M_end_of_storage
728 - this->_M_impl
._M_start
); }
731 * Returns true if the %vector is empty. (Thus begin() would
735 empty() const _GLIBCXX_NOEXCEPT
736 { return begin() == end(); }
739 * @brief Attempt to preallocate enough memory for specified number of
741 * @param __n Number of elements required.
742 * @throw std::length_error If @a n exceeds @c max_size().
744 * This function attempts to reserve enough memory for the
745 * %vector to hold the specified number of elements. If the
746 * number requested is more than max_size(), length_error is
749 * The advantage of this function is that if optimal code is a
750 * necessity and the user can determine the number of elements
751 * that will be required, the user can reserve the memory in
752 * %advance, and thus prevent a possible reallocation of memory
753 * and copying of %vector data.
756 reserve(size_type __n
);
760 * @brief Subscript access to the data contained in the %vector.
761 * @param __n The index of the element for which data should be
763 * @return Read/write reference to data.
765 * This operator allows for easy, array-style, data access.
766 * Note that data access with this operator is unchecked and
767 * out_of_range lookups are not defined. (For checked lookups
771 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
772 { return *(this->_M_impl
._M_start
+ __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-only (constant) 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
) const _GLIBCXX_NOEXCEPT
787 { return *(this->_M_impl
._M_start
+ __n
); }
790 /// Safety check used only from at().
792 _M_range_check(size_type __n
) const
794 if (__n
>= this->size())
795 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
796 "(which is %zu) >= this->size() "
803 * @brief Provides access to the data contained in the %vector.
804 * @param __n The index of the element for which data should be
806 * @return Read/write reference to data.
807 * @throw std::out_of_range If @a __n is an invalid index.
809 * This function provides for safer data access. The parameter
810 * is first checked that it is in the range of the vector. The
811 * function throws out_of_range if the check fails.
821 * @brief Provides access to the data contained in the %vector.
822 * @param __n The index of the element for which data should be
824 * @return Read-only (constant) reference to data.
825 * @throw std::out_of_range If @a __n is an invalid index.
827 * This function provides for safer data access. The parameter
828 * is first checked that it is in the range of the vector. The
829 * function throws out_of_range if the check fails.
832 at(size_type __n
) const
839 * Returns a read/write reference to the data at the first
840 * element of the %vector.
843 front() _GLIBCXX_NOEXCEPT
847 * Returns a read-only (constant) reference to the data at the first
848 * element of the %vector.
851 front() const _GLIBCXX_NOEXCEPT
855 * Returns a read/write reference to the data at the last
856 * element of the %vector.
859 back() _GLIBCXX_NOEXCEPT
860 { return *(end() - 1); }
863 * Returns a read-only (constant) reference to the data at the
864 * last element of the %vector.
867 back() const _GLIBCXX_NOEXCEPT
868 { return *(end() - 1); }
870 // _GLIBCXX_RESOLVE_LIB_DEFECTS
871 // DR 464. Suggestion for new member functions in standard containers.
874 * Returns a pointer such that [data(), data() + size()) is a valid
875 * range. For a non-empty %vector, data() == &front().
877 #if __cplusplus >= 201103L
882 data() _GLIBCXX_NOEXCEPT
883 { return std::__addressof(front()); }
885 #if __cplusplus >= 201103L
890 data() const _GLIBCXX_NOEXCEPT
891 { return std::__addressof(front()); }
893 // [23.2.4.3] modifiers
895 * @brief Add data to the end of the %vector.
896 * @param __x Data to be added.
898 * This is a typical stack operation. The function creates an
899 * element at the end of the %vector and assigns the given data
900 * to it. Due to the nature of a %vector this operation can be
901 * done in constant time if the %vector has preallocated space
905 push_back(const value_type
& __x
)
907 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
909 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
911 ++this->_M_impl
._M_finish
;
914 #if __cplusplus >= 201103L
915 _M_emplace_back_aux(__x
);
917 _M_insert_aux(end(), __x
);
921 #if __cplusplus >= 201103L
923 push_back(value_type
&& __x
)
924 { emplace_back(std::move(__x
)); }
926 template<typename
... _Args
>
928 emplace_back(_Args
&&... __args
);
932 * @brief Removes last element.
934 * This is a typical stack operation. It shrinks the %vector by one.
936 * Note that no data is returned, and if the last element's
937 * data is needed, it should be retrieved before pop_back() is
941 pop_back() _GLIBCXX_NOEXCEPT
943 --this->_M_impl
._M_finish
;
944 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
947 #if __cplusplus >= 201103L
949 * @brief Inserts an object in %vector before specified iterator.
950 * @param __position A const_iterator into the %vector.
951 * @param __args Arguments.
952 * @return An iterator that points to the inserted data.
954 * This function will insert an object of type T constructed
955 * with T(std::forward<Args>(args)...) before the specified location.
956 * Note that this kind of operation could be expensive for a %vector
957 * and if it is frequently used the user should consider using
960 template<typename
... _Args
>
962 emplace(const_iterator __position
, _Args
&&... __args
);
965 * @brief Inserts given value into %vector before specified iterator.
966 * @param __position A const_iterator into the %vector.
967 * @param __x Data to be inserted.
968 * @return An iterator that points to the inserted data.
970 * This function will insert a copy of the given value before
971 * the specified location. Note that this kind of operation
972 * could be expensive for a %vector and if it is frequently
973 * used the user should consider using std::list.
976 insert(const_iterator __position
, const value_type
& __x
);
979 * @brief Inserts given value into %vector before specified iterator.
980 * @param __position An iterator into the %vector.
981 * @param __x Data to be inserted.
982 * @return An iterator that points to the inserted data.
984 * This function will insert a copy of the given value before
985 * the specified location. Note that this kind of operation
986 * could be expensive for a %vector and if it is frequently
987 * used the user should consider using std::list.
990 insert(iterator __position
, const value_type
& __x
);
993 #if __cplusplus >= 201103L
995 * @brief Inserts given rvalue into %vector before specified iterator.
996 * @param __position A const_iterator into the %vector.
997 * @param __x Data to be inserted.
998 * @return An iterator that points to the inserted data.
1000 * This function will insert a copy of the given rvalue before
1001 * the specified location. Note that this kind of operation
1002 * could be expensive for a %vector and if it is frequently
1003 * used the user should consider using std::list.
1006 insert(const_iterator __position
, value_type
&& __x
)
1007 { return emplace(__position
, std::move(__x
)); }
1010 * @brief Inserts an initializer_list into the %vector.
1011 * @param __position An iterator into the %vector.
1012 * @param __l An initializer_list.
1014 * This function will insert copies of the data in the
1015 * initializer_list @a l into the %vector before the location
1016 * specified by @a position.
1018 * Note that this kind of operation could be expensive for a
1019 * %vector and if it is frequently used the user should
1020 * consider using std::list.
1023 insert(const_iterator __position
, initializer_list
<value_type
> __l
)
1024 { return this->insert(__position
, __l
.begin(), __l
.end()); }
1027 #if __cplusplus >= 201103L
1029 * @brief Inserts a number of copies of given data into the %vector.
1030 * @param __position A const_iterator into the %vector.
1031 * @param __n Number of elements to be inserted.
1032 * @param __x Data to be inserted.
1033 * @return An iterator that points to the inserted data.
1035 * This function will insert a specified number of copies of
1036 * the given data before the location specified by @a position.
1038 * Note that this kind of operation could be expensive for a
1039 * %vector and if it is frequently used the user should
1040 * consider using std::list.
1043 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1045 difference_type __offset
= __position
- cbegin();
1046 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1047 return begin() + __offset
;
1051 * @brief Inserts a number of copies of given data into the %vector.
1052 * @param __position An iterator into the %vector.
1053 * @param __n Number of elements to be inserted.
1054 * @param __x Data to be inserted.
1056 * This function will insert a specified number of copies of
1057 * the given data before the location specified by @a position.
1059 * Note that this kind of operation could be expensive for a
1060 * %vector and if it is frequently used the user should
1061 * consider using std::list.
1064 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1065 { _M_fill_insert(__position
, __n
, __x
); }
1068 #if __cplusplus >= 201103L
1070 * @brief Inserts a range into the %vector.
1071 * @param __position A const_iterator into the %vector.
1072 * @param __first An input iterator.
1073 * @param __last An input iterator.
1074 * @return An iterator that points to the inserted data.
1076 * This function will insert copies of the data in the range
1077 * [__first,__last) into the %vector before the location specified
1080 * Note that this kind of operation could be expensive for a
1081 * %vector and if it is frequently used the user should
1082 * consider using std::list.
1084 template<typename _InputIterator
,
1085 typename
= std::_RequireInputIter
<_InputIterator
>>
1087 insert(const_iterator __position
, _InputIterator __first
,
1088 _InputIterator __last
)
1090 difference_type __offset
= __position
- cbegin();
1091 _M_insert_dispatch(__position
._M_const_cast(),
1092 __first
, __last
, __false_type());
1093 return begin() + __offset
;
1097 * @brief Inserts a range into the %vector.
1098 * @param __position An iterator into the %vector.
1099 * @param __first An input iterator.
1100 * @param __last An input iterator.
1102 * This function will insert copies of the data in the range
1103 * [__first,__last) into the %vector before the location specified
1106 * Note that this kind of operation could be expensive for a
1107 * %vector and if it is frequently used the user should
1108 * consider using std::list.
1110 template<typename _InputIterator
>
1112 insert(iterator __position
, _InputIterator __first
,
1113 _InputIterator __last
)
1115 // Check whether it's an integral type. If so, it's not an iterator.
1116 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1117 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1122 * @brief Remove element at given position.
1123 * @param __position Iterator pointing to element to be erased.
1124 * @return An iterator pointing to the next element (or end()).
1126 * This function will erase the element at the given position and thus
1127 * shorten the %vector by one.
1129 * Note This operation could be expensive and if it is
1130 * frequently used the user should consider using std::list.
1131 * The user is also cautioned that this function only erases
1132 * the element, and that if the element is itself a pointer,
1133 * the pointed-to memory is not touched in any way. Managing
1134 * the pointer is the user's responsibility.
1137 #if __cplusplus >= 201103L
1138 erase(const_iterator __position
)
1140 erase(iterator __position
)
1142 { return _M_erase(__position
._M_const_cast()); }
1145 * @brief Remove a range of elements.
1146 * @param __first Iterator pointing to the first element to be erased.
1147 * @param __last Iterator pointing to one past the last element to be
1149 * @return An iterator pointing to the element pointed to by @a __last
1150 * prior to erasing (or end()).
1152 * This function will erase the elements in the range
1153 * [__first,__last) and shorten the %vector accordingly.
1155 * Note This operation could be expensive and if it is
1156 * frequently used the user should consider using std::list.
1157 * The user is also cautioned that this function only erases
1158 * the elements, and that if the elements themselves are
1159 * pointers, the pointed-to memory is not touched in any way.
1160 * Managing the pointer is the user's responsibility.
1163 #if __cplusplus >= 201103L
1164 erase(const_iterator __first
, const_iterator __last
)
1166 erase(iterator __first
, iterator __last
)
1168 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1171 * @brief Swaps data with another %vector.
1172 * @param __x A %vector of the same element and allocator types.
1174 * This exchanges the elements between two vectors in constant time.
1175 * (Three pointers, so it should be quite fast.)
1176 * Note that the global std::swap() function is specialized such that
1177 * std::swap(v1,v2) will feed to this function.
1181 #if __cplusplus >= 201103L
1182 noexcept(_Alloc_traits::_S_nothrow_swap())
1185 this->_M_impl
._M_swap_data(__x
._M_impl
);
1186 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1187 __x
._M_get_Tp_allocator());
1191 * Erases all the elements. Note that this function only erases the
1192 * elements, and that if the elements themselves are pointers, the
1193 * pointed-to memory is not touched in any way. Managing the pointer is
1194 * the user's responsibility.
1197 clear() _GLIBCXX_NOEXCEPT
1198 { _M_erase_at_end(this->_M_impl
._M_start
); }
1202 * Memory expansion handler. Uses the member allocation function to
1203 * obtain @a n bytes of memory, and then copies [first,last) into it.
1205 template<typename _ForwardIterator
>
1207 _M_allocate_and_copy(size_type __n
,
1208 _ForwardIterator __first
, _ForwardIterator __last
)
1210 pointer __result
= this->_M_allocate(__n
);
1213 std::__uninitialized_copy_a(__first
, __last
, __result
,
1214 _M_get_Tp_allocator());
1219 _M_deallocate(__result
, __n
);
1220 __throw_exception_again
;
1225 // Internal constructor functions follow.
1227 // Called by the range constructor to implement [23.1.1]/9
1229 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1230 // 438. Ambiguity in the "do the right thing" clause
1231 template<typename _Integer
>
1233 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1235 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1236 this->_M_impl
._M_end_of_storage
=
1237 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1238 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1241 // Called by the range constructor to implement [23.1.1]/9
1242 template<typename _InputIterator
>
1244 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1247 typedef typename
std::iterator_traits
<_InputIterator
>::
1248 iterator_category _IterCategory
;
1249 _M_range_initialize(__first
, __last
, _IterCategory());
1252 // Called by the second initialize_dispatch above
1253 template<typename _InputIterator
>
1255 _M_range_initialize(_InputIterator __first
,
1256 _InputIterator __last
, std::input_iterator_tag
)
1258 for (; __first
!= __last
; ++__first
)
1259 #if __cplusplus >= 201103L
1260 emplace_back(*__first
);
1262 push_back(*__first
);
1266 // Called by the second initialize_dispatch above
1267 template<typename _ForwardIterator
>
1269 _M_range_initialize(_ForwardIterator __first
,
1270 _ForwardIterator __last
, std::forward_iterator_tag
)
1272 const size_type __n
= std::distance(__first
, __last
);
1273 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1274 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1275 this->_M_impl
._M_finish
=
1276 std::__uninitialized_copy_a(__first
, __last
,
1277 this->_M_impl
._M_start
,
1278 _M_get_Tp_allocator());
1281 // Called by the first initialize_dispatch above and by the
1282 // vector(n,value,a) constructor.
1284 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1286 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1287 _M_get_Tp_allocator());
1288 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1291 #if __cplusplus >= 201103L
1292 // Called by the vector(n) constructor.
1294 _M_default_initialize(size_type __n
)
1296 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1297 _M_get_Tp_allocator());
1298 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1302 // Internal assign functions follow. The *_aux functions do the actual
1303 // assignment work for the range versions.
1305 // Called by the range assign to implement [23.1.1]/9
1307 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1308 // 438. Ambiguity in the "do the right thing" clause
1309 template<typename _Integer
>
1311 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1312 { _M_fill_assign(__n
, __val
); }
1314 // Called by the range assign to implement [23.1.1]/9
1315 template<typename _InputIterator
>
1317 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1320 typedef typename
std::iterator_traits
<_InputIterator
>::
1321 iterator_category _IterCategory
;
1322 _M_assign_aux(__first
, __last
, _IterCategory());
1325 // Called by the second assign_dispatch above
1326 template<typename _InputIterator
>
1328 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1329 std::input_iterator_tag
);
1331 // Called by the second assign_dispatch above
1332 template<typename _ForwardIterator
>
1334 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1335 std::forward_iterator_tag
);
1337 // Called by assign(n,t), and the range assign when it turns out
1338 // to be the same thing.
1340 _M_fill_assign(size_type __n
, const value_type
& __val
);
1343 // Internal insert functions follow.
1345 // Called by the range insert to implement [23.1.1]/9
1347 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1348 // 438. Ambiguity in the "do the right thing" clause
1349 template<typename _Integer
>
1351 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1353 { _M_fill_insert(__pos
, __n
, __val
); }
1355 // Called by the range insert to implement [23.1.1]/9
1356 template<typename _InputIterator
>
1358 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1359 _InputIterator __last
, __false_type
)
1361 typedef typename
std::iterator_traits
<_InputIterator
>::
1362 iterator_category _IterCategory
;
1363 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1366 // Called by the second insert_dispatch above
1367 template<typename _InputIterator
>
1369 _M_range_insert(iterator __pos
, _InputIterator __first
,
1370 _InputIterator __last
, std::input_iterator_tag
);
1372 // Called by the second insert_dispatch above
1373 template<typename _ForwardIterator
>
1375 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1376 _ForwardIterator __last
, std::forward_iterator_tag
);
1378 // Called by insert(p,n,x), and the range insert when it turns out to be
1381 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1383 #if __cplusplus >= 201103L
1384 // Called by resize(n).
1386 _M_default_append(size_type __n
);
1392 // Called by insert(p,x)
1393 #if __cplusplus < 201103L
1395 _M_insert_aux(iterator __position
, const value_type
& __x
);
1397 template<typename
... _Args
>
1399 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1401 template<typename
... _Args
>
1403 _M_emplace_back_aux(_Args
&&... __args
);
1406 // Called by the latter.
1408 _M_check_len(size_type __n
, const char* __s
) const
1410 if (max_size() - size() < __n
)
1411 __throw_length_error(__N(__s
));
1413 const size_type __len
= size() + std::max(size(), __n
);
1414 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1417 // Internal erase functions follow.
1419 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1422 _M_erase_at_end(pointer __pos
) _GLIBCXX_NOEXCEPT
1424 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1425 this->_M_impl
._M_finish
= __pos
;
1429 _M_erase(iterator __position
);
1432 _M_erase(iterator __first
, iterator __last
);
1434 #if __cplusplus >= 201103L
1436 // Constant-time move assignment when source object's memory can be
1437 // moved, either because the source's allocator will move too
1438 // or because the allocators are equal.
1440 _M_move_assign(vector
&& __x
, std::true_type
) noexcept
1442 vector
__tmp(get_allocator());
1443 this->_M_impl
._M_swap_data(__tmp
._M_impl
);
1444 this->_M_impl
._M_swap_data(__x
._M_impl
);
1445 if (_Alloc_traits::_S_propagate_on_move_assign())
1446 std::__alloc_on_move(_M_get_Tp_allocator(),
1447 __x
._M_get_Tp_allocator());
1450 // Do move assignment when it might not be possible to move source
1451 // object's memory, resulting in a linear-time operation.
1453 _M_move_assign(vector
&& __x
, std::false_type
)
1455 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1456 _M_move_assign(std::move(__x
), std::true_type());
1459 // The rvalue's allocator cannot be moved and is not equal,
1460 // so we need to individually move each element.
1461 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
1462 std::__make_move_if_noexcept_iterator(__x
.end()));
1471 * @brief Vector equality comparison.
1472 * @param __x A %vector.
1473 * @param __y A %vector of the same type as @a __x.
1474 * @return True iff the size and elements of the vectors are equal.
1476 * This is an equivalence relation. It is linear in the size of the
1477 * vectors. Vectors are considered equivalent if their sizes are equal,
1478 * and if corresponding elements compare equal.
1480 template<typename _Tp
, typename _Alloc
>
1482 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1483 { return (__x
.size() == __y
.size()
1484 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1487 * @brief Vector ordering relation.
1488 * @param __x A %vector.
1489 * @param __y A %vector of the same type as @a __x.
1490 * @return True iff @a __x is lexicographically less than @a __y.
1492 * This is a total ordering relation. It is linear in the size of the
1493 * vectors. The elements must be comparable with @c <.
1495 * See std::lexicographical_compare() for how the determination is made.
1497 template<typename _Tp
, typename _Alloc
>
1499 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1500 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1501 __y
.begin(), __y
.end()); }
1503 /// Based on operator==
1504 template<typename _Tp
, typename _Alloc
>
1506 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1507 { return !(__x
== __y
); }
1509 /// Based on operator<
1510 template<typename _Tp
, typename _Alloc
>
1512 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1513 { return __y
< __x
; }
1515 /// Based on operator<
1516 template<typename _Tp
, typename _Alloc
>
1518 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1519 { return !(__y
< __x
); }
1521 /// Based on operator<
1522 template<typename _Tp
, typename _Alloc
>
1524 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1525 { return !(__x
< __y
); }
1527 /// See std::vector::swap().
1528 template<typename _Tp
, typename _Alloc
>
1530 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1533 _GLIBCXX_END_NAMESPACE_CONTAINER
1536 #endif /* _STL_VECTOR_H */