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
;
76 : public _Tp_alloc_type
78 typename
_Tp_alloc_type::pointer _M_start
;
79 typename
_Tp_alloc_type::pointer _M_finish
;
80 typename
_Tp_alloc_type::pointer _M_end_of_storage
;
83 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
86 _Vector_impl(_Tp_alloc_type
const& __a
)
87 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
90 #ifdef __GXX_EXPERIMENTAL_CXX0X__
91 _Vector_impl(_Tp_alloc_type
&& __a
)
92 : _Tp_alloc_type(std::move(__a
)),
93 _M_start(0), _M_finish(0), _M_end_of_storage(0)
99 typedef _Alloc allocator_type
;
102 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
103 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
105 const _Tp_alloc_type
&
106 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
107 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
110 get_allocator() const _GLIBCXX_NOEXCEPT
111 { return allocator_type(_M_get_Tp_allocator()); }
116 _Vector_base(const allocator_type
& __a
)
119 _Vector_base(size_t __n
)
122 this->_M_impl
._M_start
= this->_M_allocate(__n
);
123 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
124 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
127 _Vector_base(size_t __n
, const allocator_type
& __a
)
130 this->_M_impl
._M_start
= this->_M_allocate(__n
);
131 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
132 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
135 #ifdef __GXX_EXPERIMENTAL_CXX0X__
136 _Vector_base(_Vector_base
&& __x
)
137 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
139 this->_M_impl
._M_start
= __x
._M_impl
._M_start
;
140 this->_M_impl
._M_finish
= __x
._M_impl
._M_finish
;
141 this->_M_impl
._M_end_of_storage
= __x
._M_impl
._M_end_of_storage
;
142 __x
._M_impl
._M_start
= 0;
143 __x
._M_impl
._M_finish
= 0;
144 __x
._M_impl
._M_end_of_storage
= 0;
149 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
150 - this->_M_impl
._M_start
); }
153 _Vector_impl _M_impl
;
155 typename
_Tp_alloc_type::pointer
156 _M_allocate(size_t __n
)
157 { return __n
!= 0 ? _M_impl
.allocate(__n
) : 0; }
160 _M_deallocate(typename
_Tp_alloc_type::pointer __p
, size_t __n
)
163 _M_impl
.deallocate(__p
, __n
);
169 * @brief A standard container which offers fixed time access to
170 * individual elements in any order.
174 * Meets the requirements of a <a href="tables.html#65">container</a>, a
175 * <a href="tables.html#66">reversible container</a>, and a
176 * <a href="tables.html#67">sequence</a>, including the
177 * <a href="tables.html#68">optional sequence requirements</a> with the
178 * %exception of @c push_front and @c pop_front.
180 * In some terminology a %vector can be described as a dynamic
181 * C-style array, it offers fast and efficient access to individual
182 * elements in any order and saves the user from worrying about
183 * memory and size allocation. Subscripting ( @c [] ) access is
184 * also provided as with C-style arrays.
186 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
187 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
189 // Concept requirements.
190 typedef typename
_Alloc::value_type _Alloc_value_type
;
191 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
192 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
194 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
195 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
198 typedef _Tp value_type
;
199 typedef typename
_Tp_alloc_type::pointer pointer
;
200 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
201 typedef typename
_Tp_alloc_type::reference reference
;
202 typedef typename
_Tp_alloc_type::const_reference const_reference
;
203 typedef __gnu_cxx::__normal_iterator
<pointer
, vector
> iterator
;
204 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector
>
206 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
207 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
208 typedef size_t size_type
;
209 typedef ptrdiff_t difference_type
;
210 typedef _Alloc allocator_type
;
213 using _Base::_M_allocate
;
214 using _Base::_M_deallocate
;
215 using _Base::_M_impl
;
216 using _Base::_M_get_Tp_allocator
;
219 // [23.2.4.1] construct/copy/destroy
220 // (assign() and get_allocator() are also listed in this section)
222 * @brief Default constructor creates no elements.
228 * @brief Creates a %vector with no elements.
229 * @param a An allocator object.
232 vector(const allocator_type
& __a
)
235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
237 * @brief Creates a %vector with default constructed elements.
238 * @param n The number of elements to initially create.
240 * This constructor fills the %vector with @a n default
241 * constructed elements.
244 vector(size_type __n
)
246 { _M_default_initialize(__n
); }
249 * @brief Creates a %vector with copies of an exemplar element.
250 * @param n The number of elements to initially create.
251 * @param value An element to copy.
252 * @param a An allocator.
254 * This constructor fills the %vector with @a n copies of @a value.
256 vector(size_type __n
, const value_type
& __value
,
257 const allocator_type
& __a
= allocator_type())
259 { _M_fill_initialize(__n
, __value
); }
262 * @brief Creates a %vector with copies of an exemplar element.
263 * @param n The number of elements to initially create.
264 * @param value An element to copy.
265 * @param a An allocator.
267 * This constructor fills the %vector with @a n copies of @a value.
270 vector(size_type __n
, const value_type
& __value
= value_type(),
271 const allocator_type
& __a
= allocator_type())
273 { _M_fill_initialize(__n
, __value
); }
277 * @brief %Vector copy constructor.
278 * @param x A %vector of identical element and allocator types.
280 * The newly-created %vector uses a copy of the allocation
281 * object used by @a x. All the elements of @a x are copied,
282 * but any extra memory in
283 * @a x (for fast expansion) will not be copied.
285 vector(const vector
& __x
)
286 : _Base(__x
.size(), __x
._M_get_Tp_allocator())
287 { this->_M_impl
._M_finish
=
288 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
289 this->_M_impl
._M_start
,
290 _M_get_Tp_allocator());
293 #ifdef __GXX_EXPERIMENTAL_CXX0X__
295 * @brief %Vector move constructor.
296 * @param x A %vector of identical element and allocator types.
298 * The newly-created %vector contains the exact contents of @a x.
299 * The contents of @a x are a valid, but unspecified %vector.
301 vector(vector
&& __x
) noexcept
302 : _Base(std::move(__x
)) { }
305 * @brief Builds a %vector from an initializer list.
306 * @param l An initializer_list.
307 * @param a An allocator.
309 * Create a %vector consisting of copies of the elements in the
310 * initializer_list @a l.
312 * This will call the element type's copy constructor N times
313 * (where N is @a l.size()) and do no memory reallocation.
315 vector(initializer_list
<value_type
> __l
,
316 const allocator_type
& __a
= allocator_type())
319 _M_range_initialize(__l
.begin(), __l
.end(),
320 random_access_iterator_tag());
325 * @brief Builds a %vector from a range.
326 * @param first An input iterator.
327 * @param last An input iterator.
328 * @param a An allocator.
330 * Create a %vector consisting of copies of the elements from
333 * If the iterators are forward, bidirectional, or
334 * random-access, then this will call the elements' copy
335 * constructor N times (where N is distance(first,last)) and do
336 * no memory reallocation. But if only input iterators are
337 * used, then this will do at most 2N calls to the copy
338 * constructor, and logN memory reallocations.
340 template<typename _InputIterator
>
341 vector(_InputIterator __first
, _InputIterator __last
,
342 const allocator_type
& __a
= allocator_type())
345 // Check whether it's an integral type. If so, it's not an iterator.
346 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
347 _M_initialize_dispatch(__first
, __last
, _Integral());
351 * The dtor only erases the elements, and note that if the
352 * elements themselves are pointers, the pointed-to memory is
353 * not touched in any way. Managing the pointer is the user's
356 ~vector() _GLIBCXX_NOEXCEPT
357 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
358 _M_get_Tp_allocator()); }
361 * @brief %Vector assignment operator.
362 * @param x A %vector of identical element and allocator types.
364 * All the elements of @a x are copied, but any extra memory in
365 * @a x (for fast expansion) will not be copied. Unlike the
366 * copy constructor, the allocator object is not copied.
369 operator=(const vector
& __x
);
371 #ifdef __GXX_EXPERIMENTAL_CXX0X__
373 * @brief %Vector move assignment operator.
374 * @param x A %vector of identical element and allocator types.
376 * The contents of @a x are moved into this %vector (without copying).
377 * @a x is a valid, but unspecified %vector.
380 operator=(vector
&& __x
)
390 * @brief %Vector list assignment operator.
391 * @param l An initializer_list.
393 * This function fills a %vector with copies of the elements in the
394 * initializer list @a l.
396 * Note that the assignment completely changes the %vector and
397 * that the resulting %vector's size is the same as the number
398 * of elements assigned. Old data may be lost.
401 operator=(initializer_list
<value_type
> __l
)
403 this->assign(__l
.begin(), __l
.end());
409 * @brief Assigns a given value to a %vector.
410 * @param n Number of elements to be assigned.
411 * @param val Value to be assigned.
413 * This function fills a %vector with @a n copies of the given
414 * value. Note that the assignment completely changes the
415 * %vector and that the resulting %vector's size is the same as
416 * the number of elements assigned. Old data may be lost.
419 assign(size_type __n
, const value_type
& __val
)
420 { _M_fill_assign(__n
, __val
); }
423 * @brief Assigns a range to a %vector.
424 * @param first An input iterator.
425 * @param last An input iterator.
427 * This function fills a %vector with copies of the elements in the
428 * range [first,last).
430 * Note that the assignment completely changes the %vector and
431 * that the resulting %vector's size is the same as the number
432 * of elements assigned. Old data may be lost.
434 template<typename _InputIterator
>
436 assign(_InputIterator __first
, _InputIterator __last
)
438 // Check whether it's an integral type. If so, it's not an iterator.
439 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
440 _M_assign_dispatch(__first
, __last
, _Integral());
443 #ifdef __GXX_EXPERIMENTAL_CXX0X__
445 * @brief Assigns an initializer list to a %vector.
446 * @param l An initializer_list.
448 * This function fills a %vector with copies of the elements in the
449 * initializer list @a l.
451 * Note that the assignment completely changes the %vector and
452 * that the resulting %vector's size is the same as the number
453 * of elements assigned. Old data may be lost.
456 assign(initializer_list
<value_type
> __l
)
457 { this->assign(__l
.begin(), __l
.end()); }
460 /// Get a copy of the memory allocation object.
461 using _Base::get_allocator
;
465 * Returns a read/write iterator that points to the first
466 * element in the %vector. Iteration is done in ordinary
470 begin() _GLIBCXX_NOEXCEPT
471 { return iterator(this->_M_impl
._M_start
); }
474 * Returns a read-only (constant) iterator that points to the
475 * first element in the %vector. Iteration is done in ordinary
479 begin() const _GLIBCXX_NOEXCEPT
480 { return const_iterator(this->_M_impl
._M_start
); }
483 * Returns a read/write iterator that points one past the last
484 * element in the %vector. Iteration is done in ordinary
488 end() _GLIBCXX_NOEXCEPT
489 { return iterator(this->_M_impl
._M_finish
); }
492 * Returns a read-only (constant) iterator that points one past
493 * the last element in the %vector. Iteration is done in
494 * ordinary element order.
497 end() const _GLIBCXX_NOEXCEPT
498 { return const_iterator(this->_M_impl
._M_finish
); }
501 * Returns a read/write reverse iterator that points to the
502 * last element in the %vector. Iteration is done in reverse
506 rbegin() _GLIBCXX_NOEXCEPT
507 { return reverse_iterator(end()); }
510 * Returns a read-only (constant) reverse iterator that points
511 * to the last element in the %vector. Iteration is done in
512 * reverse element order.
514 const_reverse_iterator
515 rbegin() const _GLIBCXX_NOEXCEPT
516 { return const_reverse_iterator(end()); }
519 * Returns a read/write reverse iterator that points to one
520 * before the first element in the %vector. Iteration is done
521 * in reverse element order.
524 rend() _GLIBCXX_NOEXCEPT
525 { return reverse_iterator(begin()); }
528 * Returns a read-only (constant) reverse iterator that points
529 * to one before the first element in the %vector. Iteration
530 * is done in reverse element order.
532 const_reverse_iterator
533 rend() const _GLIBCXX_NOEXCEPT
534 { return const_reverse_iterator(begin()); }
536 #ifdef __GXX_EXPERIMENTAL_CXX0X__
538 * Returns a read-only (constant) iterator that points to the
539 * first element in the %vector. Iteration is done in ordinary
543 cbegin() const noexcept
544 { return const_iterator(this->_M_impl
._M_start
); }
547 * Returns a read-only (constant) iterator that points one past
548 * the last element in the %vector. Iteration is done in
549 * ordinary element order.
552 cend() const noexcept
553 { return const_iterator(this->_M_impl
._M_finish
); }
556 * Returns a read-only (constant) reverse iterator that points
557 * to the last element in the %vector. Iteration is done in
558 * reverse element order.
560 const_reverse_iterator
561 crbegin() const noexcept
562 { return const_reverse_iterator(end()); }
565 * Returns a read-only (constant) reverse iterator that points
566 * to one before the first element in the %vector. Iteration
567 * is done in reverse element order.
569 const_reverse_iterator
570 crend() const noexcept
571 { return const_reverse_iterator(begin()); }
574 // [23.2.4.2] capacity
575 /** Returns the number of elements in the %vector. */
577 size() const _GLIBCXX_NOEXCEPT
578 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
580 /** Returns the size() of the largest possible %vector. */
582 max_size() const _GLIBCXX_NOEXCEPT
583 { return _M_get_Tp_allocator().max_size(); }
585 #ifdef __GXX_EXPERIMENTAL_CXX0X__
587 * @brief Resizes the %vector to the specified number of elements.
588 * @param new_size Number of elements the %vector should contain.
590 * This function will %resize the %vector to the specified
591 * number of elements. If the number is smaller than the
592 * %vector's current size the %vector is truncated, otherwise
593 * default constructed elements are appended.
596 resize(size_type __new_size
)
598 if (__new_size
> size())
599 _M_default_append(__new_size
- size());
600 else if (__new_size
< size())
601 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
605 * @brief Resizes the %vector to the specified number of elements.
606 * @param new_size Number of elements the %vector should contain.
607 * @param x Data with which new elements should be populated.
609 * This function will %resize the %vector to the specified
610 * number of elements. If the number is smaller than the
611 * %vector's current size the %vector is truncated, otherwise
612 * the %vector is extended and new elements are populated with
616 resize(size_type __new_size
, const value_type
& __x
)
618 if (__new_size
> size())
619 insert(end(), __new_size
- size(), __x
);
620 else if (__new_size
< size())
621 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
625 * @brief Resizes the %vector to the specified number of elements.
626 * @param new_size Number of elements the %vector should contain.
627 * @param x Data with which new elements should be populated.
629 * This function will %resize the %vector to the specified
630 * number of elements. If the number is smaller than the
631 * %vector's current size the %vector is truncated, otherwise
632 * the %vector is extended and new elements are populated with
636 resize(size_type __new_size
, value_type __x
= value_type())
638 if (__new_size
> size())
639 insert(end(), __new_size
- size(), __x
);
640 else if (__new_size
< size())
641 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
646 /** A non-binding request to reduce capacity() to size(). */
649 { _M_shrink_to_fit(); }
653 * Returns the total number of elements that the %vector can
654 * hold before needing to allocate more memory.
657 capacity() const _GLIBCXX_NOEXCEPT
658 { return size_type(this->_M_impl
._M_end_of_storage
659 - this->_M_impl
._M_start
); }
662 * Returns true if the %vector is empty. (Thus begin() would
666 empty() const _GLIBCXX_NOEXCEPT
667 { return begin() == end(); }
670 * @brief Attempt to preallocate enough memory for specified number of
672 * @param n Number of elements required.
673 * @throw std::length_error If @a n exceeds @c max_size().
675 * This function attempts to reserve enough memory for the
676 * %vector to hold the specified number of elements. If the
677 * number requested is more than max_size(), length_error is
680 * The advantage of this function is that if optimal code is a
681 * necessity and the user can determine the number of elements
682 * that will be required, the user can reserve the memory in
683 * %advance, and thus prevent a possible reallocation of memory
684 * and copying of %vector data.
687 reserve(size_type __n
);
691 * @brief Subscript access to the data contained in the %vector.
692 * @param n The index of the element for which data should be
694 * @return Read/write reference to data.
696 * This operator allows for easy, array-style, data access.
697 * Note that data access with this operator is unchecked and
698 * out_of_range lookups are not defined. (For checked lookups
702 operator[](size_type __n
)
703 { return *(this->_M_impl
._M_start
+ __n
); }
706 * @brief Subscript access to the data contained in the %vector.
707 * @param n The index of the element for which data should be
709 * @return Read-only (constant) reference to data.
711 * This operator allows for easy, array-style, data access.
712 * Note that data access with this operator is unchecked and
713 * out_of_range lookups are not defined. (For checked lookups
717 operator[](size_type __n
) const
718 { return *(this->_M_impl
._M_start
+ __n
); }
721 /// Safety check used only from at().
723 _M_range_check(size_type __n
) const
725 if (__n
>= this->size())
726 __throw_out_of_range(__N("vector::_M_range_check"));
731 * @brief Provides access to the data contained in the %vector.
732 * @param n The index of the element for which data should be
734 * @return Read/write reference to data.
735 * @throw std::out_of_range If @a n is an invalid index.
737 * This function provides for safer data access. The parameter
738 * is first checked that it is in the range of the vector. The
739 * function throws out_of_range if the check fails.
749 * @brief Provides access to the data contained in the %vector.
750 * @param n The index of the element for which data should be
752 * @return Read-only (constant) reference to data.
753 * @throw std::out_of_range If @a n is an invalid index.
755 * This function provides for safer data access. The parameter
756 * is first checked that it is in the range of the vector. The
757 * function throws out_of_range if the check fails.
760 at(size_type __n
) const
767 * Returns a read/write reference to the data at the first
768 * element of the %vector.
775 * Returns a read-only (constant) reference to the data at the first
776 * element of the %vector.
783 * Returns a read/write reference to the data at the last
784 * element of the %vector.
788 { return *(end() - 1); }
791 * Returns a read-only (constant) reference to the data at the
792 * last element of the %vector.
796 { return *(end() - 1); }
798 // _GLIBCXX_RESOLVE_LIB_DEFECTS
799 // DR 464. Suggestion for new member functions in standard containers.
802 * Returns a pointer such that [data(), data() + size()) is a valid
803 * range. For a non-empty %vector, data() == &front().
805 #ifdef __GXX_EXPERIMENTAL_CXX0X__
810 data() _GLIBCXX_NOEXCEPT
811 { return std::__addressof(front()); }
813 #ifdef __GXX_EXPERIMENTAL_CXX0X__
818 data() const _GLIBCXX_NOEXCEPT
819 { return std::__addressof(front()); }
821 // [23.2.4.3] modifiers
823 * @brief Add data to the end of the %vector.
824 * @param x Data to be added.
826 * This is a typical stack operation. The function creates an
827 * element at the end of the %vector and assigns the given data
828 * to it. Due to the nature of a %vector this operation can be
829 * done in constant time if the %vector has preallocated space
833 push_back(const value_type
& __x
)
835 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
837 this->_M_impl
.construct(this->_M_impl
._M_finish
, __x
);
838 ++this->_M_impl
._M_finish
;
841 _M_insert_aux(end(), __x
);
844 #ifdef __GXX_EXPERIMENTAL_CXX0X__
846 push_back(value_type
&& __x
)
847 { emplace_back(std::move(__x
)); }
849 template<typename
... _Args
>
851 emplace_back(_Args
&&... __args
);
855 * @brief Removes last element.
857 * This is a typical stack operation. It shrinks the %vector by one.
859 * Note that no data is returned, and if the last element's
860 * data is needed, it should be retrieved before pop_back() is
866 --this->_M_impl
._M_finish
;
867 this->_M_impl
.destroy(this->_M_impl
._M_finish
);
870 #ifdef __GXX_EXPERIMENTAL_CXX0X__
872 * @brief Inserts an object in %vector before specified iterator.
873 * @param position An iterator into the %vector.
874 * @param args Arguments.
875 * @return An iterator that points to the inserted data.
877 * This function will insert an object of type T constructed
878 * with T(std::forward<Args>(args)...) before the specified location.
879 * Note that this kind of operation could be expensive for a %vector
880 * and if it is frequently used the user should consider using
883 template<typename
... _Args
>
885 emplace(iterator __position
, _Args
&&... __args
);
889 * @brief Inserts given value into %vector before specified iterator.
890 * @param position An iterator into the %vector.
891 * @param x Data to be inserted.
892 * @return An iterator that points to the inserted data.
894 * This function will insert a copy of the given value before
895 * the specified location. Note that this kind of operation
896 * could be expensive for a %vector and if it is frequently
897 * used the user should consider using std::list.
900 insert(iterator __position
, const value_type
& __x
);
902 #ifdef __GXX_EXPERIMENTAL_CXX0X__
904 * @brief Inserts given rvalue into %vector before specified iterator.
905 * @param position An iterator into the %vector.
906 * @param x Data to be inserted.
907 * @return An iterator that points to the inserted data.
909 * This function will insert a copy of the given rvalue before
910 * the specified location. Note that this kind of operation
911 * could be expensive for a %vector and if it is frequently
912 * used the user should consider using std::list.
915 insert(iterator __position
, value_type
&& __x
)
916 { return emplace(__position
, std::move(__x
)); }
919 * @brief Inserts an initializer_list into the %vector.
920 * @param position An iterator into the %vector.
921 * @param l An initializer_list.
923 * This function will insert copies of the data in the
924 * initializer_list @a l into the %vector before the location
925 * specified by @a position.
927 * Note that this kind of operation could be expensive for a
928 * %vector and if it is frequently used the user should
929 * consider using std::list.
932 insert(iterator __position
, initializer_list
<value_type
> __l
)
933 { this->insert(__position
, __l
.begin(), __l
.end()); }
937 * @brief Inserts a number of copies of given data into the %vector.
938 * @param position An iterator into the %vector.
939 * @param n Number of elements to be inserted.
940 * @param x Data to be inserted.
942 * This function will insert a specified number of copies of
943 * the given data before the location specified by @a position.
945 * Note that this kind of operation could be expensive for a
946 * %vector and if it is frequently used the user should
947 * consider using std::list.
950 insert(iterator __position
, size_type __n
, const value_type
& __x
)
951 { _M_fill_insert(__position
, __n
, __x
); }
954 * @brief Inserts a range into the %vector.
955 * @param position An iterator into the %vector.
956 * @param first An input iterator.
957 * @param last An input iterator.
959 * This function will insert copies of the data in the range
960 * [first,last) into the %vector before the location specified
963 * Note that this kind of operation could be expensive for a
964 * %vector and if it is frequently used the user should
965 * consider using std::list.
967 template<typename _InputIterator
>
969 insert(iterator __position
, _InputIterator __first
,
970 _InputIterator __last
)
972 // Check whether it's an integral type. If so, it's not an iterator.
973 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
974 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
978 * @brief Remove element at given position.
979 * @param position Iterator pointing to element to be erased.
980 * @return An iterator pointing to the next element (or end()).
982 * This function will erase the element at the given position and thus
983 * shorten the %vector by one.
985 * Note This operation could be expensive and if it is
986 * frequently used the user should consider using std::list.
987 * The user is also cautioned that this function only erases
988 * the element, and that if the element is itself a pointer,
989 * the pointed-to memory is not touched in any way. Managing
990 * the pointer is the user's responsibility.
993 erase(iterator __position
);
996 * @brief Remove a range of elements.
997 * @param first Iterator pointing to the first element to be erased.
998 * @param last Iterator pointing to one past the last element to be
1000 * @return An iterator pointing to the element pointed to by @a last
1001 * prior to erasing (or end()).
1003 * This function will erase the elements in the range [first,last) and
1004 * shorten the %vector accordingly.
1006 * Note This operation could be expensive and if it is
1007 * frequently used the user should consider using std::list.
1008 * The user is also cautioned that this function only erases
1009 * the elements, and that if the elements themselves are
1010 * pointers, the pointed-to memory is not touched in any way.
1011 * Managing the pointer is the user's responsibility.
1014 erase(iterator __first
, iterator __last
);
1017 * @brief Swaps data with another %vector.
1018 * @param x A %vector of the same element and allocator types.
1020 * This exchanges the elements between two vectors in constant time.
1021 * (Three pointers, so it should be quite fast.)
1022 * Note that the global std::swap() function is specialized such that
1023 * std::swap(v1,v2) will feed to this function.
1028 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1029 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1030 std::swap(this->_M_impl
._M_end_of_storage
,
1031 __x
._M_impl
._M_end_of_storage
);
1033 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1034 // 431. Swapping containers with unequal allocators.
1035 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1036 __x
._M_get_Tp_allocator());
1040 * Erases all the elements. Note that this function only erases the
1041 * elements, and that if the elements themselves are pointers, the
1042 * pointed-to memory is not touched in any way. Managing the pointer is
1043 * the user's responsibility.
1046 clear() _GLIBCXX_NOEXCEPT
1047 { _M_erase_at_end(this->_M_impl
._M_start
); }
1051 * Memory expansion handler. Uses the member allocation function to
1052 * obtain @a n bytes of memory, and then copies [first,last) into it.
1054 template<typename _ForwardIterator
>
1056 _M_allocate_and_copy(size_type __n
,
1057 _ForwardIterator __first
, _ForwardIterator __last
)
1059 pointer __result
= this->_M_allocate(__n
);
1062 std::__uninitialized_copy_a(__first
, __last
, __result
,
1063 _M_get_Tp_allocator());
1068 _M_deallocate(__result
, __n
);
1069 __throw_exception_again
;
1074 // Internal constructor functions follow.
1076 // Called by the range constructor to implement [23.1.1]/9
1078 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1079 // 438. Ambiguity in the "do the right thing" clause
1080 template<typename _Integer
>
1082 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
1084 this->_M_impl
._M_start
= _M_allocate(static_cast<size_type
>(__n
));
1085 this->_M_impl
._M_end_of_storage
=
1086 this->_M_impl
._M_start
+ static_cast<size_type
>(__n
);
1087 _M_fill_initialize(static_cast<size_type
>(__n
), __value
);
1090 // Called by the range constructor to implement [23.1.1]/9
1091 template<typename _InputIterator
>
1093 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1096 typedef typename
std::iterator_traits
<_InputIterator
>::
1097 iterator_category _IterCategory
;
1098 _M_range_initialize(__first
, __last
, _IterCategory());
1101 // Called by the second initialize_dispatch above
1102 template<typename _InputIterator
>
1104 _M_range_initialize(_InputIterator __first
,
1105 _InputIterator __last
, std::input_iterator_tag
)
1107 for (; __first
!= __last
; ++__first
)
1108 push_back(*__first
);
1111 // Called by the second initialize_dispatch above
1112 template<typename _ForwardIterator
>
1114 _M_range_initialize(_ForwardIterator __first
,
1115 _ForwardIterator __last
, std::forward_iterator_tag
)
1117 const size_type __n
= std::distance(__first
, __last
);
1118 this->_M_impl
._M_start
= this->_M_allocate(__n
);
1119 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
1120 this->_M_impl
._M_finish
=
1121 std::__uninitialized_copy_a(__first
, __last
,
1122 this->_M_impl
._M_start
,
1123 _M_get_Tp_allocator());
1126 // Called by the first initialize_dispatch above and by the
1127 // vector(n,value,a) constructor.
1129 _M_fill_initialize(size_type __n
, const value_type
& __value
)
1131 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
1132 _M_get_Tp_allocator());
1133 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1136 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1137 // Called by the vector(n) constructor.
1139 _M_default_initialize(size_type __n
)
1141 std::__uninitialized_default_n_a(this->_M_impl
._M_start
, __n
,
1142 _M_get_Tp_allocator());
1143 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
1147 // Internal assign functions follow. The *_aux functions do the actual
1148 // assignment work for the range versions.
1150 // Called by the range assign to implement [23.1.1]/9
1152 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1153 // 438. Ambiguity in the "do the right thing" clause
1154 template<typename _Integer
>
1156 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1157 { _M_fill_assign(__n
, __val
); }
1159 // Called by the range assign to implement [23.1.1]/9
1160 template<typename _InputIterator
>
1162 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1165 typedef typename
std::iterator_traits
<_InputIterator
>::
1166 iterator_category _IterCategory
;
1167 _M_assign_aux(__first
, __last
, _IterCategory());
1170 // Called by the second assign_dispatch above
1171 template<typename _InputIterator
>
1173 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1174 std::input_iterator_tag
);
1176 // Called by the second assign_dispatch above
1177 template<typename _ForwardIterator
>
1179 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1180 std::forward_iterator_tag
);
1182 // Called by assign(n,t), and the range assign when it turns out
1183 // to be the same thing.
1185 _M_fill_assign(size_type __n
, const value_type
& __val
);
1188 // Internal insert functions follow.
1190 // Called by the range insert to implement [23.1.1]/9
1192 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1193 // 438. Ambiguity in the "do the right thing" clause
1194 template<typename _Integer
>
1196 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
1198 { _M_fill_insert(__pos
, __n
, __val
); }
1200 // Called by the range insert to implement [23.1.1]/9
1201 template<typename _InputIterator
>
1203 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
1204 _InputIterator __last
, __false_type
)
1206 typedef typename
std::iterator_traits
<_InputIterator
>::
1207 iterator_category _IterCategory
;
1208 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
1211 // Called by the second insert_dispatch above
1212 template<typename _InputIterator
>
1214 _M_range_insert(iterator __pos
, _InputIterator __first
,
1215 _InputIterator __last
, std::input_iterator_tag
);
1217 // Called by the second insert_dispatch above
1218 template<typename _ForwardIterator
>
1220 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
1221 _ForwardIterator __last
, std::forward_iterator_tag
);
1223 // Called by insert(p,n,x), and the range insert when it turns out to be
1226 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1229 // Called by resize(n).
1231 _M_default_append(size_type __n
);
1237 // Called by insert(p,x)
1238 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1240 _M_insert_aux(iterator __position
, const value_type
& __x
);
1242 template<typename
... _Args
>
1244 _M_insert_aux(iterator __position
, _Args
&&... __args
);
1247 // Called by the latter.
1249 _M_check_len(size_type __n
, const char* __s
) const
1251 if (max_size() - size() < __n
)
1252 __throw_length_error(__N(__s
));
1254 const size_type __len
= size() + std::max(size(), __n
);
1255 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1258 // Internal erase functions follow.
1260 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1263 _M_erase_at_end(pointer __pos
)
1265 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
1266 this->_M_impl
._M_finish
= __pos
;
1272 * @brief Vector equality comparison.
1273 * @param x A %vector.
1274 * @param y A %vector of the same type as @a x.
1275 * @return True iff the size and elements of the vectors are equal.
1277 * This is an equivalence relation. It is linear in the size of the
1278 * vectors. Vectors are considered equivalent if their sizes are equal,
1279 * and if corresponding elements compare equal.
1281 template<typename _Tp
, typename _Alloc
>
1283 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1284 { return (__x
.size() == __y
.size()
1285 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
1288 * @brief Vector ordering relation.
1289 * @param x A %vector.
1290 * @param y A %vector of the same type as @a x.
1291 * @return True iff @a x is lexicographically less than @a y.
1293 * This is a total ordering relation. It is linear in the size of the
1294 * vectors. The elements must be comparable with @c <.
1296 * See std::lexicographical_compare() for how the determination is made.
1298 template<typename _Tp
, typename _Alloc
>
1300 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1301 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1302 __y
.begin(), __y
.end()); }
1304 /// Based on operator==
1305 template<typename _Tp
, typename _Alloc
>
1307 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1308 { return !(__x
== __y
); }
1310 /// Based on operator<
1311 template<typename _Tp
, typename _Alloc
>
1313 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1314 { return __y
< __x
; }
1316 /// Based on operator<
1317 template<typename _Tp
, typename _Alloc
>
1319 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1320 { return !(__y
< __x
); }
1322 /// Based on operator<
1323 template<typename _Tp
, typename _Alloc
>
1325 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
1326 { return !(__x
< __y
); }
1328 /// See std::vector::swap().
1329 template<typename _Tp
, typename _Alloc
>
1331 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
1334 _GLIBCXX_END_NAMESPACE_CONTAINER
1337 #endif /* _STL_VECTOR_H */