1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_vector.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
63 #define _STL_VECTOR_H 1
65 #include <bits/stl_iterator_base_funcs.h>
66 #include <bits/functexcept.h>
67 #include <bits/concept_check.h>
68 #include <initializer_list>
70 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
72 /// See bits/stl_deque.h's _Deque_base for an explanation.
73 template<typename _Tp
, typename _Alloc
>
76 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
79 : public _Tp_alloc_type
81 typename
_Tp_alloc_type::pointer _M_start
;
82 typename
_Tp_alloc_type::pointer _M_finish
;
83 typename
_Tp_alloc_type::pointer _M_end_of_storage
;
86 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
89 _Vector_impl(_Tp_alloc_type
const& __a
)
90 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
95 typedef _Alloc allocator_type
;
99 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
101 const _Tp_alloc_type
&
102 _M_get_Tp_allocator() const
103 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
106 get_allocator() const
107 { return allocator_type(_M_get_Tp_allocator()); }
112 _Vector_base(const allocator_type
& __a
)
115 _Vector_base(size_t __n
, const allocator_type
& __a
)
118 this->_M_impl
._M_start
= this->_M_allocate(__n
);
119 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
120 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 _Vector_base(_Vector_base
&& __x
)
125 : _M_impl(__x
._M_get_Tp_allocator())
127 this->_M_impl
._M_start
= __x
._M_impl
._M_start
;
128 this->_M_impl
._M_finish
= __x
._M_impl
._M_finish
;
129 this->_M_impl
._M_end_of_storage
= __x
._M_impl
._M_end_of_storage
;
130 __x
._M_impl
._M_start
= 0;
131 __x
._M_impl
._M_finish
= 0;
132 __x
._M_impl
._M_end_of_storage
= 0;
137 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
138 - this->_M_impl
._M_start
); }
141 _Vector_impl _M_impl
;
143 typename
_Tp_alloc_type::pointer
144 _M_allocate(size_t __n
)
145 { return __n
!= 0 ? _M_impl
.allocate(__n
) : 0; }
148 _M_deallocate(typename
_Tp_alloc_type::pointer __p
, size_t __n
)
151 _M_impl
.deallocate(__p
, __n
);
157 * @brief A standard container which offers fixed time access to
158 * individual elements in any order.
160 * @ingroup Containers
163 * Meets the requirements of a <a href="tables.html#65">container</a>, a
164 * <a href="tables.html#66">reversible container</a>, and a
165 * <a href="tables.html#67">sequence</a>, including the
166 * <a href="tables.html#68">optional sequence requirements</a> with the
167 * %exception of @c push_front and @c pop_front.
169 * In some terminology a %vector can be described as a dynamic
170 * C-style array, it offers fast and efficient access to individual
171 * elements in any order and saves the user from worrying about
172 * memory and size allocation. Subscripting ( @c [] ) access is
173 * also provided as with C-style arrays.
175 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
176 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
178 // Concept requirements.
179 typedef typename
_Alloc::value_type _Alloc_value_type
;
180 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
181 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
183 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
184 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
187 typedef _Tp value_type
;
188 typedef typename
_Tp_alloc_type::pointer pointer
;
189 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
190 typedef typename
_Tp_alloc_type::reference reference
;
191 typedef typename
_Tp_alloc_type::const_reference const_reference
;
192 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
193 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
195 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
196 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
197 typedef size_t size_type
;
198 typedef ptrdiff_t difference_type
;
199 typedef _Alloc allocator_type
;
202 using _Base::_M_allocate
;
203 using _Base::_M_deallocate
;
204 using _Base::_M_impl
;
205 using _Base::_M_get_Tp_allocator
;
208 // [23.2.4.1] construct/copy/destroy
209 // (assign() and get_allocator() are also listed in this section)
211 * @brief Default constructor creates no elements.
217 * @brief Creates a %vector with no elements.
218 * @param a An allocator object.
221 vector(const allocator_type
& __a
)
225 * @brief Creates a %vector with copies of an exemplar element.
226 * @param n The number of elements to initially create.
227 * @param value An element to copy.
228 * @param a An allocator.
230 * This constructor fills the %vector with @a n copies of @a value.
233 vector(size_type __n
, const value_type
& __value
= value_type(),
234 const allocator_type
& __a
= allocator_type())
236 { _M_fill_initialize(__n
, __value
); }
239 * @brief %Vector copy constructor.
240 * @param x A %vector of identical element and allocator types.
242 * The newly-created %vector uses a copy of the allocation
243 * object used by @a x. All the elements of @a x are copied,
244 * but any extra memory in
245 * @a x (for fast expansion) will not be copied.
247 vector(const vector
& __x
)
248 : _Base(__x
.size(), __x
._M_get_Tp_allocator())
249 { this->_M_impl
._M_finish
=
250 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
251 this->_M_impl
._M_start
,
252 _M_get_Tp_allocator());
255 #ifdef __GXX_EXPERIMENTAL_CXX0X__
257 * @brief %Vector move constructor.
258 * @param x A %vector of identical element and allocator types.
260 * The newly-created %vector contains the exact contents of @a x.
261 * The contents of @a x are a valid, but unspecified %vector.
264 : _Base(std::forward
<_Base
>(__x
)) { }
267 * @brief Builds a %vector from an initializer list.
268 * @param l An initializer_list.
269 * @param a An allocator.
271 * Create a %vector consisting of copies of the elements in the
272 * initializer_list @a l.
274 * This will call the element type's copy constructor N times
275 * (where N is @a l.size()) and do no memory reallocation.
277 vector(initializer_list
<value_type
> __l
,
278 const allocator_type
& __a
= allocator_type())
281 _M_range_initialize(__l
.begin(), __l
.end(),
282 random_access_iterator_tag());
287 * @brief Builds a %vector from a range.
288 * @param first An input iterator.
289 * @param last An input iterator.
290 * @param a An allocator.
292 * Create a %vector consisting of copies of the elements from
295 * If the iterators are forward, bidirectional, or
296 * random-access, then this will call the elements' copy
297 * constructor N times (where N is distance(first,last)) and do
298 * no memory reallocation. But if only input iterators are
299 * used, then this will do at most 2N calls to the copy
300 * constructor, and logN memory reallocations.
302 template<typename _InputIterator
>
303 vector(_InputIterator __first
, _InputIterator __last
,
304 const allocator_type
& __a
= allocator_type())
307 // Check whether it's an integral type. If so, it's not an iterator.
308 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
309 _M_initialize_dispatch(__first
, __last
, _Integral());
313 * The dtor only erases the elements, and note that if the
314 * elements themselves are pointers, the pointed-to memory is
315 * not touched in any way. Managing the pointer is the user's
319 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
320 _M_get_Tp_allocator()); }
323 * @brief %Vector assignment operator.
324 * @param x A %vector of identical element and allocator types.
326 * All the elements of @a x are copied, but any extra memory in
327 * @a x (for fast expansion) will not be copied. Unlike the
328 * copy constructor, the allocator object is not copied.
331 operator=(const vector
& __x
);
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
335 * @brief %Vector move assignment operator.
336 * @param x A %vector of identical element and allocator types.
338 * The contents of @a x are moved into this %vector (without copying).
339 * @a x is a valid, but unspecified %vector.
342 operator=(vector
&& __x
)
351 * @brief %Vector list assignment operator.
352 * @param l An initializer_list.
354 * This function fills a %vector with copies of the elements in the
355 * initializer list @a l.
357 * Note that the assignment completely changes the %vector and
358 * that the resulting %vector's size is the same as the number
359 * of elements assigned. Old data may be lost.
362 operator=(initializer_list
<value_type
> __l
)
364 this->assign(__l
.begin(), __l
.end());
370 * @brief Assigns a given value to a %vector.
371 * @param n Number of elements to be assigned.
372 * @param val Value to be assigned.
374 * This function fills a %vector with @a n copies of the given
375 * value. Note that the assignment completely changes the
376 * %vector and that the resulting %vector's size is the same as
377 * the number of elements assigned. Old data may be lost.
380 assign(size_type __n
, const value_type
& __val
)
381 { _M_fill_assign(__n
, __val
); }
384 * @brief Assigns a range to a %vector.
385 * @param first An input iterator.
386 * @param last An input iterator.
388 * This function fills a %vector with copies of the elements in the
389 * range [first,last).
391 * Note that the assignment completely changes the %vector and
392 * that the resulting %vector's size is the same as the number
393 * of elements assigned. Old data may be lost.
395 template<typename _InputIterator
>
397 assign(_InputIterator __first
, _InputIterator __last
)
399 // Check whether it's an integral type. If so, it's not an iterator.
400 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
401 _M_assign_dispatch(__first
, __last
, _Integral());
404 #ifdef __GXX_EXPERIMENTAL_CXX0X__
406 * @brief Assigns an initializer list to a %vector.
407 * @param l An initializer_list.
409 * This function fills a %vector with copies of the elements in the
410 * initializer list @a l.
412 * Note that the assignment completely changes the %vector and
413 * that the resulting %vector's size is the same as the number
414 * of elements assigned. Old data may be lost.
417 assign(initializer_list
<value_type
> __l
)
418 { this->assign(__l
.begin(), __l
.end()); }
421 /// Get a copy of the memory allocation object.
422 using _Base::get_allocator
;
426 * Returns a read/write iterator that points to the first
427 * element in the %vector. Iteration is done in ordinary
432 { return iterator(this->_M_impl
._M_start
); }
435 * Returns a read-only (constant) iterator that points to the
436 * first element in the %vector. Iteration is done in ordinary
441 { return const_iterator(this->_M_impl
._M_start
); }
444 * Returns a read/write iterator that points one past the last
445 * element in the %vector. Iteration is done in ordinary
450 { return iterator(this->_M_impl
._M_finish
); }
453 * Returns a read-only (constant) iterator that points one past
454 * the last element in the %vector. Iteration is done in
455 * ordinary element order.
459 { return const_iterator(this->_M_impl
._M_finish
); }
462 * Returns a read/write reverse iterator that points to the
463 * last element in the %vector. Iteration is done in reverse
468 { return reverse_iterator(end()); }
471 * Returns a read-only (constant) reverse iterator that points
472 * to the last element in the %vector. Iteration is done in
473 * reverse element order.
475 const_reverse_iterator
477 { return const_reverse_iterator(end()); }
480 * Returns a read/write reverse iterator that points to one
481 * before the first element in the %vector. Iteration is done
482 * in reverse element order.
486 { return reverse_iterator(begin()); }
489 * Returns a read-only (constant) reverse iterator that points
490 * to one before the first element in the %vector. Iteration
491 * is done in reverse element order.
493 const_reverse_iterator
495 { return const_reverse_iterator(begin()); }
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
499 * Returns a read-only (constant) iterator that points to the
500 * first element in the %vector. Iteration is done in ordinary
505 { return const_iterator(this->_M_impl
._M_start
); }
508 * Returns a read-only (constant) iterator that points one past
509 * the last element in the %vector. Iteration is done in
510 * ordinary element order.
514 { return const_iterator(this->_M_impl
._M_finish
); }
517 * Returns a read-only (constant) reverse iterator that points
518 * to the last element in the %vector. Iteration is done in
519 * reverse element order.
521 const_reverse_iterator
523 { return const_reverse_iterator(end()); }
526 * Returns a read-only (constant) reverse iterator that points
527 * to one before the first element in the %vector. Iteration
528 * is done in reverse element order.
530 const_reverse_iterator
532 { return const_reverse_iterator(begin()); }
535 // [23.2.4.2] capacity
536 /** Returns the number of elements in the %vector. */
539 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
541 /** Returns the size() of the largest possible %vector. */
544 { return _M_get_Tp_allocator().max_size(); }
547 * @brief Resizes the %vector to the specified number of elements.
548 * @param new_size Number of elements the %vector should contain.
549 * @param x Data with which new elements should be populated.
551 * This function will %resize the %vector to the specified
552 * number of elements. If the number is smaller than the
553 * %vector's current size the %vector is truncated, otherwise
554 * the %vector is extended and new elements are populated with
558 resize(size_type __new_size
, value_type __x
= value_type())
560 if (__new_size
< size())
561 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
563 insert(end(), __new_size
- size(), __x
);
567 * Returns the total number of elements that the %vector can
568 * hold before needing to allocate more memory.
572 { return size_type(this->_M_impl
._M_end_of_storage
573 - this->_M_impl
._M_start
); }
576 * Returns true if the %vector is empty. (Thus begin() would
581 { return begin() == end(); }
584 * @brief Attempt to preallocate enough memory for specified number of
586 * @param n Number of elements required.
587 * @throw std::length_error If @a n exceeds @c max_size().
589 * This function attempts to reserve enough memory for the
590 * %vector to hold the specified number of elements. If the
591 * number requested is more than max_size(), length_error is
594 * The advantage of this function is that if optimal code is a
595 * necessity and the user can determine the number of elements
596 * that will be required, the user can reserve the memory in
597 * %advance, and thus prevent a possible reallocation of memory
598 * and copying of %vector data.
601 reserve(size_type __n
);
605 * @brief Subscript access to the data contained in the %vector.
606 * @param n The index of the element for which data should be
608 * @return Read/write reference to data.
610 * This operator allows for easy, array-style, data access.
611 * Note that data access with this operator is unchecked and
612 * out_of_range lookups are not defined. (For checked lookups
616 operator[](size_type __n
)
617 { return *(this->_M_impl
._M_start
+ __n
); }
620 * @brief Subscript access to the data contained in the %vector.
621 * @param n The index of the element for which data should be
623 * @return Read-only (constant) reference to data.
625 * This operator allows for easy, array-style, data access.
626 * Note that data access with this operator is unchecked and
627 * out_of_range lookups are not defined. (For checked lookups
631 operator[](size_type __n
) const
632 { return *(this->_M_impl
._M_start
+ __n
); }
635 /// Safety check used only from at().
637 _M_range_check(size_type __n
) const
639 if (__n
>= this->size())
640 __throw_out_of_range(__N("vector::_M_range_check"));
645 * @brief Provides access to the data contained in the %vector.
646 * @param n The index of the element for which data should be
648 * @return Read/write reference to data.
649 * @throw std::out_of_range If @a n is an invalid index.
651 * This function provides for safer data access. The parameter
652 * is first checked that it is in the range of the vector. The
653 * function throws out_of_range if the check fails.
663 * @brief Provides access to the data contained in the %vector.
664 * @param n The index of the element for which data should be
666 * @return Read-only (constant) reference to data.
667 * @throw std::out_of_range If @a n is an invalid index.
669 * This function provides for safer data access. The parameter
670 * is first checked that it is in the range of the vector. The
671 * function throws out_of_range if the check fails.
674 at(size_type __n
) const
681 * Returns a read/write reference to the data at the first
682 * element of the %vector.
689 * Returns a read-only (constant) reference to the data at the first
690 * element of the %vector.
697 * Returns a read/write reference to the data at the last
698 * element of the %vector.
702 { return *(end() - 1); }
705 * Returns a read-only (constant) reference to the data at the
706 * last element of the %vector.
710 { return *(end() - 1); }
712 // _GLIBCXX_RESOLVE_LIB_DEFECTS
713 // DR 464. Suggestion for new member functions in standard containers.
716 * Returns a pointer such that [data(), data() + size()) is a valid
717 * range. For a non-empty %vector, data() == &front().
721 { return pointer(this->_M_impl
._M_start
); }
725 { return const_pointer(this->_M_impl
._M_start
); }
727 // [23.2.4.3] modifiers
729 * @brief Add data to the end of the %vector.
730 * @param x Data to be added.
732 * This is a typical stack operation. The function creates an
733 * element at the end of the %vector and assigns the given data
734 * to it. Due to the nature of a %vector this operation can be
735 * done in constant time if the %vector has preallocated space
739 push_back(const value_type
& __x
)
741 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
743 this->_M_impl
.construct(this->_M_impl
._M_finish
, __x
);
744 ++this->_M_impl
._M_finish
;
747 _M_insert_aux(end(), __x
);
750 #ifdef __GXX_EXPERIMENTAL_CXX0X__
752 push_back(value_type
&& __x
)
753 { emplace_back(std::move(__x
)); }
755 template<typename
... _Args
>
757 emplace_back(_Args
&&... __args
);
761 * @brief Removes last element.
763 * This is a typical stack operation. It shrinks the %vector by one.
765 * Note that no data is returned, and if the last element's
766 * data is needed, it should be retrieved before pop_back() is
772 --this->_M_impl
._M_finish
;
773 this->_M_impl
.destroy(this->_M_impl
._M_finish
);
776 #ifdef __GXX_EXPERIMENTAL_CXX0X__
778 * @brief Inserts an object in %vector before specified iterator.
779 * @param position An iterator into the %vector.
780 * @param args Arguments.
781 * @return An iterator that points to the inserted data.
783 * This function will insert an object of type T constructed
784 * with T(std::forward<Args>(args)...) before the specified location.
785 * Note that this kind of operation could be expensive for a %vector
786 * and if it is frequently used the user should consider using
789 template<typename
... _Args
>
791 emplace(iterator __position
, _Args
&&... __args
);
795 * @brief Inserts given value into %vector before specified iterator.
796 * @param position An iterator into the %vector.
797 * @param x Data to be inserted.
798 * @return An iterator that points to the inserted data.
800 * This function will insert a copy of the given value before
801 * the specified location. Note that this kind of operation
802 * could be expensive for a %vector and if it is frequently
803 * used the user should consider using std::list.
806 insert(iterator __position
, const value_type
& __x
);
808 #ifdef __GXX_EXPERIMENTAL_CXX0X__
810 * @brief Inserts given rvalue into %vector before specified iterator.
811 * @param position An iterator into the %vector.
812 * @param x Data to be inserted.
813 * @return An iterator that points to the inserted data.
815 * This function will insert a copy of the given rvalue before
816 * the specified location. Note that this kind of operation
817 * could be expensive for a %vector and if it is frequently
818 * used the user should consider using std::list.
821 insert(iterator __position
, value_type
&& __x
)
822 { return emplace(__position
, std::move(__x
)); }
825 * @brief Inserts an initializer_list into the %vector.
826 * @param position An iterator into the %vector.
827 * @param l An initializer_list.
829 * This function will insert copies of the data in the
830 * initializer_list @a l into the %vector before the location
831 * specified by @a position.
833 * Note that this kind of operation could be expensive for a
834 * %vector and if it is frequently used the user should
835 * consider using std::list.
838 insert(iterator __position
, initializer_list
<value_type
> __l
)
839 { this->insert(__position
, __l
.begin(), __l
.end()); }
843 * @brief Inserts a number of copies of given data into the %vector.
844 * @param position An iterator into the %vector.
845 * @param n Number of elements to be inserted.
846 * @param x Data to be inserted.
848 * This function will insert a specified number of copies of
849 * the given data before the location specified by @a position.
851 * Note that this kind of operation could be expensive for a
852 * %vector and if it is frequently used the user should
853 * consider using std::list.
856 insert(iterator __position
, size_type __n
, const value_type
& __x
)
857 { _M_fill_insert(__position
, __n
, __x
); }
860 * @brief Inserts a range into the %vector.
861 * @param position An iterator into the %vector.
862 * @param first An input iterator.
863 * @param last An input iterator.
865 * This function will insert copies of the data in the range
866 * [first,last) into the %vector before the location specified
869 * Note that this kind of operation could be expensive for a
870 * %vector and if it is frequently used the user should
871 * consider using std::list.
873 template<typename _InputIterator
>
875 insert(iterator __position
, _InputIterator __first
,
876 _InputIterator __last
)
878 // Check whether it's an integral type. If so, it's not an iterator.
879 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
880 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
884 * @brief Remove element at given position.
885 * @param position Iterator pointing to element to be erased.
886 * @return An iterator pointing to the next element (or end()).
888 * This function will erase the element at the given position and thus
889 * shorten the %vector by one.
891 * Note This operation could be expensive and if it is
892 * frequently used the user should consider using std::list.
893 * The user is also cautioned that this function only erases
894 * the element, and that if the element is itself a pointer,
895 * the pointed-to memory is not touched in any way. Managing
896 * the pointer is the user's responsibility.
899 erase(iterator __position
);
902 * @brief Remove a range of elements.
903 * @param first Iterator pointing to the first element to be erased.
904 * @param last Iterator pointing to one past the last element to be
906 * @return An iterator pointing to the element pointed to by @a last
907 * prior to erasing (or end()).
909 * This function will erase the elements in the range [first,last) and
910 * shorten the %vector accordingly.
912 * Note This operation could be expensive and if it is
913 * frequently used the user should consider using std::list.
914 * The user is also cautioned that this function only erases
915 * the elements, and that if the elements themselves are
916 * pointers, the pointed-to memory is not touched in any way.
917 * Managing the pointer is the user's responsibility.
920 erase(iterator __first
, iterator __last
);
923 * @brief Swaps data with another %vector.
924 * @param x A %vector of the same element and allocator types.
926 * This exchanges the elements between two vectors in constant time.
927 * (Three pointers, so it should be quite fast.)
928 * Note that the global std::swap() function is specialized such that
929 * std::swap(v1,v2) will feed to this function.
932 #ifdef __GXX_EXPERIMENTAL_CXX0X__
938 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
939 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
940 std::swap(this->_M_impl
._M_end_of_storage
,
941 __x
._M_impl
._M_end_of_storage
);
943 // _GLIBCXX_RESOLVE_LIB_DEFECTS
944 // 431. Swapping containers with unequal allocators.
945 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
946 __x
._M_get_Tp_allocator());
950 * Erases all the elements. Note that this function only erases the
951 * elements, and that if the elements themselves are pointers, the
952 * pointed-to memory is not touched in any way. Managing the pointer is
953 * the user's responsibility.
957 { _M_erase_at_end(this->_M_impl
._M_start
); }
961 * Memory expansion handler. Uses the member allocation function to
962 * obtain @a n bytes of memory, and then copies [first,last) into it.
964 template<typename _ForwardIterator
>
966 _M_allocate_and_copy(size_type __n
,
967 _ForwardIterator __first
, _ForwardIterator __last
)
969 pointer __result
= this->_M_allocate(__n
);
972 std::__uninitialized_copy_a(__first
, __last
, __result
,
973 _M_get_Tp_allocator());
978 _M_deallocate(__result
, __n
);
979 __throw_exception_again
;
984 // Internal constructor functions follow.
986 // Called by the range constructor to implement [23.1.1]/9
988 // _GLIBCXX_RESOLVE_LIB_DEFECTS
989 // 438. Ambiguity in the "do the right thing" clause
990 template<typename _Integer
>
992 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
994 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
995 this->_M_impl
._M_end_of_storage
=
996 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
997 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1000 // Called by the range constructor to implement [23.1.1]/9
1001 template<typename _InputIterator
>
1003 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1006 typedef typename
std::iterator_traits
<_InputIterator
>::
1007 iterator_category _IterCategory
;
1008 _M_range_initialize(__first
, __last
, _IterCategory());
1011 // Called by the second initialize_dispatch above
1012 template<typename _InputIterator
>
1014 _M_range_initialize(_InputIterator __first
,
1015 _InputIterator __last
, std::input_iterator_tag
)
1017 for (; __first
!= __last
; ++__first
)
1018 push_back(*__first
);
1021 // Called by the second initialize_dispatch above
1022 template<typename _ForwardIterator
>
1024 _M_range_initialize(_ForwardIterator __first
,
1025 _ForwardIterator __last
, std::forward_iterator_tag
)
1027 const size_type __n
= std::distance(__first
, __last
);
1028 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1029 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1030 this->_M_impl
._M_finish
=
1031 std::__uninitialized_copy_a(__first
, __last
,
1032 this->_M_impl
._M_start
,
1033 _M_get_Tp_allocator());
1036 // Called by the first initialize_dispatch above and by the
1037 // vector(n,value,a) constructor.
1039 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1041 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1042 _M_get_Tp_allocator());
1043 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1047 // Internal assign functions follow. The *_aux functions do the actual
1048 // assignment work for the range versions.
1050 // Called by the range assign to implement [23.1.1]/9
1052 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1053 // 438. Ambiguity in the "do the right thing" clause
1054 template<typename _Integer
>
1056 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1057 { _M_fill_assign(__n
, __val
); }
1059 // Called by the range assign to implement [23.1.1]/9
1060 template<typename _InputIterator
>
1062 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1065 typedef typename
std::iterator_traits
<_InputIterator
>::
1066 iterator_category _IterCategory
;
1067 _M_assign_aux(__first
, __last
, _IterCategory());
1070 // Called by the second assign_dispatch above
1071 template<typename _InputIterator
>
1073 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1074 std::input_iterator_tag
);
1076 // Called by the second assign_dispatch above
1077 template<typename _ForwardIterator
>
1079 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1080 std::forward_iterator_tag
);
1082 // Called by assign(n,t), and the range assign when it turns out
1083 // to be the same thing.
1085 _M_fill_assign(size_type __n
, const value_type
& __val
);
1088 // Internal insert functions follow.
1090 // Called by the range insert to implement [23.1.1]/9
1092 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1093 // 438. Ambiguity in the "do the right thing" clause
1094 template<typename _Integer
>
1096 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1098 { _M_fill_insert(__pos
, __n
, __val
); }
1100 // Called by the range insert to implement [23.1.1]/9
1101 template<typename _InputIterator
>
1103 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1104 _InputIterator __last
, __false_type
)
1106 typedef typename
std::iterator_traits
<_InputIterator
>::
1107 iterator_category _IterCategory
;
1108 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1111 // Called by the second insert_dispatch above
1112 template<typename _InputIterator
>
1114 _M_range_insert(iterator __pos
, _InputIterator __first
,
1115 _InputIterator __last
, std::input_iterator_tag
);
1117 // Called by the second insert_dispatch above
1118 template<typename _ForwardIterator
>
1120 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1121 _ForwardIterator __last
, std::forward_iterator_tag
);
1123 // Called by insert(p,n,x), and the range insert when it turns out to be
1126 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1128 // Called by insert(p,x)
1129 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1131 _M_insert_aux(iterator __position
, const value_type
& __x
);
1133 template<typename
... _Args
>
1135 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1138 // Called by the latter.
1140 _M_check_len(size_type __n
, const char* __s
) const
1142 if (max_size() - size() < __n
)
1143 __throw_length_error(__N(__s
));
1145 const size_type __len
= size() + std::max(size(), __n
);
1146 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1149 // Internal erase functions follow.
1151 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1154 _M_erase_at_end(pointer __pos
)
1156 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1157 this->_M_impl
._M_finish
= __pos
;
1163 * @brief Vector equality comparison.
1164 * @param x A %vector.
1165 * @param y A %vector of the same type as @a x.
1166 * @return True iff the size and elements of the vectors are equal.
1168 * This is an equivalence relation. It is linear in the size of the
1169 * vectors. Vectors are considered equivalent if their sizes are equal,
1170 * and if corresponding elements compare equal.
1172 template<typename _Tp
, typename _Alloc
>
1174 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1175 { return (__x
.size() == __y
.size()
1176 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1179 * @brief Vector ordering relation.
1180 * @param x A %vector.
1181 * @param y A %vector of the same type as @a x.
1182 * @return True iff @a x is lexicographically less than @a y.
1184 * This is a total ordering relation. It is linear in the size of the
1185 * vectors. The elements must be comparable with @c <.
1187 * See std::lexicographical_compare() for how the determination is made.
1189 template<typename _Tp
, typename _Alloc
>
1191 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1192 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1193 __y
.begin(), __y
.end()); }
1195 /// Based on operator==
1196 template<typename _Tp
, typename _Alloc
>
1198 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1199 { return !(__x
== __y
); }
1201 /// Based on operator<
1202 template<typename _Tp
, typename _Alloc
>
1204 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1205 { return __y
< __x
; }
1207 /// Based on operator<
1208 template<typename _Tp
, typename _Alloc
>
1210 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1211 { return !(__y
< __x
); }
1213 /// Based on operator<
1214 template<typename _Tp
, typename _Alloc
>
1216 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1217 { return !(__x
< __y
); }
1219 /// See std::vector::swap().
1220 template<typename _Tp
, typename _Alloc
>
1222 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1226 template<typename _Tp
, typename _Alloc
>
1228 swap(vector
<_Tp
, _Alloc
>&& __x
, vector
<_Tp
, _Alloc
>& __y
)
1231 template<typename _Tp
, typename _Alloc
>
1233 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>&& __y
)
1237 _GLIBCXX_END_NESTED_NAMESPACE
1239 #endif /* _STL_VECTOR_H */