STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
stl_vector.h File Reference
#include <bits/stl_iterator_base_funcs.h>
#include <bits/functexcept.h>
#include <bits/concept_check.h>

Go to the source code of this file.

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {vector}

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

See bits/stl_deque.h's _Deque_base for an explanation.

A standard container which offers fixed time access to individual elements in any order.

Template Parameters
_TpType of element.
_AllocAllocator type, defaults to allocator<_Tp>.

Meets the requirements of a container, a reversible container, and a sequence, including the optional sequence requirements with the exception of push_front and pop_front.

In some terminology a vector can be described as a dynamic C-style array, it offers fast and efficient access to individual elements in any order and saves the user from worrying about memory and size allocation. Subscripting ( [] ) access is also provided as with C-style arrays.

Default constructor creates no elements.

Creates a vector with no elements.

Parameters
__aAn allocator object.

Creates a vector with copies of an exemplar element.

Parameters
__nThe number of elements to initially create.
__valueAn element to copy.
__aAn allocator.

This constructor fills the vector with __n copies of __value.

Vector copy constructor.

Parameters
__xA vector of identical element and allocator types.

The newly-created vector uses a copy of the allocation object used by __x. All the elements of __x are copied, but any extra memory in __x (for fast expansion) will not be copied.

Builds a vector from a range.

Parameters
__firstAn input iterator.
__lastAn input iterator.
__aAn allocator.

Create a vector consisting of copies of the elements from [first,last).

If the iterators are forward, bidirectional, or random-access, then this will call the elements' copy constructor N times (where N is distance(first,last)) and do no memory reallocation. But if only input iterators are used, then this will do at most 2N calls to the copy constructor, and logN memory reallocations.

The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Vector assignment operator.

Parameters
__xA vector of identical element and allocator types.

All the elements of __x are copied, but any extra memory in __x (for fast expansion) will not be copied. Unlike the copy constructor, the allocator object is not copied.

Assigns a given value to a vector.

Parameters
__nNumber of elements to be assigned.
__valValue to be assigned.

This function fills a vector with __n copies of the given value. Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. Old data may be lost.

Assigns a range to a vector.

Parameters
__firstAn input iterator.
__lastAn input iterator.

This function fills a vector with copies of the elements in the range [__first,__last).

Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. Old data may be lost.

Get a copy of the memory allocation object.

Returns a read/write iterator that points to the first element in the vector. Iteration is done in ordinary element order.

Returns a read-only (constant) iterator that points to the first element in the vector. Iteration is done in ordinary element order.

Returns a read/write iterator that points one past the last element in the vector. Iteration is done in ordinary element order.

Returns a read-only (constant) iterator that points one past the last element in the vector. Iteration is done in ordinary element order.

Returns a read/write reverse iterator that points to the last element in the vector. Iteration is done in reverse element order.

Returns a read-only (constant) reverse iterator that points to the last element in the vector. Iteration is done in reverse element order.

Returns a read/write reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order.

Returns a read-only (constant) reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order.

Returns the number of elements in the vector.

Returns the size() of the largest possible vector.

Resizes the vector to the specified number of elements.

Parameters
__new_sizeNumber of elements the vector should contain.
__xData with which new elements should be populated.

This function will resize the vector to the specified number of elements. If the number is smaller than the vector's current size the vector is truncated, otherwise the vector is extended and new elements are populated with given data.

Returns the total number of elements that the vector can hold before needing to allocate more memory.

Returns true if the vector is empty. (Thus begin() would equal end().)

Attempt to preallocate enough memory for specified number of elements.

Parameters
__nNumber of elements required.
Exceptions
std::length_errorIf n exceeds max_size().

This function attempts to reserve enough memory for the vector to hold the specified number of elements. If the number requested is more than max_size(), length_error is thrown.

The advantage of this function is that if optimal code is a necessity and the user can determine the number of elements that will be required, the user can reserve the memory in advance, and thus prevent a possible reallocation of memory and copying of vector data.

Subscript access to the data contained in the vector.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read/write reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Subscript access to the data contained in the vector.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read-only (constant) reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Safety check used only from at().

Provides access to the data contained in the vector.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read/write reference to data.
Exceptions
std::out_of_rangeIf __n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails.

Provides access to the data contained in the vector.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read-only (constant) reference to data.
Exceptions
std::out_of_rangeIf __n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails.

Returns a read/write reference to the data at the first element of the vector.

Returns a read-only (constant) reference to the data at the first element of the vector.

Returns a read/write reference to the data at the last element of the vector.

Returns a read-only (constant) reference to the data at the last element of the vector.

Returns a pointer such that [data(), data() + size()) is a valid range. For a non-empty vector, data() == &front().

Add data to the end of the vector.

Parameters
__xData to be added.

This is a typical stack operation. The function creates an element at the end of the vector and assigns the given data to it. Due to the nature of a vector this operation can be done in constant time if the vector has preallocated space available.

Removes last element.

This is a typical stack operation. It shrinks the vector by one.

Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called.

Inserts given value into vector before specified iterator.

Parameters
__positionAn iterator into the vector.
__xData to be inserted.
Returns
An iterator that points to the inserted data.

This function will insert a copy of the given value before the specified location. Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Inserts a number of copies of given data into the vector.

Parameters
__positionAn iterator into the vector.
__nNumber of elements to be inserted.
__xData to be inserted.

This function will insert a specified number of copies of the given data before the location specified by position.

Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Inserts a range into the vector.

Parameters
__positionAn iterator into the vector.
__firstAn input iterator.
__lastAn input iterator.

This function will insert copies of the data in the range [__first,__last) into the vector before the location specified by pos.

Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Remove element at given position.

Parameters
__positionIterator pointing to element to be erased.
Returns
An iterator pointing to the next element (or end()).

This function will erase the element at the given position and thus shorten the vector by one.

Note This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Remove a range of elements.

Parameters
__firstIterator pointing to the first element to be erased.
__lastIterator pointing to one past the last element to be erased.
Returns
An iterator pointing to the element pointed to by __last prior to erasing (or end()).

This function will erase the elements in the range [__first,__last) and shorten the vector accordingly.

Note This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Swaps data with another vector.

Parameters
__xA vector of the same element and allocator types.

This exchanges the elements between two vectors in constant time. (Three pointers, so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(v1,v2) will feed to this function.

Erases all the elements. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Memory expansion handler. Uses the member allocation function to obtain n bytes of memory, and then copies [first,last) into it.

Vector equality comparison.

Parameters
__xA vector.
__yA vector of the same type as __x.
Returns
True iff the size and elements of the vectors are equal.

This is an equivalence relation. It is linear in the size of the vectors. Vectors are considered equivalent if their sizes are equal, and if corresponding elements compare equal.

Vector ordering relation.

Parameters
__xA vector.
__yA vector of the same type as __x.
Returns
True iff __x is lexicographically less than __y.

This is a total ordering relation. It is linear in the size of the vectors. The elements must be comparable with <.

See std::lexicographical_compare() for how the determination is made.

Based on operator==

Based on operator<

Based on operator<

Based on operator<

See std::vector::swap().

67 {
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 
71  template<typename _Tp, typename _Alloc>
72  struct _Vector_base
73  {
74  typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
75  rebind<_Tp>::other _Tp_alloc_type;
76  typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
77  pointer;
78 
79  struct _Vector_impl
80  : public _Tp_alloc_type
81  {
82  pointer _M_start;
83  pointer _M_finish;
84  pointer _M_end_of_storage;
85 
86  _Vector_impl()
87  : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
88  { }
89 
90  _Vector_impl(_Tp_alloc_type const& __a)
91  : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92  { }
93 
94 #if __cplusplus >= 201103L
95  _Vector_impl(_Tp_alloc_type&& __a)
96  : _Tp_alloc_type(std::move(__a)),
97  _M_start(0), _M_finish(0), _M_end_of_storage(0)
98  { }
99 #endif
100 
101  void _M_swap_data(_Vector_impl& __x)
102  {
103  std::swap(_M_start, __x._M_start);
104  std::swap(_M_finish, __x._M_finish);
105  std::swap(_M_end_of_storage, __x._M_end_of_storage);
106  }
107  };
108 
109  public:
110  typedef _Alloc allocator_type;
111 
112  _Tp_alloc_type&
113  _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
114  { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
115 
116  const _Tp_alloc_type&
117  _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
118  { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
119 
120  allocator_type
121  get_allocator() const _GLIBCXX_NOEXCEPT
122  { return allocator_type(_M_get_Tp_allocator()); }
123 
124  _Vector_base()
125  : _M_impl() { }
126 
127  _Vector_base(const allocator_type& __a)
128  : _M_impl(__a) { }
129 
130  _Vector_base(size_t __n)
131  : _M_impl()
132  { _M_create_storage(__n); }
133 
134  _Vector_base(size_t __n, const allocator_type& __a)
135  : _M_impl(__a)
136  { _M_create_storage(__n); }
137 
138 #if __cplusplus >= 201103L
139  _Vector_base(_Tp_alloc_type&& __a)
140  : _M_impl(std::move(__a)) { }
141 
142  _Vector_base(_Vector_base&& __x)
143  : _M_impl(std::move(__x._M_get_Tp_allocator()))
144  { this->_M_impl._M_swap_data(__x._M_impl); }
145 
146  _Vector_base(_Vector_base&& __x, const allocator_type& __a)
147  : _M_impl(__a)
148  {
149  if (__x.get_allocator() == __a)
150  this->_M_impl._M_swap_data(__x._M_impl);
151  else
152  {
153  size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
154  _M_create_storage(__n);
155  }
156  }
157 #endif
158 
159  ~_Vector_base()
160  { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
161  - this->_M_impl._M_start); }
162 
163  public:
164  _Vector_impl _M_impl;
165 
166  pointer
167  _M_allocate(size_t __n)
168  { return __n != 0 ? _M_impl.allocate(__n) : 0; }
169 
170  void
171  _M_deallocate(pointer __p, size_t __n)
172  {
173  if (__p)
174  _M_impl.deallocate(__p, __n);
175  }
176 
177  private:
178  void
179  _M_create_storage(size_t __n)
180  {
181  this->_M_impl._M_start = this->_M_allocate(__n);
182  this->_M_impl._M_finish = this->_M_impl._M_start;
183  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
184  }
185  };
186 
187 
209  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
210  class vector : protected _Vector_base<_Tp, _Alloc>
211  {
212  // Concept requirements.
213  typedef typename _Alloc::value_type _Alloc_value_type;
214  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
215  __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
216 
217  typedef _Vector_base<_Tp, _Alloc> _Base;
218  typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
219  typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
220 
221  public:
222  typedef _Tp value_type;
223  typedef typename _Base::pointer pointer;
224  typedef typename _Alloc_traits::const_pointer const_pointer;
225  typedef typename _Alloc_traits::reference reference;
226  typedef typename _Alloc_traits::const_reference const_reference;
227  typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
228  typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
229  const_iterator;
230  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
231  typedef std::reverse_iterator<iterator> reverse_iterator;
232  typedef size_t size_type;
233  typedef ptrdiff_t difference_type;
234  typedef _Alloc allocator_type;
235 
236  protected:
237  using _Base::_M_allocate;
238  using _Base::_M_deallocate;
239  using _Base::_M_impl;
240  using _Base::_M_get_Tp_allocator;
241 
242  public:
243  // [23.2.4.1] construct/copy/destroy
244  // (assign() and get_allocator() are also listed in this section)
248  vector()
249  : _Base() { }
250 
255  explicit
256  vector(const allocator_type& __a)
257  : _Base(__a) { }
258 
259 #if __cplusplus >= 201103L
260 
268  explicit
269  vector(size_type __n, const allocator_type& __a = allocator_type())
270  : _Base(__n, __a)
271  { _M_default_initialize(__n); }
272 
281  vector(size_type __n, const value_type& __value,
282  const allocator_type& __a = allocator_type())
283  : _Base(__n, __a)
284  { _M_fill_initialize(__n, __value); }
285 #else
286 
294  explicit
295  vector(size_type __n, const value_type& __value = value_type(),
296  const allocator_type& __a = allocator_type())
297  : _Base(__n, __a)
298  { _M_fill_initialize(__n, __value); }
299 #endif
300 
310  vector(const vector& __x)
311  : _Base(__x.size(),
312  _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
313  { this->_M_impl._M_finish =
314  std::__uninitialized_copy_a(__x.begin(), __x.end(),
315  this->_M_impl._M_start,
316  _M_get_Tp_allocator());
317  }
318 
319 #if __cplusplus >= 201103L
320 
327  vector(vector&& __x) noexcept
328  : _Base(std::move(__x)) { }
329 
331  vector(const vector& __x, const allocator_type& __a)
332  : _Base(__x.size(), __a)
333  { this->_M_impl._M_finish =
334  std::__uninitialized_copy_a(__x.begin(), __x.end(),
335  this->_M_impl._M_start,
336  _M_get_Tp_allocator());
337  }
338 
340  vector(vector&& __rv, const allocator_type& __m)
341  : _Base(std::move(__rv), __m)
342  {
343  if (__rv.get_allocator() != __m)
344  {
345  this->_M_impl._M_finish =
346  std::__uninitialized_move_a(__rv.begin(), __rv.end(),
347  this->_M_impl._M_start,
348  _M_get_Tp_allocator());
349  __rv.clear();
350  }
351  }
352 
364  vector(initializer_list<value_type> __l,
365  const allocator_type& __a = allocator_type())
366  : _Base(__a)
367  {
368  _M_range_initialize(__l.begin(), __l.end(),
369  random_access_iterator_tag());
370  }
371 #endif
372 
389 #if __cplusplus >= 201103L
390  template<typename _InputIterator,
391  typename = std::_RequireInputIter<_InputIterator>>
392  vector(_InputIterator __first, _InputIterator __last,
393  const allocator_type& __a = allocator_type())
394  : _Base(__a)
395  { _M_initialize_dispatch(__first, __last, __false_type()); }
396 #else
397  template<typename _InputIterator>
398  vector(_InputIterator __first, _InputIterator __last,
399  const allocator_type& __a = allocator_type())
400  : _Base(__a)
401  {
402  // Check whether it's an integral type. If so, it's not an iterator.
403  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
404  _M_initialize_dispatch(__first, __last, _Integral());
405  }
406 #endif
407 
414  ~vector() _GLIBCXX_NOEXCEPT
415  { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
416  _M_get_Tp_allocator()); }
417 
426  vector&
427  operator=(const vector& __x);
428 
429 #if __cplusplus >= 201103L
430 
438  vector&
439  operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
440  {
441  constexpr bool __move_storage =
442  _Alloc_traits::_S_propagate_on_move_assign()
443  || _Alloc_traits::_S_always_equal();
444  _M_move_assign(std::move(__x),
445  integral_constant<bool, __move_storage>());
446  return *this;
447  }
448 
460  vector&
461  operator=(initializer_list<value_type> __l)
462  {
463  this->assign(__l.begin(), __l.end());
464  return *this;
465  }
466 #endif
467 
478  void
479  assign(size_type __n, const value_type& __val)
480  { _M_fill_assign(__n, __val); }
481 
494 #if __cplusplus >= 201103L
495  template<typename _InputIterator,
496  typename = std::_RequireInputIter<_InputIterator>>
497  void
498  assign(_InputIterator __first, _InputIterator __last)
499  { _M_assign_dispatch(__first, __last, __false_type()); }
500 #else
501  template<typename _InputIterator>
502  void
503  assign(_InputIterator __first, _InputIterator __last)
504  {
505  // Check whether it's an integral type. If so, it's not an iterator.
506  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
507  _M_assign_dispatch(__first, __last, _Integral());
508  }
509 #endif
510 
511 #if __cplusplus >= 201103L
512 
523  void
524  assign(initializer_list<value_type> __l)
525  { this->assign(__l.begin(), __l.end()); }
526 #endif
527 
529  using _Base::get_allocator;
530 
531  // iterators
537  iterator
538  begin() _GLIBCXX_NOEXCEPT
539  { return iterator(this->_M_impl._M_start); }
540 
546  const_iterator
547  begin() const _GLIBCXX_NOEXCEPT
548  { return const_iterator(this->_M_impl._M_start); }
549 
555  iterator
556  end() _GLIBCXX_NOEXCEPT
557  { return iterator(this->_M_impl._M_finish); }
558 
564  const_iterator
565  end() const _GLIBCXX_NOEXCEPT
566  { return const_iterator(this->_M_impl._M_finish); }
567 
573  reverse_iterator
574  rbegin() _GLIBCXX_NOEXCEPT
575  { return reverse_iterator(end()); }
576 
582  const_reverse_iterator
583  rbegin() const _GLIBCXX_NOEXCEPT
584  { return const_reverse_iterator(end()); }
585 
591  reverse_iterator
592  rend() _GLIBCXX_NOEXCEPT
593  { return reverse_iterator(begin()); }
594 
600  const_reverse_iterator
601  rend() const _GLIBCXX_NOEXCEPT
602  { return const_reverse_iterator(begin()); }
603 
604 #if __cplusplus >= 201103L
605 
610  const_iterator
611  cbegin() const noexcept
612  { return const_iterator(this->_M_impl._M_start); }
613 
619  const_iterator
620  cend() const noexcept
621  { return const_iterator(this->_M_impl._M_finish); }
622 
628  const_reverse_iterator
629  crbegin() const noexcept
630  { return const_reverse_iterator(end()); }
631 
637  const_reverse_iterator
638  crend() const noexcept
639  { return const_reverse_iterator(begin()); }
640 #endif
641 
642  // [23.2.4.2] capacity
644  size_type
645  size() const _GLIBCXX_NOEXCEPT
646  { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
647 
649  size_type
650  max_size() const _GLIBCXX_NOEXCEPT
651  { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
652 
653 #if __cplusplus >= 201103L
654 
663  void
664  resize(size_type __new_size)
665  {
666  if (__new_size > size())
667  _M_default_append(__new_size - size());
668  else if (__new_size < size())
669  _M_erase_at_end(this->_M_impl._M_start + __new_size);
670  }
671 
683  void
684  resize(size_type __new_size, const value_type& __x)
685  {
686  if (__new_size > size())
687  insert(end(), __new_size - size(), __x);
688  else if (__new_size < size())
689  _M_erase_at_end(this->_M_impl._M_start + __new_size);
690  }
691 #else
692 
703  void
704  resize(size_type __new_size, value_type __x = value_type())
705  {
706  if (__new_size > size())
707  insert(end(), __new_size - size(), __x);
708  else if (__new_size < size())
709  _M_erase_at_end(this->_M_impl._M_start + __new_size);
710  }
711 #endif
712 
713 #if __cplusplus >= 201103L
714 
715  void
716  shrink_to_fit()
717  { _M_shrink_to_fit(); }
718 #endif
719 
724  size_type
725  capacity() const _GLIBCXX_NOEXCEPT
726  { return size_type(this->_M_impl._M_end_of_storage
727  - this->_M_impl._M_start); }
728 
733  bool
734  empty() const _GLIBCXX_NOEXCEPT
735  { return begin() == end(); }
736 
754  void
755  reserve(size_type __n);
756 
757  // element access
769  reference
770  operator[](size_type __n)
771  { return *(this->_M_impl._M_start + __n); }
772 
784  const_reference
785  operator[](size_type __n) const
786  { return *(this->_M_impl._M_start + __n); }
787 
788  protected:
790  void
791  _M_range_check(size_type __n) const
792  {
793  if (__n >= this->size())
794  __throw_out_of_range(__N("vector::_M_range_check"));
795  }
796 
797  public:
809  reference
810  at(size_type __n)
811  {
812  _M_range_check(__n);
813  return (*this)[__n];
814  }
815 
827  const_reference
828  at(size_type __n) const
829  {
830  _M_range_check(__n);
831  return (*this)[__n];
832  }
833 
838  reference
839  front()
840  { return *begin(); }
841 
846  const_reference
847  front() const
848  { return *begin(); }
849 
854  reference
855  back()
856  { return *(end() - 1); }
857 
862  const_reference
863  back() const
864  { return *(end() - 1); }
865 
866  // _GLIBCXX_RESOLVE_LIB_DEFECTS
867  // DR 464. Suggestion for new member functions in standard containers.
868  // data access
873 #if __cplusplus >= 201103L
874  _Tp*
875 #else
876  pointer
877 #endif
878  data() _GLIBCXX_NOEXCEPT
879  { return std::__addressof(front()); }
880 
881 #if __cplusplus >= 201103L
882  const _Tp*
883 #else
884  const_pointer
885 #endif
886  data() const _GLIBCXX_NOEXCEPT
887  { return std::__addressof(front()); }
888 
889  // [23.2.4.3] modifiers
900  void
901  push_back(const value_type& __x)
902  {
903  if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
904  {
905  _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
906  __x);
907  ++this->_M_impl._M_finish;
908  }
909  else
910 #if __cplusplus >= 201103L
911  _M_emplace_back_aux(__x);
912 #else
913  _M_insert_aux(end(), __x);
914 #endif
915  }
916 
917 #if __cplusplus >= 201103L
918  void
919  push_back(value_type&& __x)
920  { emplace_back(std::move(__x)); }
921 
922  template<typename... _Args>
923  void
924  emplace_back(_Args&&... __args);
925 #endif
926 
936  void
937  pop_back()
938  {
939  --this->_M_impl._M_finish;
940  _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
941  }
942 
943 #if __cplusplus >= 201103L
944 
956  template<typename... _Args>
957  iterator
958  emplace(iterator __position, _Args&&... __args);
959 #endif
960 
972  iterator
973  insert(iterator __position, const value_type& __x);
974 
975 #if __cplusplus >= 201103L
976 
987  iterator
988  insert(iterator __position, value_type&& __x)
989  { return emplace(__position, std::move(__x)); }
990 
1004  void
1005  insert(iterator __position, initializer_list<value_type> __l)
1006  { this->insert(__position, __l.begin(), __l.end()); }
1007 #endif
1008 
1022  void
1023  insert(iterator __position, size_type __n, const value_type& __x)
1024  { _M_fill_insert(__position, __n, __x); }
1025 
1040 #if __cplusplus >= 201103L
1041  template<typename _InputIterator,
1042  typename = std::_RequireInputIter<_InputIterator>>
1043  void
1044  insert(iterator __position, _InputIterator __first,
1045  _InputIterator __last)
1046  { _M_insert_dispatch(__position, __first, __last, __false_type()); }
1047 #else
1048  template<typename _InputIterator>
1049  void
1050  insert(iterator __position, _InputIterator __first,
1051  _InputIterator __last)
1052  {
1053  // Check whether it's an integral type. If so, it's not an iterator.
1054  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1055  _M_insert_dispatch(__position, __first, __last, _Integral());
1056  }
1057 #endif
1058 
1074  iterator
1075  erase(iterator __position);
1076 
1095  iterator
1096  erase(iterator __first, iterator __last);
1097 
1107  void
1108  swap(vector& __x)
1109 #if __cplusplus >= 201103L
1110  noexcept(_Alloc_traits::_S_nothrow_swap())
1111 #endif
1112  {
1113  this->_M_impl._M_swap_data(__x._M_impl);
1114  _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1115  __x._M_get_Tp_allocator());
1116  }
1117 
1124  void
1125  clear() _GLIBCXX_NOEXCEPT
1126  { _M_erase_at_end(this->_M_impl._M_start); }
1127 
1128  protected:
1133  template<typename _ForwardIterator>
1134  pointer
1135  _M_allocate_and_copy(size_type __n,
1136  _ForwardIterator __first, _ForwardIterator __last)
1137  {
1138  pointer __result = this->_M_allocate(__n);
1139  __try
1140  {
1141  std::__uninitialized_copy_a(__first, __last, __result,
1142  _M_get_Tp_allocator());
1143  return __result;
1144  }
1145  __catch(...)
1146  {
1147  _M_deallocate(__result, __n);
1149  }
1150  }
1151 
1152 
1153  // Internal constructor functions follow.
1154 
1155  // Called by the range constructor to implement [23.1.1]/9
1156 
1157  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1158  // 438. Ambiguity in the "do the right thing" clause
1159  template<typename _Integer>
1160  void
1161  _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1162  {
1163  this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1164  this->_M_impl._M_end_of_storage =
1165  this->_M_impl._M_start + static_cast<size_type>(__n);
1166  _M_fill_initialize(static_cast<size_type>(__n), __value);
1167  }
1168 
1169  // Called by the range constructor to implement [23.1.1]/9
1170  template<typename _InputIterator>
1171  void
1172  _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1173  __false_type)
1174  {
1175  typedef typename std::iterator_traits<_InputIterator>::
1176  iterator_category _IterCategory;
1177  _M_range_initialize(__first, __last, _IterCategory());
1178  }
1179 
1180  // Called by the second initialize_dispatch above
1181  template<typename _InputIterator>
1182  void
1183  _M_range_initialize(_InputIterator __first,
1184  _InputIterator __last, std::input_iterator_tag)
1185  {
1186  for (; __first != __last; ++__first)
1187 #if __cplusplus >= 201103L
1188  emplace_back(*__first);
1189 #else
1190  push_back(*__first);
1191 #endif
1192  }
1193 
1194  // Called by the second initialize_dispatch above
1195  template<typename _ForwardIterator>
1196  void
1197  _M_range_initialize(_ForwardIterator __first,
1198  _ForwardIterator __last, std::forward_iterator_tag)
1199  {
1200  const size_type __n = std::distance(__first, __last);
1201  this->_M_impl._M_start = this->_M_allocate(__n);
1202  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1203  this->_M_impl._M_finish =
1204  std::__uninitialized_copy_a(__first, __last,
1205  this->_M_impl._M_start,
1206  _M_get_Tp_allocator());
1207  }
1208 
1209  // Called by the first initialize_dispatch above and by the
1210  // vector(n,value,a) constructor.
1211  void
1212  _M_fill_initialize(size_type __n, const value_type& __value)
1213  {
1214  std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1215  _M_get_Tp_allocator());
1216  this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1217  }
1218 
1219 #if __cplusplus >= 201103L
1220  // Called by the vector(n) constructor.
1221  void
1222  _M_default_initialize(size_type __n)
1223  {
1224  std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1225  _M_get_Tp_allocator());
1226  this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1227  }
1228 #endif
1229 
1230  // Internal assign functions follow. The *_aux functions do the actual
1231  // assignment work for the range versions.
1232 
1233  // Called by the range assign to implement [23.1.1]/9
1234 
1235  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1236  // 438. Ambiguity in the "do the right thing" clause
1237  template<typename _Integer>
1238  void
1239  _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1240  { _M_fill_assign(__n, __val); }
1241 
1242  // Called by the range assign to implement [23.1.1]/9
1243  template<typename _InputIterator>
1244  void
1245  _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1246  __false_type)
1247  {
1248  typedef typename std::iterator_traits<_InputIterator>::
1249  iterator_category _IterCategory;
1250  _M_assign_aux(__first, __last, _IterCategory());
1251  }
1252 
1253  // Called by the second assign_dispatch above
1254  template<typename _InputIterator>
1255  void
1256  _M_assign_aux(_InputIterator __first, _InputIterator __last,
1257  std::input_iterator_tag);
1258 
1259  // Called by the second assign_dispatch above
1260  template<typename _ForwardIterator>
1261  void
1262  _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1263  std::forward_iterator_tag);
1264 
1265  // Called by assign(n,t), and the range assign when it turns out
1266  // to be the same thing.
1267  void
1268  _M_fill_assign(size_type __n, const value_type& __val);
1269 
1270 
1271  // Internal insert functions follow.
1272 
1273  // Called by the range insert to implement [23.1.1]/9
1274 
1275  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1276  // 438. Ambiguity in the "do the right thing" clause
1277  template<typename _Integer>
1278  void
1279  _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1280  __true_type)
1281  { _M_fill_insert(__pos, __n, __val); }
1282 
1283  // Called by the range insert to implement [23.1.1]/9
1284  template<typename _InputIterator>
1285  void
1286  _M_insert_dispatch(iterator __pos, _InputIterator __first,
1287  _InputIterator __last, __false_type)
1288  {
1289  typedef typename std::iterator_traits<_InputIterator>::
1290  iterator_category _IterCategory;
1291  _M_range_insert(__pos, __first, __last, _IterCategory());
1292  }
1293 
1294  // Called by the second insert_dispatch above
1295  template<typename _InputIterator>
1296  void
1297  _M_range_insert(iterator __pos, _InputIterator __first,
1298  _InputIterator __last, std::input_iterator_tag);
1299 
1300  // Called by the second insert_dispatch above
1301  template<typename _ForwardIterator>
1302  void
1303  _M_range_insert(iterator __pos, _ForwardIterator __first,
1304  _ForwardIterator __last, std::forward_iterator_tag);
1305 
1306  // Called by insert(p,n,x), and the range insert when it turns out to be
1307  // the same thing.
1308  void
1309  _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1310 
1311 #if __cplusplus >= 201103L
1312  // Called by resize(n).
1313  void
1314  _M_default_append(size_type __n);
1315 
1316  bool
1317  _M_shrink_to_fit();
1318 #endif
1319 
1320  // Called by insert(p,x)
1321 #if __cplusplus < 201103L
1322  void
1323  _M_insert_aux(iterator __position, const value_type& __x);
1324 #else
1325  template<typename... _Args>
1326  void
1327  _M_insert_aux(iterator __position, _Args&&... __args);
1328 
1329  template<typename... _Args>
1330  void
1331  _M_emplace_back_aux(_Args&&... __args);
1332 #endif
1333 
1334  // Called by the latter.
1335  size_type
1336  _M_check_len(size_type __n, const char* __s) const
1337  {
1338  if (max_size() - size() < __n)
1339  __throw_length_error(__N(__s));
1340 
1341  const size_type __len = size() + std::max(size(), __n);
1342  return (__len < size() || __len > max_size()) ? max_size() : __len;
1343  }
1344 
1345  // Internal erase functions follow.
1346 
1347  // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1348  // _M_assign_aux.
1349  void
1350  _M_erase_at_end(pointer __pos)
1351  {
1352  std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1353  this->_M_impl._M_finish = __pos;
1354  }
1355 
1356 #if __cplusplus >= 201103L
1357  private:
1358  // Constant-time move assignment when source object's memory can be
1359  // moved, either because the source's allocator will move too
1360  // or because the allocators are equal.
1361  void
1362  _M_move_assign(vector&& __x, std::true_type) noexcept
1363  {
1364  vector __tmp(get_allocator());
1365  this->_M_impl._M_swap_data(__tmp._M_impl);
1366  this->_M_impl._M_swap_data(__x._M_impl);
1367  if (_Alloc_traits::_S_propagate_on_move_assign())
1368  std::__alloc_on_move(_M_get_Tp_allocator(),
1369  __x._M_get_Tp_allocator());
1370  }
1371 
1372  // Do move assignment when it might not be possible to move source
1373  // object's memory, resulting in a linear-time operation.
1374  void
1375  _M_move_assign(vector&& __x, std::false_type)
1376  {
1377  if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1378  _M_move_assign(std::move(__x), std::true_type());
1379  else
1380  {
1381  // The rvalue's allocator cannot be moved and is not equal,
1382  // so we need to individually move each element.
1383  this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
1384  std::__make_move_if_noexcept_iterator(__x.end()));
1385  __x.clear();
1386  }
1387  }
1388 #endif
1389  };
1390 
1391 
1402  template<typename _Tp, typename _Alloc>
1403  inline bool
1404  operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1405  { return (__x.size() == __y.size()
1406  && std::equal(__x.begin(), __x.end(), __y.begin())); }
1407 
1419  template<typename _Tp, typename _Alloc>
1420  inline bool
1421  operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1422  { return std::lexicographical_compare(__x.begin(), __x.end(),
1423  __y.begin(), __y.end()); }
1424 
1426  template<typename _Tp, typename _Alloc>
1427  inline bool
1428  operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1429  { return !(__x == __y); }
1430 
1432  template<typename _Tp, typename _Alloc>
1433  inline bool
1434  operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1435  { return __y < __x; }
1436 
1438  template<typename _Tp, typename _Alloc>
1439  inline bool
1440  operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1441  { return !(__y < __x); }
1442 
1444  template<typename _Tp, typename _Alloc>
1445  inline bool
1446  operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1447  { return !(__x < __y); }
1448 
1450  template<typename _Tp, typename _Alloc>
1451  inline void
1452  swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1453  { __x.swap(__y); }
1454 
1455 _GLIBCXX_END_NAMESPACE_CONTAINER
1456 } // namespace std
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __try
Definition: exception_defines.h:35
#define __throw_exception_again
Definition: exception_defines.h:37
#define __glibcxx_class_requires(_a, _b)
Definition: concept_check.h:48
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
#define __glibcxx_class_requires2(_a, _b, _c)
Definition: concept_check.h:49
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
const _Tp & max(const _Tp &__a, const _Tp &__b)
Equivalent to std::max.
Definition: base.h:150
std::tr1::integral_constant< int, 1 > true_type
Definition: type_utils.hpp:70
#define __catch(X)
Definition: exception_defines.h:36
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160
std::tr1::integral_constant< int, 0 > false_type
Definition: type_utils.hpp:71