1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_vector.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{vector}
58 #define _STL_VECTOR_H 1
60 #include <bits/stl_iterator_base_funcs.h>
61 #include <bits/functexcept.h>
62 #include <bits/concept_check.h>
63 #include <initializer_list>
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 /// See bits/stl_deque.h's _Deque_base for an explanation.
70 template<typename _Tp
, typename _Alloc
>
73 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
74 typedef typename
__gnu_cxx::__alloc_traits
<_Tp_alloc_type
>::pointer
78 : public _Tp_alloc_type
82 pointer _M_end_of_storage
;
85 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
88 _Vector_impl(_Tp_alloc_type
const& __a
)
89 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92 #ifdef __GXX_EXPERIMENTAL_CXX0X__
93 _Vector_impl(_Tp_alloc_type
&& __a
)
94 : _Tp_alloc_type(std::move(__a
)),
95 _M_start(0), _M_finish(0), _M_end_of_storage(0)
99 void _M_swap_data(_Vector_impl
& __x
)
101 std::swap(_M_start
, __x
._M_start
);
102 std::swap(_M_finish
, __x
._M_finish
);
103 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
108 typedef _Alloc allocator_type
;
111 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
112 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
114 const _Tp_alloc_type
&
115 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
116 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
119 get_allocator() const _GLIBCXX_NOEXCEPT
120 { return allocator_type(_M_get_Tp_allocator()); }
125 _Vector_base(const allocator_type
& __a
)
128 _Vector_base(size_t __n
)
130 { _M_create_storage(__n
); }
132 _Vector_base(size_t __n
, const allocator_type
& __a
)
134 { _M_create_storage(__n
); }
136 #ifdef __GXX_EXPERIMENTAL_CXX0X__
137 _Vector_base(_Tp_alloc_type
&& __a
)
138 : _M_impl(std::move(__a
)) { }
140 _Vector_base(_Vector_base
&& __x
)
141 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
142 { this->_M_impl
._M_swap_data(__x
._M_impl
); }
144 _Vector_base(_Vector_base
&& __x
, const allocator_type
& __a
)
147 if (__x
.get_allocator() == __a
)
148 this->_M_impl
._M_swap_data(__x
._M_impl
);
151 size_t __n
= __x
._M_impl
._M_finish
- __x
._M_impl
._M_start
;
152 _M_create_storage(__n
);
158 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
159 - this->_M_impl
._M_start
); }
162 _Vector_impl _M_impl
;
165 _M_allocate(size_t __n
)
166 { return __n
!= 0 ? _M_impl
.allocate(__n
) : 0; }
169 _M_deallocate(pointer __p
, size_t __n
)
172 _M_impl
.deallocate(__p
, __n
);
177 _M_create_storage(size_t __n
)
179 this->_M_impl
._M_start
= this->_M_allocate(__n
);
180 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
181 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
187 * @brief A standard container which offers fixed time access to
188 * individual elements in any order.
192 * Meets the requirements of a <a href="tables.html#65">container</a>, a
193 * <a href="tables.html#66">reversible container</a>, and a
194 * <a href="tables.html#67">sequence</a>, including the
195 * <a href="tables.html#68">optional sequence requirements</a> with the
196 * %exception of @c push_front and @c pop_front.
198 * In some terminology a %vector can be described as a dynamic
199 * C-style array, it offers fast and efficient access to individual
200 * elements in any order and saves the user from worrying about
201 * memory and size allocation. Subscripting ( @c [] ) access is
202 * also provided as with C-style arrays.
204 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
205 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
207 // Concept requirements.
208 typedef typename
_Alloc::value_type _Alloc_value_type
;
209 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
210 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
212 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
213 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
216 typedef _Tp value_type
;
217 typedef typename
_Base::pointer pointer
;
218 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
219 typedef typename
_Alloc_traits::const_pointer const_pointer
;
220 typedef typename
_Alloc_traits::reference reference
;
221 typedef typename
_Alloc_traits::const_reference const_reference
;
222 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
223 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
225 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
226 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
227 typedef size_t size_type
;
228 typedef ptrdiff_t difference_type
;
229 typedef _Alloc allocator_type
;
232 using _Base::_M_allocate
;
233 using _Base::_M_deallocate
;
234 using _Base::_M_impl
;
235 using _Base::_M_get_Tp_allocator
;
238 // [23.2.4.1] construct/copy/destroy
239 // (assign() and get_allocator() are also listed in this section)
241 * @brief Default constructor creates no elements.
247 * @brief Creates a %vector with no elements.
248 * @param a An allocator object.
251 vector(const allocator_type
& __a
)
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
256 * @brief Creates a %vector with default constructed elements.
257 * @param n The number of elements to initially create.
259 * This constructor fills the %vector with @a n default
260 * constructed elements.
263 vector(size_type __n
)
265 { _M_default_initialize(__n
); }
268 * @brief Creates a %vector with copies of an exemplar element.
269 * @param n The number of elements to initially create.
270 * @param value An element to copy.
271 * @param a An allocator.
273 * This constructor fills the %vector with @a n copies of @a value.
275 vector(size_type __n
, const value_type
& __value
,
276 const allocator_type
& __a
= allocator_type())
278 { _M_fill_initialize(__n
, __value
); }
281 * @brief Creates a %vector with copies of an exemplar element.
282 * @param n The number of elements to initially create.
283 * @param value An element to copy.
284 * @param a An allocator.
286 * This constructor fills the %vector with @a n copies of @a value.
289 vector(size_type __n
, const value_type
& __value
= value_type(),
290 const allocator_type
& __a
= allocator_type())
292 { _M_fill_initialize(__n
, __value
); }
296 * @brief %Vector copy constructor.
297 * @param x A %vector of identical element and allocator types.
299 * The newly-created %vector uses a copy of the allocation
300 * object used by @a x. All the elements of @a x are copied,
301 * but any extra memory in
302 * @a x (for fast expansion) will not be copied.
304 vector(const vector
& __x
)
306 _Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()))
307 { this->_M_impl
._M_finish
=
308 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
309 this->_M_impl
._M_start
,
310 _M_get_Tp_allocator());
313 #ifdef __GXX_EXPERIMENTAL_CXX0X__
315 * @brief %Vector move constructor.
316 * @param x A %vector of identical element and allocator types.
318 * The newly-created %vector contains the exact contents of @a x.
319 * The contents of @a x are a valid, but unspecified %vector.
321 vector(vector
&& __x
) noexcept
322 : _Base(std::move(__x
)) { }
324 /// Copy constructor with alternative allocator
325 vector(const vector
& __x
, const allocator_type
& __a
)
326 : _Base(__x
.size(), __a
)
327 { this->_M_impl
._M_finish
=
328 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
329 this->_M_impl
._M_start
,
330 _M_get_Tp_allocator());
333 /// Move constructor with alternative allocator
334 vector(vector
&& __rv
, const allocator_type
& __m
)
335 : _Base(std::move(__rv
), __m
)
337 if (__rv
.get_allocator() != __m
)
339 this->_M_impl
._M_finish
=
340 std::__uninitialized_move_a(__rv
.begin(), __rv
.end(),
341 this->_M_impl
._M_start
,
342 _M_get_Tp_allocator());
348 * @brief Builds a %vector from an initializer list.
349 * @param l An initializer_list.
350 * @param a An allocator.
352 * Create a %vector consisting of copies of the elements in the
353 * initializer_list @a l.
355 * This will call the element type's copy constructor N times
356 * (where N is @a l.size()) and do no memory reallocation.
358 vector(initializer_list
<value_type
> __l
,
359 const allocator_type
& __a
= allocator_type())
362 _M_range_initialize(__l
.begin(), __l
.end(),
363 random_access_iterator_tag());
368 * @brief Builds a %vector from a range.
369 * @param first An input iterator.
370 * @param last An input iterator.
371 * @param a An allocator.
373 * Create a %vector consisting of copies of the elements from
376 * If the iterators are forward, bidirectional, or
377 * random-access, then this will call the elements' copy
378 * constructor N times (where N is distance(first,last)) and do
379 * no memory reallocation. But if only input iterators are
380 * used, then this will do at most 2N calls to the copy
381 * constructor, and logN memory reallocations.
383 template<typename _InputIterator
>
384 vector(_InputIterator __first
, _InputIterator __last
,
385 const allocator_type
& __a
= allocator_type())
388 // Check whether it's an integral type. If so, it's not an iterator.
389 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
390 _M_initialize_dispatch(__first
, __last
, _Integral());
394 * The dtor only erases the elements, and note that if the
395 * elements themselves are pointers, the pointed-to memory is
396 * not touched in any way. Managing the pointer is the user's
399 ~vector() _GLIBCXX_NOEXCEPT
400 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
401 _M_get_Tp_allocator()); }
404 * @brief %Vector assignment operator.
405 * @param x A %vector of identical element and allocator types.
407 * All the elements of @a x are copied, but any extra memory in
408 * @a x (for fast expansion) will not be copied. Unlike the
409 * copy constructor, the allocator object is not copied.
412 operator=(const vector
& __x
);
414 #ifdef __GXX_EXPERIMENTAL_CXX0X__
416 * @brief %Vector move assignment operator.
417 * @param x A %vector of identical element and allocator types.
419 * The contents of @a x are moved into this %vector (without copying).
420 * @a x is a valid, but unspecified %vector.
423 operator=(vector
&& __x
) noexcept(_Alloc_traits::_S_nothrow_move())
425 if (_Alloc_traits::_S_propagate_on_move_assign())
427 // We're moving the rvalue's allocator so can move the data too.
428 const vector
__tmp(std::move(*this)); // discard existing data
429 this->_M_impl
._M_swap_data(__x
._M_impl
);
430 std::__alloc_on_move(_M_get_Tp_allocator(),
431 __x
._M_get_Tp_allocator());
433 else if (_Alloc_traits::_S_always_equal()
434 || __x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
436 // The rvalue's allocator can free our storage and vice versa,
437 // so can swap the data storage after destroying our contents.
439 this->_M_impl
._M_swap_data(__x
._M_impl
);
443 // The rvalue's allocator cannot be moved, or is not equal,
444 // so we need to individually move each element.
445 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
446 std::__make_move_if_noexcept_iterator(__x
.end()));
453 * @brief %Vector list assignment operator.
454 * @param l An initializer_list.
456 * This function fills a %vector with copies of the elements in the
457 * initializer list @a l.
459 * Note that the assignment completely changes the %vector and
460 * that the resulting %vector's size is the same as the number
461 * of elements assigned. Old data may be lost.
464 operator=(initializer_list
<value_type
> __l
)
466 this->assign(__l
.begin(), __l
.end());
472 * @brief Assigns a given value to a %vector.
473 * @param n Number of elements to be assigned.
474 * @param val Value to be assigned.
476 * This function fills a %vector with @a n copies of the given
477 * value. Note that the assignment completely changes the
478 * %vector and that the resulting %vector's size is the same as
479 * the number of elements assigned. Old data may be lost.
482 assign(size_type __n
, const value_type
& __val
)
483 { _M_fill_assign(__n
, __val
); }
486 * @brief Assigns a range to a %vector.
487 * @param first An input iterator.
488 * @param last An input iterator.
490 * This function fills a %vector with copies of the elements in the
491 * range [first,last).
493 * Note that the assignment completely changes the %vector and
494 * that the resulting %vector's size is the same as the number
495 * of elements assigned. Old data may be lost.
497 template<typename _InputIterator
>
499 assign(_InputIterator __first
, _InputIterator __last
)
501 // Check whether it's an integral type. If so, it's not an iterator.
502 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
503 _M_assign_dispatch(__first
, __last
, _Integral());
506 #ifdef __GXX_EXPERIMENTAL_CXX0X__
508 * @brief Assigns an initializer list to a %vector.
509 * @param l An initializer_list.
511 * This function fills a %vector with copies of the elements in the
512 * initializer list @a l.
514 * Note that the assignment completely changes the %vector and
515 * that the resulting %vector's size is the same as the number
516 * of elements assigned. Old data may be lost.
519 assign(initializer_list
<value_type
> __l
)
520 { this->assign(__l
.begin(), __l
.end()); }
523 /// Get a copy of the memory allocation object.
524 using _Base::get_allocator
;
528 * Returns a read/write iterator that points to the first
529 * element in the %vector. Iteration is done in ordinary
533 begin() _GLIBCXX_NOEXCEPT
534 { return iterator(this->_M_impl
._M_start
); }
537 * Returns a read-only (constant) iterator that points to the
538 * first element in the %vector. Iteration is done in ordinary
542 begin() const _GLIBCXX_NOEXCEPT
543 { return const_iterator(this->_M_impl
._M_start
); }
546 * Returns a read/write iterator that points one past the last
547 * element in the %vector. Iteration is done in ordinary
551 end() _GLIBCXX_NOEXCEPT
552 { return iterator(this->_M_impl
._M_finish
); }
555 * Returns a read-only (constant) iterator that points one past
556 * the last element in the %vector. Iteration is done in
557 * ordinary element order.
560 end() const _GLIBCXX_NOEXCEPT
561 { return const_iterator(this->_M_impl
._M_finish
); }
564 * Returns a read/write reverse iterator that points to the
565 * last element in the %vector. Iteration is done in reverse
569 rbegin() _GLIBCXX_NOEXCEPT
570 { return reverse_iterator(end()); }
573 * Returns a read-only (constant) reverse iterator that points
574 * to the last element in the %vector. Iteration is done in
575 * reverse element order.
577 const_reverse_iterator
578 rbegin() const _GLIBCXX_NOEXCEPT
579 { return const_reverse_iterator(end()); }
582 * Returns a read/write reverse iterator that points to one
583 * before the first element in the %vector. Iteration is done
584 * in reverse element order.
587 rend() _GLIBCXX_NOEXCEPT
588 { return reverse_iterator(begin()); }
591 * Returns a read-only (constant) reverse iterator that points
592 * to one before the first element in the %vector. Iteration
593 * is done in reverse element order.
595 const_reverse_iterator
596 rend() const _GLIBCXX_NOEXCEPT
597 { return const_reverse_iterator(begin()); }
599 #ifdef __GXX_EXPERIMENTAL_CXX0X__
601 * Returns a read-only (constant) iterator that points to the
602 * first element in the %vector. Iteration is done in ordinary
606 cbegin() const noexcept
607 { return const_iterator(this->_M_impl
._M_start
); }
610 * Returns a read-only (constant) iterator that points one past
611 * the last element in the %vector. Iteration is done in
612 * ordinary element order.
615 cend() const noexcept
616 { return const_iterator(this->_M_impl
._M_finish
); }
619 * Returns a read-only (constant) reverse iterator that points
620 * to the last element in the %vector. Iteration is done in
621 * reverse element order.
623 const_reverse_iterator
624 crbegin() const noexcept
625 { return const_reverse_iterator(end()); }
628 * Returns a read-only (constant) reverse iterator that points
629 * to one before the first element in the %vector. Iteration
630 * is done in reverse element order.
632 const_reverse_iterator
633 crend() const noexcept
634 { return const_reverse_iterator(begin()); }
637 // [23.2.4.2] capacity
638 /** Returns the number of elements in the %vector. */
640 size() const _GLIBCXX_NOEXCEPT
641 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
643 /** Returns the size() of the largest possible %vector. */
645 max_size() const _GLIBCXX_NOEXCEPT
646 { return _M_get_Tp_allocator().max_size(); }
648 #ifdef __GXX_EXPERIMENTAL_CXX0X__
650 * @brief Resizes the %vector to the specified number of elements.
651 * @param new_size Number of elements the %vector should contain.
653 * This function will %resize the %vector to the specified
654 * number of elements. If the number is smaller than the
655 * %vector's current size the %vector is truncated, otherwise
656 * default constructed elements are appended.
659 resize(size_type __new_size
)
661 if (__new_size
> size())
662 _M_default_append(__new_size
- size());
663 else if (__new_size
< size())
664 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
668 * @brief Resizes the %vector to the specified number of elements.
669 * @param new_size Number of elements the %vector should contain.
670 * @param x Data with which new elements should be populated.
672 * This function will %resize the %vector to the specified
673 * number of elements. If the number is smaller than the
674 * %vector's current size the %vector is truncated, otherwise
675 * the %vector is extended and new elements are populated with
679 resize(size_type __new_size
, const value_type
& __x
)
681 if (__new_size
> size())
682 insert(end(), __new_size
- size(), __x
);
683 else if (__new_size
< size())
684 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
688 * @brief Resizes the %vector to the specified number of elements.
689 * @param new_size Number of elements the %vector should contain.
690 * @param x Data with which new elements should be populated.
692 * This function will %resize the %vector to the specified
693 * number of elements. If the number is smaller than the
694 * %vector's current size the %vector is truncated, otherwise
695 * the %vector is extended and new elements are populated with
699 resize(size_type __new_size
, value_type __x
= value_type())
701 if (__new_size
> size())
702 insert(end(), __new_size
- size(), __x
);
703 else if (__new_size
< size())
704 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
708 #ifdef __GXX_EXPERIMENTAL_CXX0X__
709 /** A non-binding request to reduce capacity() to size(). */
712 { _M_shrink_to_fit(); }
716 * Returns the total number of elements that the %vector can
717 * hold before needing to allocate more memory.
720 capacity() const _GLIBCXX_NOEXCEPT
721 { return size_type(this->_M_impl
._M_end_of_storage
722 - this->_M_impl
._M_start
); }
725 * Returns true if the %vector is empty. (Thus begin() would
729 empty() const _GLIBCXX_NOEXCEPT
730 { return begin() == end(); }
733 * @brief Attempt to preallocate enough memory for specified number of
735 * @param n Number of elements required.
736 * @throw std::length_error If @a n exceeds @c max_size().
738 * This function attempts to reserve enough memory for the
739 * %vector to hold the specified number of elements. If the
740 * number requested is more than max_size(), length_error is
743 * The advantage of this function is that if optimal code is a
744 * necessity and the user can determine the number of elements
745 * that will be required, the user can reserve the memory in
746 * %advance, and thus prevent a possible reallocation of memory
747 * and copying of %vector data.
750 reserve(size_type __n
);
754 * @brief Subscript access to the data contained in the %vector.
755 * @param n The index of the element for which data should be
757 * @return Read/write reference to data.
759 * This operator allows for easy, array-style, data access.
760 * Note that data access with this operator is unchecked and
761 * out_of_range lookups are not defined. (For checked lookups
765 operator[](size_type __n
)
766 { return *(this->_M_impl
._M_start
+ __n
); }
769 * @brief Subscript access to the data contained in the %vector.
770 * @param n The index of the element for which data should be
772 * @return Read-only (constant) reference to data.
774 * This operator allows for easy, array-style, data access.
775 * Note that data access with this operator is unchecked and
776 * out_of_range lookups are not defined. (For checked lookups
780 operator[](size_type __n
) const
781 { return *(this->_M_impl
._M_start
+ __n
); }
784 /// Safety check used only from at().
786 _M_range_check(size_type __n
) const
788 if (__n
>= this->size())
789 __throw_out_of_range(__N("vector::_M_range_check"));
794 * @brief Provides access to the data contained in the %vector.
795 * @param n The index of the element for which data should be
797 * @return Read/write reference to data.
798 * @throw std::out_of_range If @a n is an invalid index.
800 * This function provides for safer data access. The parameter
801 * is first checked that it is in the range of the vector. The
802 * function throws out_of_range if the check fails.
812 * @brief Provides access to the data contained in the %vector.
813 * @param n The index of the element for which data should be
815 * @return Read-only (constant) reference to data.
816 * @throw std::out_of_range If @a n is an invalid index.
818 * This function provides for safer data access. The parameter
819 * is first checked that it is in the range of the vector. The
820 * function throws out_of_range if the check fails.
823 at(size_type __n
) const
830 * Returns a read/write reference to the data at the first
831 * element of the %vector.
838 * Returns a read-only (constant) reference to the data at the first
839 * element of the %vector.
846 * Returns a read/write reference to the data at the last
847 * element of the %vector.
851 { return *(end() - 1); }
854 * Returns a read-only (constant) reference to the data at the
855 * last element of the %vector.
859 { return *(end() - 1); }
861 // _GLIBCXX_RESOLVE_LIB_DEFECTS
862 // DR 464. Suggestion for new member functions in standard containers.
865 * Returns a pointer such that [data(), data() + size()) is a valid
866 * range. For a non-empty %vector, data() == &front().
868 #ifdef __GXX_EXPERIMENTAL_CXX0X__
873 data() _GLIBCXX_NOEXCEPT
874 { return std::__addressof(front()); }
876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
881 data() const _GLIBCXX_NOEXCEPT
882 { return std::__addressof(front()); }
884 // [23.2.4.3] modifiers
886 * @brief Add data to the end of the %vector.
887 * @param x Data to be added.
889 * This is a typical stack operation. The function creates an
890 * element at the end of the %vector and assigns the given data
891 * to it. Due to the nature of a %vector this operation can be
892 * done in constant time if the %vector has preallocated space
896 push_back(const value_type
& __x
)
898 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
900 _Alloc_traits::construct(this->_M_impl
, this->_M_impl
._M_finish
,
902 ++this->_M_impl
._M_finish
;
905 _M_insert_aux(end(), __x
);
908 #ifdef __GXX_EXPERIMENTAL_CXX0X__
910 push_back(value_type
&& __x
)
911 { emplace_back(std::move(__x
)); }
913 template<typename
... _Args
>
915 emplace_back(_Args
&&... __args
);
919 * @brief Removes last element.
921 * This is a typical stack operation. It shrinks the %vector by one.
923 * Note that no data is returned, and if the last element's
924 * data is needed, it should be retrieved before pop_back() is
930 --this->_M_impl
._M_finish
;
931 _Alloc_traits::destroy(this->_M_impl
, this->_M_impl
._M_finish
);
934 #ifdef __GXX_EXPERIMENTAL_CXX0X__
936 * @brief Inserts an object in %vector before specified iterator.
937 * @param position An iterator into the %vector.
938 * @param args Arguments.
939 * @return An iterator that points to the inserted data.
941 * This function will insert an object of type T constructed
942 * with T(std::forward<Args>(args)...) before the specified location.
943 * Note that this kind of operation could be expensive for a %vector
944 * and if it is frequently used the user should consider using
947 template<typename
... _Args
>
949 emplace(iterator __position
, _Args
&&... __args
);
953 * @brief Inserts given value into %vector before specified iterator.
954 * @param position An iterator into the %vector.
955 * @param x Data to be inserted.
956 * @return An iterator that points to the inserted data.
958 * This function will insert a copy of the given value before
959 * the specified location. Note that this kind of operation
960 * could be expensive for a %vector and if it is frequently
961 * used the user should consider using std::list.
964 insert(iterator __position
, const value_type
& __x
);
966 #ifdef __GXX_EXPERIMENTAL_CXX0X__
968 * @brief Inserts given rvalue into %vector before specified iterator.
969 * @param position An iterator into the %vector.
970 * @param x Data to be inserted.
971 * @return An iterator that points to the inserted data.
973 * This function will insert a copy of the given rvalue before
974 * the specified location. Note that this kind of operation
975 * could be expensive for a %vector and if it is frequently
976 * used the user should consider using std::list.
979 insert(iterator __position
, value_type
&& __x
)
980 { return emplace(__position
, std::move(__x
)); }
983 * @brief Inserts an initializer_list into the %vector.
984 * @param position An iterator into the %vector.
985 * @param l An initializer_list.
987 * This function will insert copies of the data in the
988 * initializer_list @a l into the %vector before the location
989 * specified by @a position.
991 * Note that this kind of operation could be expensive for a
992 * %vector and if it is frequently used the user should
993 * consider using std::list.
996 insert(iterator __position
, initializer_list
<value_type
> __l
)
997 { this->insert(__position
, __l
.begin(), __l
.end()); }
1001 * @brief Inserts a number of copies of given data into the %vector.
1002 * @param position An iterator into the %vector.
1003 * @param n Number of elements to be inserted.
1004 * @param x Data to be inserted.
1006 * This function will insert a specified number of copies of
1007 * the given data before the location specified by @a position.
1009 * Note that this kind of operation could be expensive for a
1010 * %vector and if it is frequently used the user should
1011 * consider using std::list.
1014 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1015 { _M_fill_insert(__position
, __n
, __x
); }
1018 * @brief Inserts a range into the %vector.
1019 * @param position An iterator into the %vector.
1020 * @param first An input iterator.
1021 * @param last An input iterator.
1023 * This function will insert copies of the data in the range
1024 * [first,last) into the %vector before the location specified
1027 * Note that this kind of operation could be expensive for a
1028 * %vector and if it is frequently used the user should
1029 * consider using std::list.
1031 template<typename _InputIterator
>
1033 insert(iterator __position
, _InputIterator __first
,
1034 _InputIterator __last
)
1036 // Check whether it's an integral type. If so, it's not an iterator.
1037 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1038 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1042 * @brief Remove element at given position.
1043 * @param position Iterator pointing to element to be erased.
1044 * @return An iterator pointing to the next element (or end()).
1046 * This function will erase the element at the given position and thus
1047 * shorten the %vector by one.
1049 * Note This operation could be expensive and if it is
1050 * frequently used the user should consider using std::list.
1051 * The user is also cautioned that this function only erases
1052 * the element, and that if the element is itself a pointer,
1053 * the pointed-to memory is not touched in any way. Managing
1054 * the pointer is the user's responsibility.
1057 erase(iterator __position
);
1060 * @brief Remove a range of elements.
1061 * @param first Iterator pointing to the first element to be erased.
1062 * @param last Iterator pointing to one past the last element to be
1064 * @return An iterator pointing to the element pointed to by @a last
1065 * prior to erasing (or end()).
1067 * This function will erase the elements in the range [first,last) and
1068 * shorten the %vector accordingly.
1070 * Note This operation could be expensive and if it is
1071 * frequently used the user should consider using std::list.
1072 * The user is also cautioned that this function only erases
1073 * the elements, and that if the elements themselves are
1074 * pointers, the pointed-to memory is not touched in any way.
1075 * Managing the pointer is the user's responsibility.
1078 erase(iterator __first
, iterator __last
);
1081 * @brief Swaps data with another %vector.
1082 * @param x A %vector of the same element and allocator types.
1084 * This exchanges the elements between two vectors in constant time.
1085 * (Three pointers, so it should be quite fast.)
1086 * Note that the global std::swap() function is specialized such that
1087 * std::swap(v1,v2) will feed to this function.
1091 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1092 noexcept(_Alloc_traits::_S_nothrow_swap())
1095 this->_M_impl
._M_swap_data(__x
._M_impl
);
1096 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1097 __x
._M_get_Tp_allocator());
1101 * Erases all the elements. Note that this function only erases the
1102 * elements, and that if the elements themselves are pointers, the
1103 * pointed-to memory is not touched in any way. Managing the pointer is
1104 * the user's responsibility.
1107 clear() _GLIBCXX_NOEXCEPT
1108 { _M_erase_at_end(this->_M_impl
._M_start
); }
1112 * Memory expansion handler. Uses the member allocation function to
1113 * obtain @a n bytes of memory, and then copies [first,last) into it.
1115 template<typename _ForwardIterator
>
1117 _M_allocate_and_copy(size_type __n
,
1118 _ForwardIterator __first
, _ForwardIterator __last
)
1120 pointer __result
= this->_M_allocate(__n
);
1123 std::__uninitialized_copy_a(__first
, __last
, __result
,
1124 _M_get_Tp_allocator());
1129 _M_deallocate(__result
, __n
);
1130 __throw_exception_again
;
1135 // Internal constructor functions follow.
1137 // Called by the range constructor to implement [23.1.1]/9
1139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1140 // 438. Ambiguity in the "do the right thing" clause
1141 template<typename _Integer
>
1143 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1145 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1146 this->_M_impl
._M_end_of_storage
=
1147 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1148 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1151 // Called by the range constructor to implement [23.1.1]/9
1152 template<typename _InputIterator
>
1154 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1157 typedef typename
std::iterator_traits
<_InputIterator
>::
1158 iterator_category _IterCategory
;
1159 _M_range_initialize(__first
, __last
, _IterCategory());
1162 // Called by the second initialize_dispatch above
1163 template<typename _InputIterator
>
1165 _M_range_initialize(_InputIterator __first
,
1166 _InputIterator __last
, std::input_iterator_tag
)
1168 for (; __first
!= __last
; ++__first
)
1169 push_back(*__first
);
1172 // Called by the second initialize_dispatch above
1173 template<typename _ForwardIterator
>
1175 _M_range_initialize(_ForwardIterator __first
,
1176 _ForwardIterator __last
, std::forward_iterator_tag
)
1178 const size_type __n
= std::distance(__first
, __last
);
1179 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1180 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1181 this->_M_impl
._M_finish
=
1182 std::__uninitialized_copy_a(__first
, __last
,
1183 this->_M_impl
._M_start
,
1184 _M_get_Tp_allocator());
1187 // Called by the first initialize_dispatch above and by the
1188 // vector(n,value,a) constructor.
1190 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1192 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1193 _M_get_Tp_allocator());
1194 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1197 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1198 // Called by the vector(n) constructor.
1200 _M_default_initialize(size_type __n
)
1202 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1203 _M_get_Tp_allocator());
1204 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1208 // Internal assign functions follow. The *_aux functions do the actual
1209 // assignment work for the range versions.
1211 // Called by the range assign to implement [23.1.1]/9
1213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1214 // 438. Ambiguity in the "do the right thing" clause
1215 template<typename _Integer
>
1217 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1218 { _M_fill_assign(__n
, __val
); }
1220 // Called by the range assign to implement [23.1.1]/9
1221 template<typename _InputIterator
>
1223 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1226 typedef typename
std::iterator_traits
<_InputIterator
>::
1227 iterator_category _IterCategory
;
1228 _M_assign_aux(__first
, __last
, _IterCategory());
1231 // Called by the second assign_dispatch above
1232 template<typename _InputIterator
>
1234 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1235 std::input_iterator_tag
);
1237 // Called by the second assign_dispatch above
1238 template<typename _ForwardIterator
>
1240 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1241 std::forward_iterator_tag
);
1243 // Called by assign(n,t), and the range assign when it turns out
1244 // to be the same thing.
1246 _M_fill_assign(size_type __n
, const value_type
& __val
);
1249 // Internal insert functions follow.
1251 // Called by the range insert to implement [23.1.1]/9
1253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1254 // 438. Ambiguity in the "do the right thing" clause
1255 template<typename _Integer
>
1257 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1259 { _M_fill_insert(__pos
, __n
, __val
); }
1261 // Called by the range insert to implement [23.1.1]/9
1262 template<typename _InputIterator
>
1264 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1265 _InputIterator __last
, __false_type
)
1267 typedef typename
std::iterator_traits
<_InputIterator
>::
1268 iterator_category _IterCategory
;
1269 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1272 // Called by the second insert_dispatch above
1273 template<typename _InputIterator
>
1275 _M_range_insert(iterator __pos
, _InputIterator __first
,
1276 _InputIterator __last
, std::input_iterator_tag
);
1278 // Called by the second insert_dispatch above
1279 template<typename _ForwardIterator
>
1281 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1282 _ForwardIterator __last
, std::forward_iterator_tag
);
1284 // Called by insert(p,n,x), and the range insert when it turns out to be
1287 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1289 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1290 // Called by resize(n).
1292 _M_default_append(size_type __n
);
1298 // Called by insert(p,x)
1299 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1301 _M_insert_aux(iterator __position
, const value_type
& __x
);
1303 template<typename
... _Args
>
1305 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1308 // Called by the latter.
1310 _M_check_len(size_type __n
, const char* __s
) const
1312 if (max_size() - size() < __n
)
1313 __throw_length_error(__N(__s
));
1315 const size_type __len
= size() + std::max(size(), __n
);
1316 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1319 // Internal erase functions follow.
1321 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1324 _M_erase_at_end(pointer __pos
)
1326 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1327 this->_M_impl
._M_finish
= __pos
;
1333 * @brief Vector equality comparison.
1334 * @param x A %vector.
1335 * @param y A %vector of the same type as @a x.
1336 * @return True iff the size and elements of the vectors are equal.
1338 * This is an equivalence relation. It is linear in the size of the
1339 * vectors. Vectors are considered equivalent if their sizes are equal,
1340 * and if corresponding elements compare equal.
1342 template<typename _Tp
, typename _Alloc
>
1344 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1345 { return (__x
.size() == __y
.size()
1346 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1349 * @brief Vector ordering relation.
1350 * @param x A %vector.
1351 * @param y A %vector of the same type as @a x.
1352 * @return True iff @a x is lexicographically less than @a y.
1354 * This is a total ordering relation. It is linear in the size of the
1355 * vectors. The elements must be comparable with @c <.
1357 * See std::lexicographical_compare() for how the determination is made.
1359 template<typename _Tp
, typename _Alloc
>
1361 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1362 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1363 __y
.begin(), __y
.end()); }
1365 /// Based on operator==
1366 template<typename _Tp
, typename _Alloc
>
1368 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1369 { return !(__x
== __y
); }
1371 /// Based on operator<
1372 template<typename _Tp
, typename _Alloc
>
1374 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1375 { return __y
< __x
; }
1377 /// Based on operator<
1378 template<typename _Tp
, typename _Alloc
>
1380 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1381 { return !(__y
< __x
); }
1383 /// Based on operator<
1384 template<typename _Tp
, typename _Alloc
>
1386 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1387 { return !(__x
< __y
); }
1389 /// See std::vector::swap().
1390 template<typename _Tp
, typename _Alloc
>
1392 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1395 _GLIBCXX_END_NAMESPACE_CONTAINER
1398 #endif /* _STL_VECTOR_H */