# stl_vector.h

Go to the documentation of this file.

    00001 // Vector implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 3, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /*
    00027  *
    00028  * Copyright (c) 1994
    00029  * Hewlett-Packard Company
    00030  *
    00031  * Permission to use, copy, modify, distribute and sell this software
    00032  * and its documentation for any purpose is hereby granted without fee,
    00033  * provided that the above copyright notice appear in all copies and
    00034  * that both that copyright notice and this permission notice appear
    00035  * in supporting documentation.  Hewlett-Packard Company makes no
    00036  * representations about the suitability of this software for any
    00037  * purpose.  It is provided "as is" without express or implied warranty.
    00038  *
    00039  *
    00040  * Copyright (c) 1996
    00041  * Silicon Graphics Computer Systems, Inc.
    00042  *
    00043  * Permission to use, copy, modify, distribute and sell this software
    00044  * and its documentation for any purpose is hereby granted without fee,
    00045  * provided that the above copyright notice appear in all copies and
    00046  * that both that copyright notice and this permission notice appear
    00047  * in supporting documentation.  Silicon Graphics makes no
    00048  * representations about the suitability of this  software for any
    00049  * purpose.  It is provided "as is" without express or implied warranty.
    00050  */
    00051
    00052 /** @file stl_vector.h
    00053  *  This is an internal header file, included by other library headers.
    00054  *  You should not attempt to use it directly.
    00055  */
    00056
    00057 #ifndef _STL_VECTOR_H
    00058 #define _STL_VECTOR_H 1
    00059
    00060 #include <bits/stl_iterator_base_funcs.h>
    00061 #include <bits/functexcept.h>
    00062 #include <bits/concept_check.h>
    00063 #include <initializer_list>
    00064
    00065 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
    00066
    00067   /// See bits/stl_deque.h's _Deque_base for an explanation.
    00068   template<typename _Tp, typename _Alloc>
    00069     struct _Vector_base
    00070     {
    00071       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
    00072
    00073       struct _Vector_impl
    00074       : public _Tp_alloc_type
    00075       {
    00076     typename _Tp_alloc_type::pointer _M_start;
    00077     typename _Tp_alloc_type::pointer _M_finish;
    00078     typename _Tp_alloc_type::pointer _M_end_of_storage;
    00079
    00080     _Vector_impl()
    00081     : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
    00082     { }
    00083
    00084     _Vector_impl(_Tp_alloc_type const& __a)
    00085     : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
    00086     { }
    00087       };
    00088
    00089     public:
    00090       typedef _Alloc allocator_type;
    00091
    00092       _Tp_alloc_type&
    00093       _M_get_Tp_allocator()
    00094       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
    00095
    00096       const _Tp_alloc_type&
    00097       _M_get_Tp_allocator() const
    00098       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
    00099
    00100       allocator_type
    00101       get_allocator() const
    00102       { return allocator_type(_M_get_Tp_allocator()); }
    00103
    00104       _Vector_base()
    00105       : _M_impl() { }
    00106
    00107       _Vector_base(const allocator_type& __a)
    00108       : _M_impl(__a) { }
    00109
    00110       _Vector_base(size_t __n, const allocator_type& __a)
    00111       : _M_impl(__a)
    00112       {
    00113     this->_M_impl._M_start = this->_M_allocate(__n);
    00114     this->_M_impl._M_finish = this->_M_impl._M_start;
    00115     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
    00116       }
    00117
    00118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00119       _Vector_base(_Vector_base&& __x)
    00120       : _M_impl(__x._M_get_Tp_allocator())
    00121       {
    00122     this->_M_impl._M_start = __x._M_impl._M_start;
    00123     this->_M_impl._M_finish = __x._M_impl._M_finish;
    00124     this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
    00125     __x._M_impl._M_start = 0;
    00126     __x._M_impl._M_finish = 0;
    00127     __x._M_impl._M_end_of_storage = 0;
    00128       }
    00129 #endif
    00130
    00131       ~_Vector_base()
    00132       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
    00133               - this->_M_impl._M_start); }
    00134
    00135     public:
    00136       _Vector_impl _M_impl;
    00137
    00138       typename _Tp_alloc_type::pointer
    00139       _M_allocate(size_t __n)
    00140       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
    00141
    00142       void
    00143       _M_deallocate(typename _Tp_alloc_type::pointer __p, size_t __n)
    00144       {
    00145     if (__p)
    00146       _M_impl.deallocate(__p, __n);
    00147       }
    00148     };
    00149
    00150
    00151   /**
    00152    *  @brief A standard container which offers fixed time access to
    00153    *  individual elements in any order.
    00154    *
    00155    *  @ingroup sequences
    00156    *
    00157    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
    00158    *  <a href="tables.html#66">reversible container</a>, and a
    00159    *  <a href="tables.html#67">sequence</a>, including the
    00160    *  <a href="tables.html#68">optional sequence requirements</a> with the
    00161    *  %exception of @c push_front and @c pop_front.
    00162    *
    00163    *  In some terminology a %vector can be described as a dynamic
    00164    *  C-style array, it offers fast and efficient access to individual
    00165    *  elements in any order and saves the user from worrying about
    00166    *  memory and size allocation.  Subscripting ( @c [] ) access is
    00167    *  also provided as with C-style arrays.
    00168   */
    00169   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
    00170     class vector : protected _Vector_base<_Tp, _Alloc>
    00171     {
    00172       // Concept requirements.
    00173       typedef typename _Alloc::value_type                _Alloc_value_type;
    00174       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
    00175       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
    00176
    00177       typedef _Vector_base<_Tp, _Alloc>          _Base;
    00178       typedef typename _Base::_Tp_alloc_type         _Tp_alloc_type;
    00179
    00180     public:
    00181       typedef _Tp                    value_type;
    00182       typedef typename _Tp_alloc_type::pointer           pointer;
    00183       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
    00184       typedef typename _Tp_alloc_type::reference         reference;
    00185       typedef typename _Tp_alloc_type::const_reference   const_reference;
    00186       typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
    00187       typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
    00188       const_iterator;
    00189       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
    00190       typedef std::reverse_iterator<iterator>        reverse_iterator;
    00191       typedef size_t                     size_type;
    00192       typedef ptrdiff_t                  difference_type;
    00193       typedef _Alloc                                 allocator_type;
    00194
    00195     protected:
    00196       using _Base::_M_allocate;
    00197       using _Base::_M_deallocate;
    00198       using _Base::_M_impl;
    00199       using _Base::_M_get_Tp_allocator;
    00200
    00201     public:
    00202       // [23.2.4.1] construct/copy/destroy
    00203       // (assign() and get_allocator() are also listed in this section)
    00204       /**
    00205        *  @brief  Default constructor creates no elements.
    00206        */
    00207       vector()
    00208       : _Base() { }
    00209
    00210       /**
    00211        *  @brief  Creates a %vector with no elements.
    00212        *  @param  a  An allocator object.
    00213        */
    00214       explicit
    00215       vector(const allocator_type& __a)
    00216       : _Base(__a) { }
    00217
    00218       /**
    00219        *  @brief  Creates a %vector with copies of an exemplar element.
    00220        *  @param  n  The number of elements to initially create.
    00221        *  @param  value  An element to copy.
    00222        *  @param  a  An allocator.
    00223        *
    00224        *  This constructor fills the %vector with @a n copies of @a value.
    00225        */
    00226       explicit
    00227       vector(size_type __n, const value_type& __value = value_type(),
    00228          const allocator_type& __a = allocator_type())
    00229       : _Base(__n, __a)
    00230       { _M_fill_initialize(__n, __value); }
    00231
    00232       /**
    00233        *  @brief  %Vector copy constructor.
    00234        *  @param  x  A %vector of identical element and allocator types.
    00235        *
    00236        *  The newly-created %vector uses a copy of the allocation
    00237        *  object used by @a x.  All the elements of @a x are copied,
    00238        *  but any extra memory in
    00239        *  @a x (for fast expansion) will not be copied.
    00240        */
    00241       vector(const vector& __x)
    00242       : _Base(__x.size(), __x._M_get_Tp_allocator())
    00243       { this->_M_impl._M_finish =
    00244       std::__uninitialized_copy_a(__x.begin(), __x.end(),
    00245                       this->_M_impl._M_start,
    00246                       _M_get_Tp_allocator());
    00247       }
    00248
    00249 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00250       /**
    00251        *  @brief  %Vector move constructor.
    00252        *  @param  x  A %vector of identical element and allocator types.
    00253        *
    00254        *  The newly-created %vector contains the exact contents of @a x.
    00255        *  The contents of @a x are a valid, but unspecified %vector.
    00256        */
    00257       vector(vector&& __x)
    00258       : _Base(std::forward<_Base>(__x)) { }
    00259
    00260       /**
    00261        *  @brief  Builds a %vector from an initializer list.
    00262        *  @param  l  An initializer_list.
    00263        *  @param  a  An allocator.
    00264        *
    00265        *  Create a %vector consisting of copies of the elements in the
    00266        *  initializer_list @a l.
    00267        *
    00268        *  This will call the element type's copy constructor N times
    00269        *  (where N is @a l.size()) and do no memory reallocation.
    00270        */
    00271       vector(initializer_list<value_type> __l,
    00272          const allocator_type& __a = allocator_type())
    00273       : _Base(__a)
    00274       {
    00275     _M_range_initialize(__l.begin(), __l.end(),
    00276                 random_access_iterator_tag());
    00277       }
    00278 #endif
    00279
    00280       /**
    00281        *  @brief  Builds a %vector from a range.
    00282        *  @param  first  An input iterator.
    00283        *  @param  last  An input iterator.
    00284        *  @param  a  An allocator.
    00285        *
    00286        *  Create a %vector consisting of copies of the elements from
    00287        *  [first,last).
    00288        *
    00289        *  If the iterators are forward, bidirectional, or
    00290        *  random-access, then this will call the elements' copy
    00291        *  constructor N times (where N is distance(first,last)) and do
    00292        *  no memory reallocation.  But if only input iterators are
    00293        *  used, then this will do at most 2N calls to the copy
    00294        *  constructor, and logN memory reallocations.
    00295        */
    00296       template<typename _InputIterator>
    00297         vector(_InputIterator __first, _InputIterator __last,
    00298            const allocator_type& __a = allocator_type())
    00299     : _Base(__a)
    00300         {
    00301       // Check whether it's an integral type.  If so, it's not an iterator.
    00302       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
    00303       _M_initialize_dispatch(__first, __last, _Integral());
    00304     }
    00305
    00306       /**
    00307        *  The dtor only erases the elements, and note that if the
    00308        *  elements themselves are pointers, the pointed-to memory is
    00309        *  not touched in any way.  Managing the pointer is the user's
    00310        *  responsibility.
    00311        */
    00312       ~vector()
    00313       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
    00314               _M_get_Tp_allocator()); }
    00315
    00316       /**
    00317        *  @brief  %Vector assignment operator.
    00318        *  @param  x  A %vector of identical element and allocator types.
    00319        *
    00320        *  All the elements of @a x are copied, but any extra memory in
    00321        *  @a x (for fast expansion) will not be copied.  Unlike the
    00322        *  copy constructor, the allocator object is not copied.
    00323        */
    00324       vector&
    00325       operator=(const vector& __x);
    00326
    00327 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00328       /**
    00329        *  @brief  %Vector move assignment operator.
    00330        *  @param  x  A %vector of identical element and allocator types.
    00331        *
    00332        *  The contents of @a x are moved into this %vector (without copying).
    00333        *  @a x is a valid, but unspecified %vector.
    00334        */
    00335       vector&
    00336       operator=(vector&& __x)
    00337       {
    00338     // NB: DR 1204.
    00339     // NB: DR 675.
    00340     this->clear();
    00341     this->swap(__x);
    00342     return *this;
    00343       }
    00344
    00345       /**
    00346        *  @brief  %Vector list assignment operator.
    00347        *  @param  l  An initializer_list.
    00348        *
    00349        *  This function fills a %vector with copies of the elements in the
    00350        *  initializer list @a l.
    00351        *
    00352        *  Note that the assignment completely changes the %vector and
    00353        *  that the resulting %vector's size is the same as the number
    00354        *  of elements assigned.  Old data may be lost.
    00355        */
    00356       vector&
    00357       operator=(initializer_list<value_type> __l)
    00358       {
    00359     this->assign(__l.begin(), __l.end());
    00360     return *this;
    00361       }
    00362 #endif
    00363
    00364       /**
    00365        *  @brief  Assigns a given value to a %vector.
    00366        *  @param  n  Number of elements to be assigned.
    00367        *  @param  val  Value to be assigned.
    00368        *
    00369        *  This function fills a %vector with @a n copies of the given
    00370        *  value.  Note that the assignment completely changes the
    00371        *  %vector and that the resulting %vector's size is the same as
    00372        *  the number of elements assigned.  Old data may be lost.
    00373        */
    00374       void
    00375       assign(size_type __n, const value_type& __val)
    00376       { _M_fill_assign(__n, __val); }
    00377
    00378       /**
    00379        *  @brief  Assigns a range to a %vector.
    00380        *  @param  first  An input iterator.
    00381        *  @param  last   An input iterator.
    00382        *
    00383        *  This function fills a %vector with copies of the elements in the
    00384        *  range [first,last).
    00385        *
    00386        *  Note that the assignment completely changes the %vector and
    00387        *  that the resulting %vector's size is the same as the number
    00388        *  of elements assigned.  Old data may be lost.
    00389        */
    00390       template<typename _InputIterator>
    00391         void
    00392         assign(_InputIterator __first, _InputIterator __last)
    00393         {
    00394       // Check whether it's an integral type.  If so, it's not an iterator.
    00395       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
    00396       _M_assign_dispatch(__first, __last, _Integral());
    00397     }
    00398
    00399 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00400       /**
    00401        *  @brief  Assigns an initializer list to a %vector.
    00402        *  @param  l  An initializer_list.
    00403        *
    00404        *  This function fills a %vector with copies of the elements in the
    00405        *  initializer list @a l.
    00406        *
    00407        *  Note that the assignment completely changes the %vector and
    00408        *  that the resulting %vector's size is the same as the number
    00409        *  of elements assigned.  Old data may be lost.
    00410        */
    00411       void
    00412       assign(initializer_list<value_type> __l)
    00413       { this->assign(__l.begin(), __l.end()); }
    00414 #endif
    00415
    00416       /// Get a copy of the memory allocation object.
    00417       using _Base::get_allocator;
    00418
    00419       // iterators
    00420       /**
    00421        *  Returns a read/write iterator that points to the first
    00422        *  element in the %vector.  Iteration is done in ordinary
    00423        *  element order.
    00424        */
    00425       iterator
    00426       begin()
    00427       { return iterator(this->_M_impl._M_start); }
    00428
    00429       /**
    00430        *  Returns a read-only (constant) iterator that points to the
    00431        *  first element in the %vector.  Iteration is done in ordinary
    00432        *  element order.
    00433        */
    00434       const_iterator
    00435       begin() const
    00436       { return const_iterator(this->_M_impl._M_start); }
    00437
    00438       /**
    00439        *  Returns a read/write iterator that points one past the last
    00440        *  element in the %vector.  Iteration is done in ordinary
    00441        *  element order.
    00442        */
    00443       iterator
    00444       end()
    00445       { return iterator(this->_M_impl._M_finish); }
    00446
    00447       /**
    00448        *  Returns a read-only (constant) iterator that points one past
    00449        *  the last element in the %vector.  Iteration is done in
    00450        *  ordinary element order.
    00451        */
    00452       const_iterator
    00453       end() const
    00454       { return const_iterator(this->_M_impl._M_finish); }
    00455
    00456       /**
    00457        *  Returns a read/write reverse iterator that points to the
    00458        *  last element in the %vector.  Iteration is done in reverse
    00459        *  element order.
    00460        */
    00461       reverse_iterator
    00462       rbegin()
    00463       { return reverse_iterator(end()); }
    00464
    00465       /**
    00466        *  Returns a read-only (constant) reverse iterator that points
    00467        *  to the last element in the %vector.  Iteration is done in
    00468        *  reverse element order.
    00469        */
    00470       const_reverse_iterator
    00471       rbegin() const
    00472       { return const_reverse_iterator(end()); }
    00473
    00474       /**
    00475        *  Returns a read/write reverse iterator that points to one
    00476        *  before the first element in the %vector.  Iteration is done
    00477        *  in reverse element order.
    00478        */
    00479       reverse_iterator
    00480       rend()
    00481       { return reverse_iterator(begin()); }
    00482
    00483       /**
    00484        *  Returns a read-only (constant) reverse iterator that points
    00485        *  to one before the first element in the %vector.  Iteration
    00486        *  is done in reverse element order.
    00487        */
    00488       const_reverse_iterator
    00489       rend() const
    00490       { return const_reverse_iterator(begin()); }
    00491
    00492 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00493       /**
    00494        *  Returns a read-only (constant) iterator that points to the
    00495        *  first element in the %vector.  Iteration is done in ordinary
    00496        *  element order.
    00497        */
    00498       const_iterator
    00499       cbegin() const
    00500       { return const_iterator(this->_M_impl._M_start); }
    00501
    00502       /**
    00503        *  Returns a read-only (constant) iterator that points one past
    00504        *  the last element in the %vector.  Iteration is done in
    00505        *  ordinary element order.
    00506        */
    00507       const_iterator
    00508       cend() const
    00509       { return const_iterator(this->_M_impl._M_finish); }
    00510
    00511       /**
    00512        *  Returns a read-only (constant) reverse iterator that points
    00513        *  to the last element in the %vector.  Iteration is done in
    00514        *  reverse element order.
    00515        */
    00516       const_reverse_iterator
    00517       crbegin() const
    00518       { return const_reverse_iterator(end()); }
    00519
    00520       /**
    00521        *  Returns a read-only (constant) reverse iterator that points
    00522        *  to one before the first element in the %vector.  Iteration
    00523        *  is done in reverse element order.
    00524        */
    00525       const_reverse_iterator
    00526       crend() const
    00527       { return const_reverse_iterator(begin()); }
    00528 #endif
    00529
    00530       // [23.2.4.2] capacity
    00531       /**  Returns the number of elements in the %vector.  */
    00532       size_type
    00533       size() const
    00534       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
    00535
    00536       /**  Returns the size() of the largest possible %vector.  */
    00537       size_type
    00538       max_size() const
    00539       { return _M_get_Tp_allocator().max_size(); }
    00540
    00541       /**
    00542        *  @brief  Resizes the %vector to the specified number of elements.
    00543        *  @param  new_size  Number of elements the %vector should contain.
    00544        *  @param  x  Data with which new elements should be populated.
    00545        *
    00546        *  This function will %resize the %vector to the specified
    00547        *  number of elements.  If the number is smaller than the
    00548        *  %vector's current size the %vector is truncated, otherwise
    00549        *  the %vector is extended and new elements are populated with
    00550        *  given data.
    00551        */
    00552       void
    00553       resize(size_type __new_size, value_type __x = value_type())
    00554       {
    00555     if (__new_size < size())
    00556       _M_erase_at_end(this->_M_impl._M_start + __new_size);
    00557     else
    00558       insert(end(), __new_size - size(), __x);
    00559       }
    00560
    00561 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00562       /**  A non-binding request to reduce capacity() to size().  */
    00563       void
    00564       shrink_to_fit()
    00565       { std::__shrink_to_fit<vector>::_S_do_it(*this); }
    00566 #endif
    00567
    00568       /**
    00569        *  Returns the total number of elements that the %vector can
    00570        *  hold before needing to allocate more memory.
    00571        */
    00572       size_type
    00573       capacity() const
    00574       { return size_type(this->_M_impl._M_end_of_storage
    00575              - this->_M_impl._M_start); }
    00576
    00577       /**
    00578        *  Returns true if the %vector is empty.  (Thus begin() would
    00579        *  equal end().)
    00580        */
    00581       bool
    00582       empty() const
    00583       { return begin() == end(); }
    00584
    00585       /**
    00586        *  @brief  Attempt to preallocate enough memory for specified number of
    00587        *          elements.
    00588        *  @param  n  Number of elements required.
    00589        *  @throw  std::length_error  If @a n exceeds @c max_size().
    00590        *
    00591        *  This function attempts to reserve enough memory for the
    00592        *  %vector to hold the specified number of elements.  If the
    00593        *  number requested is more than max_size(), length_error is
    00594        *  thrown.
    00595        *
    00596        *  The advantage of this function is that if optimal code is a
    00597        *  necessity and the user can determine the number of elements
    00598        *  that will be required, the user can reserve the memory in
    00599        *  %advance, and thus prevent a possible reallocation of memory
    00600        *  and copying of %vector data.
    00601        */
    00602       void
    00603       reserve(size_type __n);
    00604
    00605       // element access
    00606       /**
    00607        *  @brief  Subscript access to the data contained in the %vector.
    00608        *  @param n The index of the element for which data should be
    00609        *  accessed.
    00610        *  @return  Read/write reference to data.
    00611        *
    00612        *  This operator allows for easy, array-style, data access.
    00613        *  Note that data access with this operator is unchecked and
    00614        *  out_of_range lookups are not defined. (For checked lookups
    00615        *  see at().)
    00616        */
    00617       reference
    00618       operator[](size_type __n)
    00619       { return *(this->_M_impl._M_start + __n); }
    00620
    00621       /**
    00622        *  @brief  Subscript access to the data contained in the %vector.
    00623        *  @param n The index of the element for which data should be
    00624        *  accessed.
    00625        *  @return  Read-only (constant) reference to data.
    00626        *
    00627        *  This operator allows for easy, array-style, data access.
    00628        *  Note that data access with this operator is unchecked and
    00629        *  out_of_range lookups are not defined. (For checked lookups
    00630        *  see at().)
    00631        */
    00632       const_reference
    00633       operator[](size_type __n) const
    00634       { return *(this->_M_impl._M_start + __n); }
    00635
    00636     protected:
    00637       /// Safety check used only from at().
    00638       void
    00639       _M_range_check(size_type __n) const
    00640       {
    00641     if (__n >= this->size())
    00642       __throw_out_of_range(__N("vector::_M_range_check"));
    00643       }
    00644
    00645     public:
    00646       /**
    00647        *  @brief  Provides access to the data contained in the %vector.
    00648        *  @param n The index of the element for which data should be
    00649        *  accessed.
    00650        *  @return  Read/write reference to data.
    00651        *  @throw  std::out_of_range  If @a n is an invalid index.
    00652        *
    00653        *  This function provides for safer data access.  The parameter
    00654        *  is first checked that it is in the range of the vector.  The
    00655        *  function throws out_of_range if the check fails.
    00656        */
    00657       reference
    00658       at(size_type __n)
    00659       {
    00660     _M_range_check(__n);
    00661     return (*this)[__n];
    00662       }
    00663
    00664       /**
    00665        *  @brief  Provides access to the data contained in the %vector.
    00666        *  @param n The index of the element for which data should be
    00667        *  accessed.
    00668        *  @return  Read-only (constant) reference to data.
    00669        *  @throw  std::out_of_range  If @a n is an invalid index.
    00670        *
    00671        *  This function provides for safer data access.  The parameter
    00672        *  is first checked that it is in the range of the vector.  The
    00673        *  function throws out_of_range if the check fails.
    00674        */
    00675       const_reference
    00676       at(size_type __n) const
    00677       {
    00678     _M_range_check(__n);
    00679     return (*this)[__n];
    00680       }
    00681
    00682       /**
    00683        *  Returns a read/write reference to the data at the first
    00684        *  element of the %vector.
    00685        */
    00686       reference
    00687       front()
    00688       { return *begin(); }
    00689
    00690       /**
    00691        *  Returns a read-only (constant) reference to the data at the first
    00692        *  element of the %vector.
    00693        */
    00694       const_reference
    00695       front() const
    00696       { return *begin(); }
    00697
    00698       /**
    00699        *  Returns a read/write reference to the data at the last
    00700        *  element of the %vector.
    00701        */
    00702       reference
    00703       back()
    00704       { return *(end() - 1); }
    00705
    00706       /**
    00707        *  Returns a read-only (constant) reference to the data at the
    00708        *  last element of the %vector.
    00709        */
    00710       const_reference
    00711       back() const
    00712       { return *(end() - 1); }
    00713
    00714       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00715       // DR 464. Suggestion for new member functions in standard containers.
    00716       // data access
    00717       /**
    00718        *   Returns a pointer such that [data(), data() + size()) is a valid
    00719        *   range.  For a non-empty %vector, data() == &front().
    00720        */
    00721       pointer
    00722       data()
    00723       { return pointer(this->_M_impl._M_start); }
    00724
    00725       const_pointer
    00726       data() const
    00727       { return const_pointer(this->_M_impl._M_start); }
    00728
    00729       // [23.2.4.3] modifiers
    00730       /**
    00731        *  @brief  Add data to the end of the %vector.
    00732        *  @param  x  Data to be added.
    00733        *
    00734        *  This is a typical stack operation.  The function creates an
    00735        *  element at the end of the %vector and assigns the given data
    00736        *  to it.  Due to the nature of a %vector this operation can be
    00737        *  done in constant time if the %vector has preallocated space
    00738        *  available.
    00739        */
    00740       void
    00741       push_back(const value_type& __x)
    00742       {
    00743     if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
    00744       {
    00745         this->_M_impl.construct(this->_M_impl._M_finish, __x);
    00746         ++this->_M_impl._M_finish;
    00747       }
    00748     else
    00749       _M_insert_aux(end(), __x);
    00750       }
    00751
    00752 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00753       void
    00754       push_back(value_type&& __x)
    00755       { emplace_back(std::move(__x)); }
    00756
    00757       template<typename... _Args>
    00758         void
    00759         emplace_back(_Args&&... __args);
    00760 #endif
    00761
    00762       /**
    00763        *  @brief  Removes last element.
    00764        *
    00765        *  This is a typical stack operation. It shrinks the %vector by one.
    00766        *
    00767        *  Note that no data is returned, and if the last element's
    00768        *  data is needed, it should be retrieved before pop_back() is
    00769        *  called.
    00770        */
    00771       void
    00772       pop_back()
    00773       {
    00774     --this->_M_impl._M_finish;
    00775     this->_M_impl.destroy(this->_M_impl._M_finish);
    00776       }
    00777
    00778 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00779       /**
    00780        *  @brief  Inserts an object in %vector before specified iterator.
    00781        *  @param  position  An iterator into the %vector.
    00782        *  @param  args  Arguments.
    00783        *  @return  An iterator that points to the inserted data.
    00784        *
    00785        *  This function will insert an object of type T constructed
    00786        *  with T(std::forward<Args>(args)...) before the specified location.
    00787        *  Note that this kind of operation could be expensive for a %vector
    00788        *  and if it is frequently used the user should consider using
    00789        *  std::list.
    00790        */
    00791       template<typename... _Args>
    00792         iterator
    00793         emplace(iterator __position, _Args&&... __args);
    00794 #endif
    00795
    00796       /**
    00797        *  @brief  Inserts given value into %vector before specified iterator.
    00798        *  @param  position  An iterator into the %vector.
    00799        *  @param  x  Data to be inserted.
    00800        *  @return  An iterator that points to the inserted data.
    00801        *
    00802        *  This function will insert a copy of the given value before
    00803        *  the specified location.  Note that this kind of operation
    00804        *  could be expensive for a %vector and if it is frequently
    00805        *  used the user should consider using std::list.
    00806        */
    00807       iterator
    00808       insert(iterator __position, const value_type& __x);
    00809
    00810 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00811       /**
    00812        *  @brief  Inserts given rvalue into %vector before specified iterator.
    00813        *  @param  position  An iterator into the %vector.
    00814        *  @param  x  Data to be inserted.
    00815        *  @return  An iterator that points to the inserted data.
    00816        *
    00817        *  This function will insert a copy of the given rvalue before
    00818        *  the specified location.  Note that this kind of operation
    00819        *  could be expensive for a %vector and if it is frequently
    00820        *  used the user should consider using std::list.
    00821        */
    00822       iterator
    00823       insert(iterator __position, value_type&& __x)
    00824       { return emplace(__position, std::move(__x)); }
    00825
    00826       /**
    00827        *  @brief  Inserts an initializer_list into the %vector.
    00828        *  @param  position  An iterator into the %vector.
    00829        *  @param  l  An initializer_list.
    00830        *
    00831        *  This function will insert copies of the data in the
    00832        *  initializer_list @a l into the %vector before the location
    00833        *  specified by @a position.
    00834        *
    00835        *  Note that this kind of operation could be expensive for a
    00836        *  %vector and if it is frequently used the user should
    00837        *  consider using std::list.
    00838        */
    00839       void
    00840       insert(iterator __position, initializer_list<value_type> __l)
    00841       { this->insert(__position, __l.begin(), __l.end()); }
    00842 #endif
    00843
    00844       /**
    00845        *  @brief  Inserts a number of copies of given data into the %vector.
    00846        *  @param  position  An iterator into the %vector.
    00847        *  @param  n  Number of elements to be inserted.
    00848        *  @param  x  Data to be inserted.
    00849        *
    00850        *  This function will insert a specified number of copies of
    00851        *  the given data before the location specified by @a position.
    00852        *
    00853        *  Note that this kind of operation could be expensive for a
    00854        *  %vector and if it is frequently used the user should
    00855        *  consider using std::list.
    00856        */
    00857       void
    00858       insert(iterator __position, size_type __n, const value_type& __x)
    00859       { _M_fill_insert(__position, __n, __x); }
    00860
    00861       /**
    00862        *  @brief  Inserts a range into the %vector.
    00863        *  @param  position  An iterator into the %vector.
    00864        *  @param  first  An input iterator.
    00865        *  @param  last   An input iterator.
    00866        *
    00867        *  This function will insert copies of the data in the range
    00868        *  [first,last) into the %vector before the location specified
    00869        *  by @a pos.
    00870        *
    00871        *  Note that this kind of operation could be expensive for a
    00872        *  %vector and if it is frequently used the user should
    00873        *  consider using std::list.
    00874        */
    00875       template<typename _InputIterator>
    00876         void
    00877         insert(iterator __position, _InputIterator __first,
    00878            _InputIterator __last)
    00879         {
    00880       // Check whether it's an integral type.  If so, it's not an iterator.
    00881       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
    00882       _M_insert_dispatch(__position, __first, __last, _Integral());
    00883     }
    00884
    00885       /**
    00886        *  @brief  Remove element at given position.
    00887        *  @param  position  Iterator pointing to element to be erased.
    00888        *  @return  An iterator pointing to the next element (or end()).
    00889        *
    00890        *  This function will erase the element at the given position and thus
    00891        *  shorten the %vector by one.
    00892        *
    00893        *  Note This operation could be expensive and if it is
    00894        *  frequently used the user should consider using std::list.
    00895        *  The user is also cautioned that this function only erases
    00896        *  the element, and that if the element is itself a pointer,
    00897        *  the pointed-to memory is not touched in any way.  Managing
    00898        *  the pointer is the user's responsibility.
    00899        */
    00900       iterator
    00901       erase(iterator __position);
    00902
    00903       /**
    00904        *  @brief  Remove a range of elements.
    00905        *  @param  first  Iterator pointing to the first element to be erased.
    00906        *  @param  last  Iterator pointing to one past the last element to be
    00907        *                erased.
    00908        *  @return  An iterator pointing to the element pointed to by @a last
    00909        *           prior to erasing (or end()).
    00910        *
    00911        *  This function will erase the elements in the range [first,last) and
    00912        *  shorten the %vector accordingly.
    00913        *
    00914        *  Note This operation could be expensive and if it is
    00915        *  frequently used the user should consider using std::list.
    00916        *  The user is also cautioned that this function only erases
    00917        *  the elements, and that if the elements themselves are
    00918        *  pointers, the pointed-to memory is not touched in any way.
    00919        *  Managing the pointer is the user's responsibility.
    00920        */
    00921       iterator
    00922       erase(iterator __first, iterator __last);
    00923
    00924       /**
    00925        *  @brief  Swaps data with another %vector.
    00926        *  @param  x  A %vector of the same element and allocator types.
    00927        *
    00928        *  This exchanges the elements between two vectors in constant time.
    00929        *  (Three pointers, so it should be quite fast.)
    00930        *  Note that the global std::swap() function is specialized such that
    00931        *  std::swap(v1,v2) will feed to this function.
    00932        */
    00933       void
    00934       swap(vector& __x)
    00935       {
    00936     std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
    00937     std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
    00938     std::swap(this->_M_impl._M_end_of_storage,
    00939           __x._M_impl._M_end_of_storage);
    00940
    00941     // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00942     // 431. Swapping containers with unequal allocators.
    00943     std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
    00944                             __x._M_get_Tp_allocator());
    00945       }
    00946
    00947       /**
    00948        *  Erases all the elements.  Note that this function only erases the
    00949        *  elements, and that if the elements themselves are pointers, the
    00950        *  pointed-to memory is not touched in any way.  Managing the pointer is
    00951        *  the user's responsibility.
    00952        */
    00953       void
    00954       clear()
    00955       { _M_erase_at_end(this->_M_impl._M_start); }
    00956
    00957     protected:
    00958       /**
    00959        *  Memory expansion handler.  Uses the member allocation function to
    00960        *  obtain @a n bytes of memory, and then copies [first,last) into it.
    00961        */
    00962       template<typename _ForwardIterator>
    00963         pointer
    00964         _M_allocate_and_copy(size_type __n,
    00965                  _ForwardIterator __first, _ForwardIterator __last)
    00966         {
    00967       pointer __result = this->_M_allocate(__n);
    00968       __try
    00969         {
    00970           std::__uninitialized_copy_a(__first, __last, __result,
    00971                       _M_get_Tp_allocator());
    00972           return __result;
    00973         }
    00974       __catch(...)
    00975         {
    00976           _M_deallocate(__result, __n);
    00977           __throw_exception_again;
    00978         }
    00979     }
    00980
    00981
    00982       // Internal constructor functions follow.
    00983
    00984       // Called by the range constructor to implement [23.1.1]/9
    00985
    00986       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00987       // 438. Ambiguity in the "do the right thing" clause
    00988       template<typename _Integer>
    00989         void
    00990         _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
    00991         {
    00992       this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
    00993       this->_M_impl._M_end_of_storage =
    00994         this->_M_impl._M_start + static_cast<size_type>(__n);
    00995       _M_fill_initialize(static_cast<size_type>(__n), __value);
    00996     }
    00997
    00998       // Called by the range constructor to implement [23.1.1]/9
    00999       template<typename _InputIterator>
    01000         void
    01001         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
    01002                    __false_type)
    01003         {
    01004       typedef typename std::iterator_traits<_InputIterator>::
    01005         iterator_category _IterCategory;
    01006       _M_range_initialize(__first, __last, _IterCategory());
    01007     }
    01008
    01009       // Called by the second initialize_dispatch above
    01010       template<typename _InputIterator>
    01011         void
    01012         _M_range_initialize(_InputIterator __first,
    01013                 _InputIterator __last, std::input_iterator_tag)
    01014         {
    01015       for (; __first != __last; ++__first)
    01016         push_back(*__first);
    01017     }
    01018
    01019       // Called by the second initialize_dispatch above
    01020       template<typename _ForwardIterator>
    01021         void
    01022         _M_range_initialize(_ForwardIterator __first,
    01023                 _ForwardIterator __last, std::forward_iterator_tag)
    01024         {
    01025       const size_type __n = std::distance(__first, __last);
    01026       this->_M_impl._M_start = this->_M_allocate(__n);
    01027       this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
    01028       this->_M_impl._M_finish =
    01029         std::__uninitialized_copy_a(__first, __last,
    01030                     this->_M_impl._M_start,
    01031                     _M_get_Tp_allocator());
    01032     }
    01033
    01034       // Called by the first initialize_dispatch above and by the
    01035       // vector(n,value,a) constructor.
    01036       void
    01037       _M_fill_initialize(size_type __n, const value_type& __value)
    01038       {
    01039     std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
    01040                       _M_get_Tp_allocator());
    01041     this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
    01042       }
    01043
    01044
    01045       // Internal assign functions follow.  The *_aux functions do the actual
    01046       // assignment work for the range versions.
    01047
    01048       // Called by the range assign to implement [23.1.1]/9
    01049
    01050       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01051       // 438. Ambiguity in the "do the right thing" clause
    01052       template<typename _Integer>
    01053         void
    01054         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
    01055         { _M_fill_assign(__n, __val); }
    01056
    01057       // Called by the range assign to implement [23.1.1]/9
    01058       template<typename _InputIterator>
    01059         void
    01060         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
    01061                __false_type)
    01062         {
    01063       typedef typename std::iterator_traits<_InputIterator>::
    01064         iterator_category _IterCategory;
    01065       _M_assign_aux(__first, __last, _IterCategory());
    01066     }
    01067
    01068       // Called by the second assign_dispatch above
    01069       template<typename _InputIterator>
    01070         void
    01071         _M_assign_aux(_InputIterator __first, _InputIterator __last,
    01072               std::input_iterator_tag);
    01073
    01074       // Called by the second assign_dispatch above
    01075       template<typename _ForwardIterator>
    01076         void
    01077         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
    01078               std::forward_iterator_tag);
    01079
    01080       // Called by assign(n,t), and the range assign when it turns out
    01081       // to be the same thing.
    01082       void
    01083       _M_fill_assign(size_type __n, const value_type& __val);
    01084
    01085
    01086       // Internal insert functions follow.
    01087
    01088       // Called by the range insert to implement [23.1.1]/9
    01089
    01090       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01091       // 438. Ambiguity in the "do the right thing" clause
    01092       template<typename _Integer>
    01093         void
    01094         _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
    01095                __true_type)
    01096         { _M_fill_insert(__pos, __n, __val); }
    01097
    01098       // Called by the range insert to implement [23.1.1]/9
    01099       template<typename _InputIterator>
    01100         void
    01101         _M_insert_dispatch(iterator __pos, _InputIterator __first,
    01102                _InputIterator __last, __false_type)
    01103         {
    01104       typedef typename std::iterator_traits<_InputIterator>::
    01105         iterator_category _IterCategory;
    01106       _M_range_insert(__pos, __first, __last, _IterCategory());
    01107     }
    01108
    01109       // Called by the second insert_dispatch above
    01110       template<typename _InputIterator>
    01111         void
    01112         _M_range_insert(iterator __pos, _InputIterator __first,
    01113             _InputIterator __last, std::input_iterator_tag);
    01114
    01115       // Called by the second insert_dispatch above
    01116       template<typename _ForwardIterator>
    01117         void
    01118         _M_range_insert(iterator __pos, _ForwardIterator __first,
    01119             _ForwardIterator __last, std::forward_iterator_tag);
    01120
    01121       // Called by insert(p,n,x), and the range insert when it turns out to be
    01122       // the same thing.
    01123       void
    01124       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
    01125
    01126       // Called by insert(p,x)
    01127 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    01128       void
    01129       _M_insert_aux(iterator __position, const value_type& __x);
    01130 #else
    01131       template<typename... _Args>
    01132         void
    01133         _M_insert_aux(iterator __position, _Args&&... __args);
    01134 #endif
    01135
    01136       // Called by the latter.
    01137       size_type
    01138       _M_check_len(size_type __n, const char* __s) const
    01139       {
    01140     if (max_size() - size() < __n)
    01141       __throw_length_error(__N(__s));
    01142
    01143     const size_type __len = size() + std::max(size(), __n);
    01144     return (__len < size() || __len > max_size()) ? max_size() : __len;
    01145       }
    01146
    01147       // Internal erase functions follow.
    01148
    01149       // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
    01150       // _M_assign_aux.
    01151       void
    01152       _M_erase_at_end(pointer __pos)
    01153       {
    01154     std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
    01155     this->_M_impl._M_finish = __pos;
    01156       }
    01157     };
    01158
    01159
    01160   /**
    01161    *  @brief  Vector equality comparison.
    01162    *  @param  x  A %vector.
    01163    *  @param  y  A %vector of the same type as @a x.
    01164    *  @return  True iff the size and elements of the vectors are equal.
    01165    *
    01166    *  This is an equivalence relation.  It is linear in the size of the
    01167    *  vectors.  Vectors are considered equivalent if their sizes are equal,
    01168    *  and if corresponding elements compare equal.
    01169   */
    01170   template<typename _Tp, typename _Alloc>
    01171     inline bool
    01172     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01173     { return (__x.size() == __y.size()
    01174           && std::equal(__x.begin(), __x.end(), __y.begin())); }
    01175
    01176   /**
    01177    *  @brief  Vector ordering relation.
    01178    *  @param  x  A %vector.
    01179    *  @param  y  A %vector of the same type as @a x.
    01180    *  @return  True iff @a x is lexicographically less than @a y.
    01181    *
    01182    *  This is a total ordering relation.  It is linear in the size of the
    01183    *  vectors.  The elements must be comparable with @c <.
    01184    *
    01185    *  See std::lexicographical_compare() for how the determination is made.
    01186   */
    01187   template<typename _Tp, typename _Alloc>
    01188     inline bool
    01189     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01190     { return std::lexicographical_compare(__x.begin(), __x.end(),
    01191                       __y.begin(), __y.end()); }
    01192
    01193   /// Based on operator==
    01194   template<typename _Tp, typename _Alloc>
    01195     inline bool
    01196     operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01197     { return !(__x == __y); }
    01198
    01199   /// Based on operator<
    01200   template<typename _Tp, typename _Alloc>
    01201     inline bool
    01202     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01203     { return __y < __x; }
    01204
    01205   /// Based on operator<
    01206   template<typename _Tp, typename _Alloc>
    01207     inline bool
    01208     operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01209     { return !(__y < __x); }
    01210
    01211   /// Based on operator<
    01212   template<typename _Tp, typename _Alloc>
    01213     inline bool
    01214     operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
    01215     { return !(__x < __y); }
    01216
    01217   /// See std::vector::swap().
    01218   template<typename _Tp, typename _Alloc>
    01219     inline void
    01220     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
    01221     { __x.swap(__y); }
    01222
    01223 _GLIBCXX_END_NESTED_NAMESPACE
    01224
    01225 #endif /* _STL_VECTOR_H */