1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_vector.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/stl_iterator_base_funcs.h>
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
68 namespace _GLIBCXX_STD
72 * See bits/stl_deque.h's _Deque_base for an explanation.
75 template<typename _Tp
, typename _Alloc
>
78 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
81 : public _Tp_alloc_type
85 _Tp
* _M_end_of_storage
;
86 _Vector_impl(_Tp_alloc_type
const& __a
)
87 : _Tp_alloc_type(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92 typedef _Alloc allocator_type
;
95 _M_get_Tp_allocator() const
96 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
100 { return _M_get_Tp_allocator(); }
102 _Vector_base(const allocator_type
& __a
)
106 _Vector_base(size_t __n
, const allocator_type
& __a
)
109 this->_M_impl
._M_start
= this->_M_allocate(__n
);
110 this->_M_impl
._M_finish
= this->_M_impl
._M_start
;
111 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
115 { _M_deallocate(this->_M_impl
._M_start
, this->_M_impl
._M_end_of_storage
116 - this->_M_impl
._M_start
); }
119 _Vector_impl _M_impl
;
122 _M_allocate(size_t __n
)
123 { return _M_impl
.allocate(__n
); }
126 _M_deallocate(_Tp
* __p
, size_t __n
)
129 _M_impl
.deallocate(__p
, __n
);
135 * @brief A standard container which offers fixed time access to
136 * individual elements in any order.
138 * @ingroup Containers
141 * Meets the requirements of a <a href="tables.html#65">container</a>, a
142 * <a href="tables.html#66">reversible container</a>, and a
143 * <a href="tables.html#67">sequence</a>, including the
144 * <a href="tables.html#68">optional sequence requirements</a> with the
145 * %exception of @c push_front and @c pop_front.
147 * In some terminology a %vector can be described as a dynamic
148 * C-style array, it offers fast and efficient access to individual
149 * elements in any order and saves the user from worrying about
150 * memory and size allocation. Subscripting ( @c [] ) access is
151 * also provided as with C-style arrays.
153 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
154 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
156 // Concept requirements.
157 typedef typename
_Alloc::value_type _Alloc_value_type
;
158 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
159 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
161 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
162 typedef vector
<_Tp
, _Alloc
> vector_type
;
163 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
166 typedef _Tp value_type
;
167 typedef typename
_Tp_alloc_type::pointer pointer
;
168 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
169 typedef typename
_Tp_alloc_type::reference reference
;
170 typedef typename
_Tp_alloc_type::const_reference const_reference
;
171 typedef __gnu_cxx::__normal_iterator
<pointer
, vector_type
> iterator
;
172 typedef __gnu_cxx::__normal_iterator
<const_pointer
, vector_type
>
174 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
175 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
176 typedef size_t size_type
;
177 typedef ptrdiff_t difference_type
;
178 typedef _Alloc allocator_type
;
182 * These two functions and three data members are all from the
183 * base class. They should be pretty self-explanatory, as
184 * %vector uses a simple contiguous allocation scheme. @endif
186 using _Base::_M_allocate
;
187 using _Base::_M_deallocate
;
188 using _Base::_M_impl
;
189 using _Base::_M_get_Tp_allocator
;
192 // [23.2.4.1] construct/copy/destroy
193 // (assign() and get_allocator() are also listed in this section)
195 * @brief Default constructor creates no elements.
198 vector(const allocator_type
& __a
= allocator_type())
203 * @brief Create a %vector with copies of an exemplar element.
204 * @param n The number of elements to initially create.
205 * @param value An element to copy.
207 * This constructor fills the %vector with @a n copies of @a value.
210 vector(size_type __n
, const value_type
& __value
= value_type(),
211 const allocator_type
& __a
= allocator_type())
214 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
215 _M_get_Tp_allocator());
216 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ __n
;
220 * @brief %Vector copy constructor.
221 * @param x A %vector of identical element and allocator types.
223 * The newly-created %vector uses a copy of the allocation
224 * object used by @a x. All the elements of @a x are copied,
225 * but any extra memory in
226 * @a x (for fast expansion) will not be copied.
228 vector(const vector
& __x
)
229 : _Base(__x
.size(), __x
.get_allocator())
230 { this->_M_impl
._M_finish
=
231 std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
232 this->_M_impl
._M_start
,
233 _M_get_Tp_allocator());
237 * @brief Builds a %vector from a range.
238 * @param first An input iterator.
239 * @param last An input iterator.
241 * Create a %vector consisting of copies of the elements from
244 * If the iterators are forward, bidirectional, or
245 * random-access, then this will call the elements' copy
246 * constructor N times (where N is distance(first,last)) and do
247 * no memory reallocation. But if only input iterators are
248 * used, then this will do at most 2N calls to the copy
249 * constructor, and logN memory reallocations.
251 template<typename _InputIterator
>
252 vector(_InputIterator __first
, _InputIterator __last
,
253 const allocator_type
& __a
= allocator_type())
256 // Check whether it's an integral type. If so, it's not an iterator.
257 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
258 _M_initialize_dispatch(__first
, __last
, _Integral());
262 * The dtor only erases the elements, and note that if the
263 * elements themselves are pointers, the pointed-to memory is
264 * not touched in any way. Managing the pointer is the user's
268 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
,
269 _M_get_Tp_allocator()); }
272 * @brief %Vector assignment operator.
273 * @param x A %vector of identical element and allocator types.
275 * All the elements of @a x are copied, but any extra memory in
276 * @a x (for fast expansion) will not be copied. Unlike the
277 * copy constructor, the allocator object is not copied.
280 operator=(const vector
& __x
);
283 * @brief Assigns a given value to a %vector.
284 * @param n Number of elements to be assigned.
285 * @param val Value to be assigned.
287 * This function fills a %vector with @a n copies of the given
288 * value. Note that the assignment completely changes the
289 * %vector and that the resulting %vector's size is the same as
290 * the number of elements assigned. Old data may be lost.
293 assign(size_type __n
, const value_type
& __val
)
294 { _M_fill_assign(__n
, __val
); }
297 * @brief Assigns a range to a %vector.
298 * @param first An input iterator.
299 * @param last An input iterator.
301 * This function fills a %vector with copies of the elements in the
302 * range [first,last).
304 * Note that the assignment completely changes the %vector and
305 * that the resulting %vector's size is the same as the number
306 * of elements assigned. Old data may be lost.
308 template<typename _InputIterator
>
310 assign(_InputIterator __first
, _InputIterator __last
)
312 // Check whether it's an integral type. If so, it's not an iterator.
313 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
314 _M_assign_dispatch(__first
, __last
, _Integral());
317 /// Get a copy of the memory allocation object.
318 using _Base::get_allocator
;
322 * Returns a read/write iterator that points to the first
323 * element in the %vector. Iteration is done in ordinary
328 { return iterator(this->_M_impl
._M_start
); }
331 * Returns a read-only (constant) iterator that points to the
332 * first element in the %vector. Iteration is done in ordinary
337 { return const_iterator(this->_M_impl
._M_start
); }
340 * Returns a read/write iterator that points one past the last
341 * element in the %vector. Iteration is done in ordinary
346 { return iterator(this->_M_impl
._M_finish
); }
349 * Returns a read-only (constant) iterator that points one past
350 * the last element in the %vector. Iteration is done in
351 * ordinary element order.
355 { return const_iterator(this->_M_impl
._M_finish
); }
358 * Returns a read/write reverse iterator that points to the
359 * last element in the %vector. Iteration is done in reverse
364 { return reverse_iterator(end()); }
367 * Returns a read-only (constant) reverse iterator that points
368 * to the last element in the %vector. Iteration is done in
369 * reverse element order.
371 const_reverse_iterator
373 { return const_reverse_iterator(end()); }
376 * Returns a read/write reverse iterator that points to one
377 * before the first element in the %vector. Iteration is done
378 * in reverse element order.
382 { return reverse_iterator(begin()); }
385 * Returns a read-only (constant) reverse iterator that points
386 * to one before the first element in the %vector. Iteration
387 * is done in reverse element order.
389 const_reverse_iterator
391 { return const_reverse_iterator(begin()); }
393 // [23.2.4.2] capacity
394 /** Returns the number of elements in the %vector. */
397 { return size_type(this->_M_impl
._M_finish
- this->_M_impl
._M_start
); }
399 /** Returns the size() of the largest possible %vector. */
402 { return size_type(-1) / sizeof(value_type
); }
405 * @brief Resizes the %vector to the specified number of elements.
406 * @param new_size Number of elements the %vector should contain.
407 * @param x Data with which new elements should be populated.
409 * This function will %resize the %vector to the specified
410 * number of elements. If the number is smaller than the
411 * %vector's current size the %vector is truncated, otherwise
412 * the %vector is extended and new elements are populated with
416 resize(size_type __new_size
, value_type __x
= value_type())
418 if (__new_size
< size())
419 _M_erase_at_end(this->_M_impl
._M_start
+ __new_size
);
421 insert(end(), __new_size
- size(), __x
);
425 * Returns the total number of elements that the %vector can
426 * hold before needing to allocate more memory.
430 { return size_type(this->_M_impl
._M_end_of_storage
431 - this->_M_impl
._M_start
); }
434 * Returns true if the %vector is empty. (Thus begin() would
439 { return begin() == end(); }
442 * @brief Attempt to preallocate enough memory for specified number of
444 * @param n Number of elements required.
445 * @throw std::length_error If @a n exceeds @c max_size().
447 * This function attempts to reserve enough memory for the
448 * %vector to hold the specified number of elements. If the
449 * number requested is more than max_size(), length_error is
452 * The advantage of this function is that if optimal code is a
453 * necessity and the user can determine the number of elements
454 * that will be required, the user can reserve the memory in
455 * %advance, and thus prevent a possible reallocation of memory
456 * and copying of %vector data.
459 reserve(size_type __n
);
463 * @brief Subscript access to the data contained in the %vector.
464 * @param n The index of the element for which data should be
466 * @return Read/write reference to data.
468 * This operator allows for easy, array-style, data access.
469 * Note that data access with this operator is unchecked and
470 * out_of_range lookups are not defined. (For checked lookups
474 operator[](size_type __n
)
475 { return *(this->_M_impl
._M_start
+ __n
); }
478 * @brief Subscript access to the data contained in the %vector.
479 * @param n The index of the element for which data should be
481 * @return Read-only (constant) reference to data.
483 * This operator allows for easy, array-style, data access.
484 * Note that data access with this operator is unchecked and
485 * out_of_range lookups are not defined. (For checked lookups
489 operator[](size_type __n
) const
490 { return *(this->_M_impl
._M_start
+ __n
); }
493 /// @if maint Safety check used only from at(). @endif
495 _M_range_check(size_type __n
) const
497 if (__n
>= this->size())
498 __throw_out_of_range(__N("vector::_M_range_check"));
503 * @brief Provides access to the data contained in the %vector.
504 * @param n The index of the element for which data should be
506 * @return Read/write reference to data.
507 * @throw std::out_of_range If @a n is an invalid index.
509 * This function provides for safer data access. The parameter
510 * is first checked that it is in the range of the vector. The
511 * function throws out_of_range if the check fails.
521 * @brief Provides access to the data contained in the %vector.
522 * @param n The index of the element for which data should be
524 * @return Read-only (constant) reference to data.
525 * @throw std::out_of_range If @a n is an invalid index.
527 * This function provides for safer data access. The parameter
528 * is first checked that it is in the range of the vector. The
529 * function throws out_of_range if the check fails.
532 at(size_type __n
) const
539 * Returns a read/write reference to the data at the first
540 * element of the %vector.
547 * Returns a read-only (constant) reference to the data at the first
548 * element of the %vector.
555 * Returns a read/write reference to the data at the last
556 * element of the %vector.
560 { return *(end() - 1); }
563 * Returns a read-only (constant) reference to the data at the
564 * last element of the %vector.
568 { return *(end() - 1); }
570 // _GLIBCXX_RESOLVE_LIB_DEFECTS
571 // DR 464. Suggestion for new member functions in standard containers.
574 * Returns a pointer such that [data(), data() + size()) is a valid
575 * range. For a non-empty %vector, data() == &front().
579 { return pointer(this->_M_impl
._M_start
); }
583 { return const_pointer(this->_M_impl
._M_start
); }
585 // [23.2.4.3] modifiers
587 * @brief Add data to the end of the %vector.
588 * @param x Data to be added.
590 * This is a typical stack operation. The function creates an
591 * element at the end of the %vector and assigns the given data
592 * to it. Due to the nature of a %vector this operation can be
593 * done in constant time if the %vector has preallocated space
597 push_back(const value_type
& __x
)
599 if (this->_M_impl
._M_finish
!= this->_M_impl
._M_end_of_storage
)
601 this->_M_impl
.construct(this->_M_impl
._M_finish
, __x
);
602 ++this->_M_impl
._M_finish
;
605 _M_insert_aux(end(), __x
);
609 * @brief Removes last element.
611 * This is a typical stack operation. It shrinks the %vector by one.
613 * Note that no data is returned, and if the last element's
614 * data is needed, it should be retrieved before pop_back() is
620 --this->_M_impl
._M_finish
;
621 this->_M_impl
.destroy(this->_M_impl
._M_finish
);
625 * @brief Inserts given value into %vector before specified iterator.
626 * @param position An iterator into the %vector.
627 * @param x Data to be inserted.
628 * @return An iterator that points to the inserted data.
630 * This function will insert a copy of the given value before
631 * the specified location. Note that this kind of operation
632 * could be expensive for a %vector and if it is frequently
633 * used the user should consider using std::list.
636 insert(iterator __position
, const value_type
& __x
);
639 * @brief Inserts a number of copies of given data into the %vector.
640 * @param position An iterator into the %vector.
641 * @param n Number of elements to be inserted.
642 * @param x Data to be inserted.
644 * This function will insert a specified number of copies of
645 * the given data before the location specified by @a position.
647 * Note that this kind of operation could be expensive for a
648 * %vector and if it is frequently used the user should
649 * consider using std::list.
652 insert(iterator __position
, size_type __n
, const value_type
& __x
)
653 { _M_fill_insert(__position
, __n
, __x
); }
656 * @brief Inserts a range into the %vector.
657 * @param position An iterator into the %vector.
658 * @param first An input iterator.
659 * @param last An input iterator.
661 * This function will insert copies of the data in the range
662 * [first,last) into the %vector before the location specified
665 * Note that this kind of operation could be expensive for a
666 * %vector and if it is frequently used the user should
667 * consider using std::list.
669 template<typename _InputIterator
>
671 insert(iterator __position
, _InputIterator __first
,
672 _InputIterator __last
)
674 // Check whether it's an integral type. If so, it's not an iterator.
675 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
676 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
680 * @brief Remove element at given position.
681 * @param position Iterator pointing to element to be erased.
682 * @return An iterator pointing to the next element (or end()).
684 * This function will erase the element at the given position and thus
685 * shorten the %vector by one.
687 * Note This operation could be expensive and if it is
688 * frequently used the user should consider using std::list.
689 * The user is also cautioned that this function only erases
690 * the element, and that if the element is itself a pointer,
691 * the pointed-to memory is not touched in any way. Managing
692 * the pointer is the user's responsibilty.
695 erase(iterator __position
);
698 * @brief Remove a range of elements.
699 * @param first Iterator pointing to the first element to be erased.
700 * @param last Iterator pointing to one past the last element to be
702 * @return An iterator pointing to the element pointed to by @a last
703 * prior to erasing (or end()).
705 * This function will erase the elements in the range [first,last) and
706 * shorten the %vector accordingly.
708 * Note This operation could be expensive and if it is
709 * frequently used the user should consider using std::list.
710 * The user is also cautioned that this function only erases
711 * the elements, and that if the elements themselves are
712 * pointers, the pointed-to memory is not touched in any way.
713 * Managing the pointer is the user's responsibilty.
716 erase(iterator __first
, iterator __last
);
719 * @brief Swaps data with another %vector.
720 * @param x A %vector of the same element and allocator types.
722 * This exchanges the elements between two vectors in constant time.
723 * (Three pointers, so it should be quite fast.)
724 * Note that the global std::swap() function is specialized such that
725 * std::swap(v1,v2) will feed to this function.
730 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
731 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
732 std::swap(this->_M_impl
._M_end_of_storage
,
733 __x
._M_impl
._M_end_of_storage
);
737 * Erases all the elements. Note that this function only erases the
738 * elements, and that if the elements themselves are pointers, the
739 * pointed-to memory is not touched in any way. Managing the pointer is
740 * the user's responsibilty.
744 { _M_erase_at_end(this->_M_impl
._M_start
); }
749 * Memory expansion handler. Uses the member allocation function to
750 * obtain @a n bytes of memory, and then copies [first,last) into it.
753 template<typename _ForwardIterator
>
755 _M_allocate_and_copy(size_type __n
,
756 _ForwardIterator __first
, _ForwardIterator __last
)
758 pointer __result
= this->_M_allocate(__n
);
761 std::__uninitialized_copy_a(__first
, __last
, __result
,
762 _M_get_Tp_allocator());
767 _M_deallocate(__result
, __n
);
768 __throw_exception_again
;
773 // Internal constructor functions follow.
775 // Called by the range constructor to implement [23.1.1]/9
776 template<typename _Integer
>
778 _M_initialize_dispatch(_Integer __n
, _Integer __value
, __true_type
)
780 this->_M_impl
._M_start
= _M_allocate(__n
);
781 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
782 std::__uninitialized_fill_n_a(this->_M_impl
._M_start
, __n
, __value
,
783 _M_get_Tp_allocator());
784 this->_M_impl
._M_finish
= this->_M_impl
._M_end_of_storage
;
787 // Called by the range constructor to implement [23.1.1]/9
788 template<typename _InputIterator
>
790 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
793 typedef typename
std::iterator_traits
<_InputIterator
>::
794 iterator_category _IterCategory
;
795 _M_range_initialize(__first
, __last
, _IterCategory());
798 // Called by the second initialize_dispatch above
799 template<typename _InputIterator
>
801 _M_range_initialize(_InputIterator __first
,
802 _InputIterator __last
, std::input_iterator_tag
)
804 for (; __first
!= __last
; ++__first
)
808 // Called by the second initialize_dispatch above
809 template<typename _ForwardIterator
>
811 _M_range_initialize(_ForwardIterator __first
,
812 _ForwardIterator __last
, std::forward_iterator_tag
)
814 const size_type __n
= std::distance(__first
, __last
);
815 this->_M_impl
._M_start
= this->_M_allocate(__n
);
816 this->_M_impl
._M_end_of_storage
= this->_M_impl
._M_start
+ __n
;
817 this->_M_impl
._M_finish
=
818 std::__uninitialized_copy_a(__first
, __last
,
819 this->_M_impl
._M_start
,
820 _M_get_Tp_allocator());
824 // Internal assign functions follow. The *_aux functions do the actual
825 // assignment work for the range versions.
827 // Called by the range assign to implement [23.1.1]/9
828 template<typename _Integer
>
830 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
832 _M_fill_assign(static_cast<size_type
>(__n
),
833 static_cast<value_type
>(__val
));
836 // Called by the range assign to implement [23.1.1]/9
837 template<typename _InputIterator
>
839 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
842 typedef typename
std::iterator_traits
<_InputIterator
>::
843 iterator_category _IterCategory
;
844 _M_assign_aux(__first
, __last
, _IterCategory());
847 // Called by the second assign_dispatch above
848 template<typename _InputIterator
>
850 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
851 std::input_iterator_tag
);
853 // Called by the second assign_dispatch above
854 template<typename _ForwardIterator
>
856 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
857 std::forward_iterator_tag
);
859 // Called by assign(n,t), and the range assign when it turns out
860 // to be the same thing.
862 _M_fill_assign(size_type __n
, const value_type
& __val
);
865 // Internal insert functions follow.
867 // Called by the range insert to implement [23.1.1]/9
868 template<typename _Integer
>
870 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
873 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
874 static_cast<value_type
>(__val
));
877 // Called by the range insert to implement [23.1.1]/9
878 template<typename _InputIterator
>
880 _M_insert_dispatch(iterator __pos
, _InputIterator __first
,
881 _InputIterator __last
, __false_type
)
883 typedef typename
std::iterator_traits
<_InputIterator
>::
884 iterator_category _IterCategory
;
885 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
888 // Called by the second insert_dispatch above
889 template<typename _InputIterator
>
891 _M_range_insert(iterator __pos
, _InputIterator __first
,
892 _InputIterator __last
, std::input_iterator_tag
);
894 // Called by the second insert_dispatch above
895 template<typename _ForwardIterator
>
897 _M_range_insert(iterator __pos
, _ForwardIterator __first
,
898 _ForwardIterator __last
, std::forward_iterator_tag
);
900 // Called by insert(p,n,x), and the range insert when it turns out to be
903 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
905 // Called by insert(p,x)
907 _M_insert_aux(iterator __position
, const value_type
& __x
);
909 // Internal erase functions follow.
911 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
914 _M_erase_at_end(pointer __pos
)
916 std::_Destroy(__pos
, this->_M_impl
._M_finish
, _M_get_Tp_allocator());
917 this->_M_impl
._M_finish
= __pos
;
923 * @brief Vector equality comparison.
924 * @param x A %vector.
925 * @param y A %vector of the same type as @a x.
926 * @return True iff the size and elements of the vectors are equal.
928 * This is an equivalence relation. It is linear in the size of the
929 * vectors. Vectors are considered equivalent if their sizes are equal,
930 * and if corresponding elements compare equal.
932 template<typename _Tp
, typename _Alloc
>
934 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
935 { return (__x
.size() == __y
.size()
936 && std::equal(__x
.begin(), __x
.end(), __y
.begin())); }
939 * @brief Vector ordering relation.
940 * @param x A %vector.
941 * @param y A %vector of the same type as @a x.
942 * @return True iff @a x is lexicographically less than @a y.
944 * This is a total ordering relation. It is linear in the size of the
945 * vectors. The elements must be comparable with @c <.
947 * See std::lexicographical_compare() for how the determination is made.
949 template<typename _Tp
, typename _Alloc
>
951 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
952 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
953 __y
.begin(), __y
.end()); }
955 /// Based on operator==
956 template<typename _Tp
, typename _Alloc
>
958 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
959 { return !(__x
== __y
); }
961 /// Based on operator<
962 template<typename _Tp
, typename _Alloc
>
964 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
965 { return __y
< __x
; }
967 /// Based on operator<
968 template<typename _Tp
, typename _Alloc
>
970 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
971 { return !(__y
< __x
); }
973 /// Based on operator<
974 template<typename _Tp
, typename _Alloc
>
976 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
977 { return !(__x
< __y
); }
979 /// See std::vector::swap().
980 template<typename _Tp
, typename _Alloc
>
982 swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
986 #endif /* _VECTOR_H */