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.
162 * Meets the requirements of a <a href="tables.html#65">container</a>, a
163 * <a href="tables.html#66">reversible container</a>, and a
164 * <a href="tables.html#67">sequence</a>, including the
165 * <a href="tables.html#68">optional sequence requirements</a> with the
166 * %exception of @c push_front and @c pop_front.
168 * In some terminology a %vector can be described as a dynamic
169 * C-style array, it offers fast and efficient access to individual
170 * elements in any order and saves the user from worrying about
171 * memory and size allocation. Subscripting ( @c [] ) access is
172 * also provided as with C-style arrays.
174 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
175 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
177 // Concept requirements.
178 typedef typename
_Alloc::value_type _Alloc_value_type
;
179 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
180 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
182 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
183 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
186 typedef _Tp value_type
;
187 typedef typename
_Tp_alloc_type::pointer pointer
;
188 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
189 typedef typename
_Tp_alloc_type::reference reference
;
190 typedef typename
_Tp_alloc_type::const_reference const_reference
;
191 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
192 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
194 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
195 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
196 typedef size_t size_type
;
197 typedef ptrdiff_t difference_type
;
198 typedef _Alloc allocator_type
;
201 using _Base::_M_allocate
;
202 using _Base::_M_deallocate
;
203 using _Base::_M_impl
;
204 using _Base::_M_get_Tp_allocator
;
207 // [23.2.4.1] construct/copy/destroy
208 // (assign() and get_allocator() are also listed in this section)
210 * @brief Default constructor creates no elements.
216 * @brief Creates a %vector with no elements.
217 * @param a An allocator object.
220 vector(const allocator_type
& __a
)
224 * @brief Creates a %vector with copies of an exemplar element.
225 * @param n The number of elements to initially create.
226 * @param value An element to copy.
227 * @param a An allocator.
229 * This constructor fills the %vector with @a n copies of @a value.
232 vector(size_type __n
, const value_type
& __value
= value_type(),
233 const allocator_type
& __a
= allocator_type())
235 { _M_fill_initialize(__n
, __value
); }
238 * @brief %Vector copy constructor.
239 * @param x A %vector of identical element and allocator types.
241 * The newly-created %vector uses a copy of the allocation
242 * object used by @a x. All the elements of @a x are copied,
243 * but any extra memory in
244 * @a x (for fast expansion) will not be copied.
246 vector(const vector
& __x
)
247 : _Base(__x
.size(), __x
._M_get_Tp_allocator())
248 { this->_M_impl
._M_finish
=
249 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
250 this->_M_impl
._M_start
,
251 _M_get_Tp_allocator());
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
256 * @brief %Vector move constructor.
257 * @param x A %vector of identical element and allocator types.
259 * The newly-created %vector contains the exact contents of @a x.
260 * The contents of @a x are a valid, but unspecified %vector.
263 : _Base(std::forward
<_Base
>(__x
)) { }
266 * @brief Builds a %vector from an initializer list.
267 * @param l An initializer_list.
268 * @param a An allocator.
270 * Create a %vector consisting of copies of the elements in the
271 * initializer_list @a l.
273 * This will call the element type's copy constructor N times
274 * (where N is @a l.size()) and do no memory reallocation.
276 vector(initializer_list
<value_type
> __l
,
277 const allocator_type
& __a
= allocator_type())
280 _M_range_initialize(__l
.begin(), __l
.end(),
281 random_access_iterator_tag());
286 * @brief Builds a %vector from a range.
287 * @param first An input iterator.
288 * @param last An input iterator.
289 * @param a An allocator.
291 * Create a %vector consisting of copies of the elements from
294 * If the iterators are forward, bidirectional, or
295 * random-access, then this will call the elements' copy
296 * constructor N times (where N is distance(first,last)) and do
297 * no memory reallocation. But if only input iterators are
298 * used, then this will do at most 2N calls to the copy
299 * constructor, and logN memory reallocations.
301 template<typename _InputIterator
>
302 vector(_InputIterator __first
, _InputIterator __last
,
303 const allocator_type
& __a
= allocator_type())
306 // Check whether it's an integral type. If so, it's not an iterator.
307 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
308 _M_initialize_dispatch(__first
, __last
, _Integral());
312 * The dtor only erases the elements, and note that if the
313 * elements themselves are pointers, the pointed-to memory is
314 * not touched in any way. Managing the pointer is the user's
318 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
319 _M_get_Tp_allocator()); }
322 * @brief %Vector assignment operator.
323 * @param x A %vector of identical element and allocator types.
325 * All the elements of @a x are copied, but any extra memory in
326 * @a x (for fast expansion) will not be copied. Unlike the
327 * copy constructor, the allocator object is not copied.
330 operator=(const vector
& __x
);
332 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334 * @brief %Vector move assignment operator.
335 * @param x A %vector of identical element and allocator types.
337 * The contents of @a x are moved into this %vector (without copying).
338 * @a x is a valid, but unspecified %vector.
341 operator=(vector
&& __x
)
350 * @brief %Vector list assignment operator.
351 * @param l An initializer_list.
353 * This function fills a %vector with copies of the elements in the
354 * initializer list @a l.
356 * Note that the assignment completely changes the %vector and
357 * that the resulting %vector's size is the same as the number
358 * of elements assigned. Old data may be lost.
361 operator=(initializer_list
<value_type
> __l
)
363 this->assign(__l
.begin(), __l
.end());
369 * @brief Assigns a given value to a %vector.
370 * @param n Number of elements to be assigned.
371 * @param val Value to be assigned.
373 * This function fills a %vector with @a n copies of the given
374 * value. Note that the assignment completely changes the
375 * %vector and that the resulting %vector's size is the same as
376 * the number of elements assigned. Old data may be lost.
379 assign(size_type __n
, const value_type
& __val
)
380 { _M_fill_assign(__n
, __val
); }
383 * @brief Assigns a range to a %vector.
384 * @param first An input iterator.
385 * @param last An input iterator.
387 * This function fills a %vector with copies of the elements in the
388 * range [first,last).
390 * Note that the assignment completely changes the %vector and
391 * that the resulting %vector's size is the same as the number
392 * of elements assigned. Old data may be lost.
394 template<typename _InputIterator
>
396 assign(_InputIterator __first
, _InputIterator __last
)
398 // Check whether it's an integral type. If so, it's not an iterator.
399 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
400 _M_assign_dispatch(__first
, __last
, _Integral());
403 #ifdef __GXX_EXPERIMENTAL_CXX0X__
405 * @brief Assigns an initializer list to a %vector.
406 * @param l An initializer_list.
408 * This function fills a %vector with copies of the elements in the
409 * initializer list @a l.
411 * Note that the assignment completely changes the %vector and
412 * that the resulting %vector's size is the same as the number
413 * of elements assigned. Old data may be lost.
416 assign(initializer_list
<value_type
> __l
)
417 { this->assign(__l
.begin(), __l
.end()); }
420 /// Get a copy of the memory allocation object.
421 using _Base::get_allocator
;
425 * Returns a read/write iterator that points to the first
426 * element in the %vector. Iteration is done in ordinary
431 { return iterator(this->_M_impl
._M_start
); }
434 * Returns a read-only (constant) iterator that points to the
435 * first element in the %vector. Iteration is done in ordinary
440 { return const_iterator(this->_M_impl
._M_start
); }
443 * Returns a read/write iterator that points one past the last
444 * element in the %vector. Iteration is done in ordinary
449 { return iterator(this->_M_impl
._M_finish
); }
452 * Returns a read-only (constant) iterator that points one past
453 * the last element in the %vector. Iteration is done in
454 * ordinary element order.
458 { return const_iterator(this->_M_impl
._M_finish
); }
461 * Returns a read/write reverse iterator that points to the
462 * last element in the %vector. Iteration is done in reverse
467 { return reverse_iterator(end()); }
470 * Returns a read-only (constant) reverse iterator that points
471 * to the last element in the %vector. Iteration is done in
472 * reverse element order.
474 const_reverse_iterator
476 { return const_reverse_iterator(end()); }
479 * Returns a read/write reverse iterator that points to one
480 * before the first element in the %vector. Iteration is done
481 * in reverse element order.
485 { return reverse_iterator(begin()); }
488 * Returns a read-only (constant) reverse iterator that points
489 * to one before the first element in the %vector. Iteration
490 * is done in reverse element order.
492 const_reverse_iterator
494 { return const_reverse_iterator(begin()); }
496 #ifdef __GXX_EXPERIMENTAL_CXX0X__
498 * Returns a read-only (constant) iterator that points to the
499 * first element in the %vector. Iteration is done in ordinary
504 { return const_iterator(this->_M_impl
._M_start
); }
507 * Returns a read-only (constant) iterator that points one past
508 * the last element in the %vector. Iteration is done in
509 * ordinary element order.
513 { return const_iterator(this->_M_impl
._M_finish
); }
516 * Returns a read-only (constant) reverse iterator that points
517 * to the last element in the %vector. Iteration is done in
518 * reverse element order.
520 const_reverse_iterator
522 { return const_reverse_iterator(end()); }
525 * Returns a read-only (constant) reverse iterator that points
526 * to one before the first element in the %vector. Iteration
527 * is done in reverse element order.
529 const_reverse_iterator
531 { return const_reverse_iterator(begin()); }
534 // [23.2.4.2] capacity
535 /** Returns the number of elements in the %vector. */
538 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
540 /** Returns the size() of the largest possible %vector. */
543 { return _M_get_Tp_allocator().max_size(); }
546 * @brief Resizes the %vector to the specified number of elements.
547 * @param new_size Number of elements the %vector should contain.
548 * @param x Data with which new elements should be populated.
550 * This function will %resize the %vector to the specified
551 * number of elements. If the number is smaller than the
552 * %vector's current size the %vector is truncated, otherwise
553 * the %vector is extended and new elements are populated with
557 resize(size_type __new_size
, value_type __x
= value_type())
559 if (__new_size
< size())
560 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
562 insert(end(), __new_size
- size(), __x
);
566 * Returns the total number of elements that the %vector can
567 * hold before needing to allocate more memory.
571 { return size_type(this->_M_impl
._M_end_of_storage
572 - this->_M_impl
._M_start
); }
575 * Returns true if the %vector is empty. (Thus begin() would
580 { return begin() == end(); }
583 * @brief Attempt to preallocate enough memory for specified number of
585 * @param n Number of elements required.
586 * @throw std::length_error If @a n exceeds @c max_size().
588 * This function attempts to reserve enough memory for the
589 * %vector to hold the specified number of elements. If the
590 * number requested is more than max_size(), length_error is
593 * The advantage of this function is that if optimal code is a
594 * necessity and the user can determine the number of elements
595 * that will be required, the user can reserve the memory in
596 * %advance, and thus prevent a possible reallocation of memory
597 * and copying of %vector data.
600 reserve(size_type __n
);
604 * @brief Subscript access to the data contained in the %vector.
605 * @param n The index of the element for which data should be
607 * @return Read/write reference to data.
609 * This operator allows for easy, array-style, data access.
610 * Note that data access with this operator is unchecked and
611 * out_of_range lookups are not defined. (For checked lookups
615 operator[](size_type __n
)
616 { return *(this->_M_impl
._M_start
+ __n
); }
619 * @brief Subscript access to the data contained in the %vector.
620 * @param n The index of the element for which data should be
622 * @return Read-only (constant) reference to data.
624 * This operator allows for easy, array-style, data access.
625 * Note that data access with this operator is unchecked and
626 * out_of_range lookups are not defined. (For checked lookups
630 operator[](size_type __n
) const
631 { return *(this->_M_impl
._M_start
+ __n
); }
634 /// Safety check used only from at().
636 _M_range_check(size_type __n
) const
638 if (__n
>= this->size())
639 __throw_out_of_range(__N("vector::_M_range_check"));
644 * @brief Provides access to the data contained in the %vector.
645 * @param n The index of the element for which data should be
647 * @return Read/write reference to data.
648 * @throw std::out_of_range If @a n is an invalid index.
650 * This function provides for safer data access. The parameter
651 * is first checked that it is in the range of the vector. The
652 * function throws out_of_range if the check fails.
662 * @brief Provides access to the data contained in the %vector.
663 * @param n The index of the element for which data should be
665 * @return Read-only (constant) reference to data.
666 * @throw std::out_of_range If @a n is an invalid index.
668 * This function provides for safer data access. The parameter
669 * is first checked that it is in the range of the vector. The
670 * function throws out_of_range if the check fails.
673 at(size_type __n
) const
680 * Returns a read/write reference to the data at the first
681 * element of the %vector.
688 * Returns a read-only (constant) reference to the data at the first
689 * element of the %vector.
696 * Returns a read/write reference to the data at the last
697 * element of the %vector.
701 { return *(end() - 1); }
704 * Returns a read-only (constant) reference to the data at the
705 * last element of the %vector.
709 { return *(end() - 1); }
711 // _GLIBCXX_RESOLVE_LIB_DEFECTS
712 // DR 464. Suggestion for new member functions in standard containers.
715 * Returns a pointer such that [data(), data() + size()) is a valid
716 * range. For a non-empty %vector, data() == &front().
720 { return pointer(this->_M_impl
._M_start
); }
724 { return const_pointer(this->_M_impl
._M_start
); }
726 // [23.2.4.3] modifiers
728 * @brief Add data to the end of the %vector.
729 * @param x Data to be added.
731 * This is a typical stack operation. The function creates an
732 * element at the end of the %vector and assigns the given data
733 * to it. Due to the nature of a %vector this operation can be
734 * done in constant time if the %vector has preallocated space
738 push_back(const value_type
& __x
)
740 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
742 this->_M_impl
.construct(this->_M_impl
._M_finish
, __x
);
743 ++this->_M_impl
._M_finish
;
746 _M_insert_aux(end(), __x
);
749 #ifdef __GXX_EXPERIMENTAL_CXX0X__
751 push_back(value_type
&& __x
)
752 { emplace_back(std::move(__x
)); }
754 template<typename
... _Args
>
756 emplace_back(_Args
&&... __args
);
760 * @brief Removes last element.
762 * This is a typical stack operation. It shrinks the %vector by one.
764 * Note that no data is returned, and if the last element's
765 * data is needed, it should be retrieved before pop_back() is
771 --this->_M_impl
._M_finish
;
772 this->_M_impl
.destroy(this->_M_impl
._M_finish
);
775 #ifdef __GXX_EXPERIMENTAL_CXX0X__
777 * @brief Inserts an object in %vector before specified iterator.
778 * @param position An iterator into the %vector.
779 * @param args Arguments.
780 * @return An iterator that points to the inserted data.
782 * This function will insert an object of type T constructed
783 * with T(std::forward<Args>(args)...) before the specified location.
784 * Note that this kind of operation could be expensive for a %vector
785 * and if it is frequently used the user should consider using
788 template<typename
... _Args
>
790 emplace(iterator __position
, _Args
&&... __args
);
794 * @brief Inserts given value into %vector before specified iterator.
795 * @param position An iterator into the %vector.
796 * @param x Data to be inserted.
797 * @return An iterator that points to the inserted data.
799 * This function will insert a copy of the given value before
800 * the specified location. Note that this kind of operation
801 * could be expensive for a %vector and if it is frequently
802 * used the user should consider using std::list.
805 insert(iterator __position
, const value_type
& __x
);
807 #ifdef __GXX_EXPERIMENTAL_CXX0X__
809 * @brief Inserts given rvalue into %vector before specified iterator.
810 * @param position An iterator into the %vector.
811 * @param x Data to be inserted.
812 * @return An iterator that points to the inserted data.
814 * This function will insert a copy of the given rvalue before
815 * the specified location. Note that this kind of operation
816 * could be expensive for a %vector and if it is frequently
817 * used the user should consider using std::list.
820 insert(iterator __position
, value_type
&& __x
)
821 { return emplace(__position
, std::move(__x
)); }
824 * @brief Inserts an initializer_list into the %vector.
825 * @param position An iterator into the %vector.
826 * @param l An initializer_list.
828 * This function will insert copies of the data in the
829 * initializer_list @a l into the %vector before the location
830 * specified by @a position.
832 * Note that this kind of operation could be expensive for a
833 * %vector and if it is frequently used the user should
834 * consider using std::list.
837 insert(iterator __position
, initializer_list
<value_type
> __l
)
838 { this->insert(__position
, __l
.begin(), __l
.end()); }
842 * @brief Inserts a number of copies of given data into the %vector.
843 * @param position An iterator into the %vector.
844 * @param n Number of elements to be inserted.
845 * @param x Data to be inserted.
847 * This function will insert a specified number of copies of
848 * the given data before the location specified by @a position.
850 * Note that this kind of operation could be expensive for a
851 * %vector and if it is frequently used the user should
852 * consider using std::list.
855 insert(iterator __position
, size_type __n
, const value_type
& __x
)
856 { _M_fill_insert(__position
, __n
, __x
); }
859 * @brief Inserts a range into the %vector.
860 * @param position An iterator into the %vector.
861 * @param first An input iterator.
862 * @param last An input iterator.
864 * This function will insert copies of the data in the range
865 * [first,last) into the %vector before the location specified
868 * Note that this kind of operation could be expensive for a
869 * %vector and if it is frequently used the user should
870 * consider using std::list.
872 template<typename _InputIterator
>
874 insert(iterator __position
, _InputIterator __first
,
875 _InputIterator __last
)
877 // Check whether it's an integral type. If so, it's not an iterator.
878 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
879 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
883 * @brief Remove element at given position.
884 * @param position Iterator pointing to element to be erased.
885 * @return An iterator pointing to the next element (or end()).
887 * This function will erase the element at the given position and thus
888 * shorten the %vector by one.
890 * Note This operation could be expensive and if it is
891 * frequently used the user should consider using std::list.
892 * The user is also cautioned that this function only erases
893 * the element, and that if the element is itself a pointer,
894 * the pointed-to memory is not touched in any way. Managing
895 * the pointer is the user's responsibility.
898 erase(iterator __position
);
901 * @brief Remove a range of elements.
902 * @param first Iterator pointing to the first element to be erased.
903 * @param last Iterator pointing to one past the last element to be
905 * @return An iterator pointing to the element pointed to by @a last
906 * prior to erasing (or end()).
908 * This function will erase the elements in the range [first,last) and
909 * shorten the %vector accordingly.
911 * Note This operation could be expensive and if it is
912 * frequently used the user should consider using std::list.
913 * The user is also cautioned that this function only erases
914 * the elements, and that if the elements themselves are
915 * pointers, the pointed-to memory is not touched in any way.
916 * Managing the pointer is the user's responsibility.
919 erase(iterator __first
, iterator __last
);
922 * @brief Swaps data with another %vector.
923 * @param x A %vector of the same element and allocator types.
925 * This exchanges the elements between two vectors in constant time.
926 * (Three pointers, so it should be quite fast.)
927 * Note that the global std::swap() function is specialized such that
928 * std::swap(v1,v2) will feed to this function.
931 #ifdef __GXX_EXPERIMENTAL_CXX0X__
937 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
938 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
939 std::swap(this->_M_impl
._M_end_of_storage
,
940 __x
._M_impl
._M_end_of_storage
);
942 // _GLIBCXX_RESOLVE_LIB_DEFECTS
943 // 431. Swapping containers with unequal allocators.
944 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
945 __x
._M_get_Tp_allocator());
949 * Erases all the elements. Note that this function only erases the
950 * elements, and that if the elements themselves are pointers, the
951 * pointed-to memory is not touched in any way. Managing the pointer is
952 * the user's responsibility.
956 { _M_erase_at_end(this->_M_impl
._M_start
); }
960 * Memory expansion handler. Uses the member allocation function to
961 * obtain @a n bytes of memory, and then copies [first,last) into it.
963 template<typename _ForwardIterator
>
965 _M_allocate_and_copy(size_type __n
,
966 _ForwardIterator __first
, _ForwardIterator __last
)
968 pointer __result
= this->_M_allocate(__n
);
971 std::__uninitialized_copy_a(__first
, __last
, __result
,
972 _M_get_Tp_allocator());
977 _M_deallocate(__result
, __n
);
978 __throw_exception_again
;
983 // Internal constructor functions follow.
985 // Called by the range constructor to implement [23.1.1]/9
987 // _GLIBCXX_RESOLVE_LIB_DEFECTS
988 // 438. Ambiguity in the "do the right thing" clause
989 template<typename _Integer
>
991 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
993 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
994 this->_M_impl
._M_end_of_storage
=
995 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
996 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
999 // Called by the range constructor to implement [23.1.1]/9
1000 template<typename _InputIterator
>
1002 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1005 typedef typename
std::iterator_traits
<_InputIterator
>::
1006 iterator_category _IterCategory
;
1007 _M_range_initialize(__first
, __last
, _IterCategory());
1010 // Called by the second initialize_dispatch above
1011 template<typename _InputIterator
>
1013 _M_range_initialize(_InputIterator __first
,
1014 _InputIterator __last
, std::input_iterator_tag
)
1016 for (; __first
!= __last
; ++__first
)
1017 push_back(*__first
);
1020 // Called by the second initialize_dispatch above
1021 template<typename _ForwardIterator
>
1023 _M_range_initialize(_ForwardIterator __first
,
1024 _ForwardIterator __last
, std::forward_iterator_tag
)
1026 const size_type __n
= std::distance(__first
, __last
);
1027 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1028 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1029 this->_M_impl
._M_finish
=
1030 std::__uninitialized_copy_a(__first
, __last
,
1031 this->_M_impl
._M_start
,
1032 _M_get_Tp_allocator());
1035 // Called by the first initialize_dispatch above and by the
1036 // vector(n,value,a) constructor.
1038 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1040 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1041 _M_get_Tp_allocator());
1042 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1046 // Internal assign functions follow. The *_aux functions do the actual
1047 // assignment work for the range versions.
1049 // Called by the range assign to implement [23.1.1]/9
1051 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1052 // 438. Ambiguity in the "do the right thing" clause
1053 template<typename _Integer
>
1055 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1056 { _M_fill_assign(__n
, __val
); }
1058 // Called by the range assign to implement [23.1.1]/9
1059 template<typename _InputIterator
>
1061 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1064 typedef typename
std::iterator_traits
<_InputIterator
>::
1065 iterator_category _IterCategory
;
1066 _M_assign_aux(__first
, __last
, _IterCategory());
1069 // Called by the second assign_dispatch above
1070 template<typename _InputIterator
>
1072 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1073 std::input_iterator_tag
);
1075 // Called by the second assign_dispatch above
1076 template<typename _ForwardIterator
>
1078 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1079 std::forward_iterator_tag
);
1081 // Called by assign(n,t), and the range assign when it turns out
1082 // to be the same thing.
1084 _M_fill_assign(size_type __n
, const value_type
& __val
);
1087 // Internal insert functions follow.
1089 // Called by the range insert to implement [23.1.1]/9
1091 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1092 // 438. Ambiguity in the "do the right thing" clause
1093 template<typename _Integer
>
1095 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1097 { _M_fill_insert(__pos
, __n
, __val
); }
1099 // Called by the range insert to implement [23.1.1]/9
1100 template<typename _InputIterator
>
1102 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1103 _InputIterator __last
, __false_type
)
1105 typedef typename
std::iterator_traits
<_InputIterator
>::
1106 iterator_category _IterCategory
;
1107 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1110 // Called by the second insert_dispatch above
1111 template<typename _InputIterator
>
1113 _M_range_insert(iterator __pos
, _InputIterator __first
,
1114 _InputIterator __last
, std::input_iterator_tag
);
1116 // Called by the second insert_dispatch above
1117 template<typename _ForwardIterator
>
1119 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1120 _ForwardIterator __last
, std::forward_iterator_tag
);
1122 // Called by insert(p,n,x), and the range insert when it turns out to be
1125 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1127 // Called by insert(p,x)
1128 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1130 _M_insert_aux(iterator __position
, const value_type
& __x
);
1132 template<typename
... _Args
>
1134 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1137 // Called by the latter.
1139 _M_check_len(size_type __n
, const char* __s
) const
1141 if (max_size() - size() < __n
)
1142 __throw_length_error(__N(__s
));
1144 const size_type __len
= size() + std::max(size(), __n
);
1145 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1148 // Internal erase functions follow.
1150 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1153 _M_erase_at_end(pointer __pos
)
1155 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1156 this->_M_impl
._M_finish
= __pos
;
1162 * @brief Vector equality comparison.
1163 * @param x A %vector.
1164 * @param y A %vector of the same type as @a x.
1165 * @return True iff the size and elements of the vectors are equal.
1167 * This is an equivalence relation. It is linear in the size of the
1168 * vectors. Vectors are considered equivalent if their sizes are equal,
1169 * and if corresponding elements compare equal.
1171 template<typename _Tp
, typename _Alloc
>
1173 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1174 { return (__x
.size() == __y
.size()
1175 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1178 * @brief Vector ordering relation.
1179 * @param x A %vector.
1180 * @param y A %vector of the same type as @a x.
1181 * @return True iff @a x is lexicographically less than @a y.
1183 * This is a total ordering relation. It is linear in the size of the
1184 * vectors. The elements must be comparable with @c <.
1186 * See std::lexicographical_compare() for how the determination is made.
1188 template<typename _Tp
, typename _Alloc
>
1190 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1191 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1192 __y
.begin(), __y
.end()); }
1194 /// Based on operator==
1195 template<typename _Tp
, typename _Alloc
>
1197 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1198 { return !(__x
== __y
); }
1200 /// Based on operator<
1201 template<typename _Tp
, typename _Alloc
>
1203 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1204 { return __y
< __x
; }
1206 /// Based on operator<
1207 template<typename _Tp
, typename _Alloc
>
1209 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1210 { return !(__y
< __x
); }
1212 /// Based on operator<
1213 template<typename _Tp
, typename _Alloc
>
1215 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1216 { return !(__x
< __y
); }
1218 /// See std::vector::swap().
1219 template<typename _Tp
, typename _Alloc
>
1221 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1224 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1225 template<typename _Tp
, typename _Alloc
>
1227 swap(vector
<_Tp
, _Alloc
>&& __x
, vector
<_Tp
, _Alloc
>& __y
)
1230 template<typename _Tp
, typename _Alloc
>
1232 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>&& __y
)
1236 _GLIBCXX_END_NESTED_NAMESPACE
1238 #endif /* _STL_VECTOR_H */