libstdc++
stl_vector.h
Go to the documentation of this file.
1 // Vector implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
51 /** @file bits/stl_vector.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{vector}
54  */
55 
56 #ifndef _STL_VECTOR_H
57 #define _STL_VECTOR_H 1
58 
60 #include <bits/functexcept.h>
61 #include <bits/concept_check.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
64 #endif
65 
66 namespace std _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 
70  /// See bits/stl_deque.h's _Deque_base for an explanation.
71  template<typename _Tp, typename _Alloc>
72  struct _Vector_base
73  {
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 
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 
188  /**
189  * @brief A standard container which offers fixed time access to
190  * individual elements in any order.
191  *
192  * @ingroup sequences
193  *
194  * @tparam _Tp Type of element.
195  * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
196  *
197  * Meets the requirements of a <a href="tables.html#65">container</a>, a
198  * <a href="tables.html#66">reversible container</a>, and a
199  * <a href="tables.html#67">sequence</a>, including the
200  * <a href="tables.html#68">optional sequence requirements</a> with the
201  * %exception of @c push_front and @c pop_front.
202  *
203  * In some terminology a %vector can be described as a dynamic
204  * C-style array, it offers fast and efficient access to individual
205  * elements in any order and saves the user from worrying about
206  * memory and size allocation. Subscripting ( @c [] ) access is
207  * also provided as with C-style arrays.
208  */
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 
218  typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
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;
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)
245  /**
246  * @brief Default constructor creates no elements.
247  */
249  : _Base() { }
250 
251  /**
252  * @brief Creates a %vector with no elements.
253  * @param __a An allocator object.
254  */
255  explicit
256  vector(const allocator_type& __a)
257  : _Base(__a) { }
258 
259 #if __cplusplus >= 201103L
260  /**
261  * @brief Creates a %vector with default constructed elements.
262  * @param __n The number of elements to initially create.
263  * @param __a An allocator.
264  *
265  * This constructor fills the %vector with @a __n default
266  * constructed elements.
267  */
268  explicit
269  vector(size_type __n, const allocator_type& __a = allocator_type())
270  : _Base(__n, __a)
271  { _M_default_initialize(__n); }
272 
273  /**
274  * @brief Creates a %vector with copies of an exemplar element.
275  * @param __n The number of elements to initially create.
276  * @param __value An element to copy.
277  * @param __a An allocator.
278  *
279  * This constructor fills the %vector with @a __n copies of @a __value.
280  */
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  /**
287  * @brief Creates a %vector with copies of an exemplar element.
288  * @param __n The number of elements to initially create.
289  * @param __value An element to copy.
290  * @param __a An allocator.
291  *
292  * This constructor fills the %vector with @a __n copies of @a __value.
293  */
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 
301  /**
302  * @brief %Vector copy constructor.
303  * @param __x A %vector of identical element and allocator types.
304  *
305  * The newly-created %vector uses a copy of the allocation
306  * object used by @a __x. All the elements of @a __x are copied,
307  * but any extra memory in
308  * @a __x (for fast expansion) will not be copied.
309  */
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  /**
321  * @brief %Vector move constructor.
322  * @param __x A %vector of identical element and allocator types.
323  *
324  * The newly-created %vector contains the exact contents of @a __x.
325  * The contents of @a __x are a valid, but unspecified %vector.
326  */
327  vector(vector&& __x) noexcept
328  : _Base(std::move(__x)) { }
329 
330  /// Copy constructor with alternative allocator
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 
339  /// Move constructor with alternative allocator
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 
353  /**
354  * @brief Builds a %vector from an initializer list.
355  * @param __l An initializer_list.
356  * @param __a An allocator.
357  *
358  * Create a %vector consisting of copies of the elements in the
359  * initializer_list @a __l.
360  *
361  * This will call the element type's copy constructor N times
362  * (where N is @a __l.size()) and do no memory reallocation.
363  */
365  const allocator_type& __a = allocator_type())
366  : _Base(__a)
367  {
368  _M_range_initialize(__l.begin(), __l.end(),
370  }
371 #endif
372 
373  /**
374  * @brief Builds a %vector from a range.
375  * @param __first An input iterator.
376  * @param __last An input iterator.
377  * @param __a An allocator.
378  *
379  * Create a %vector consisting of copies of the elements from
380  * [first,last).
381  *
382  * If the iterators are forward, bidirectional, or
383  * random-access, then this will call the elements' copy
384  * constructor N times (where N is distance(first,last)) and do
385  * no memory reallocation. But if only input iterators are
386  * used, then this will do at most 2N calls to the copy
387  * constructor, and logN memory reallocations.
388  */
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 
408  /**
409  * The dtor only erases the elements, and note that if the
410  * elements themselves are pointers, the pointed-to memory is
411  * not touched in any way. Managing the pointer is the user's
412  * responsibility.
413  */
414  ~vector() _GLIBCXX_NOEXCEPT
415  { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
416  _M_get_Tp_allocator()); }
417 
418  /**
419  * @brief %Vector assignment operator.
420  * @param __x A %vector of identical element and allocator types.
421  *
422  * All the elements of @a __x are copied, but any extra memory in
423  * @a __x (for fast expansion) will not be copied. Unlike the
424  * copy constructor, the allocator object is not copied.
425  */
426  vector&
427  operator=(const vector& __x);
428 
429 #if __cplusplus >= 201103L
430  /**
431  * @brief %Vector move assignment operator.
432  * @param __x A %vector of identical element and allocator types.
433  *
434  * The contents of @a __x are moved into this %vector (without copying,
435  * if the allocators permit it).
436  * @a __x is a valid, but unspecified %vector.
437  */
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),
446  return *this;
447  }
448 
449  /**
450  * @brief %Vector list assignment operator.
451  * @param __l An initializer_list.
452  *
453  * This function fills a %vector with copies of the elements in the
454  * initializer list @a __l.
455  *
456  * Note that the assignment completely changes the %vector and
457  * that the resulting %vector's size is the same as the number
458  * of elements assigned. Old data may be lost.
459  */
460  vector&
462  {
463  this->assign(__l.begin(), __l.end());
464  return *this;
465  }
466 #endif
467 
468  /**
469  * @brief Assigns a given value to a %vector.
470  * @param __n Number of elements to be assigned.
471  * @param __val Value to be assigned.
472  *
473  * This function fills a %vector with @a __n copies of the given
474  * value. Note that the assignment completely changes the
475  * %vector and that the resulting %vector's size is the same as
476  * the number of elements assigned. Old data may be lost.
477  */
478  void
479  assign(size_type __n, const value_type& __val)
480  { _M_fill_assign(__n, __val); }
481 
482  /**
483  * @brief Assigns a range to a %vector.
484  * @param __first An input iterator.
485  * @param __last An input iterator.
486  *
487  * This function fills a %vector with copies of the elements in the
488  * range [__first,__last).
489  *
490  * Note that the assignment completely changes the %vector and
491  * that the resulting %vector's size is the same as the number
492  * of elements assigned. Old data may be lost.
493  */
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  /**
513  * @brief Assigns an initializer list to a %vector.
514  * @param __l An initializer_list.
515  *
516  * This function fills a %vector with copies of the elements in the
517  * initializer list @a __l.
518  *
519  * Note that the assignment completely changes the %vector and
520  * that the resulting %vector's size is the same as the number
521  * of elements assigned. Old data may be lost.
522  */
523  void
525  { this->assign(__l.begin(), __l.end()); }
526 #endif
527 
528  /// Get a copy of the memory allocation object.
529  using _Base::get_allocator;
530 
531  // iterators
532  /**
533  * Returns a read/write iterator that points to the first
534  * element in the %vector. Iteration is done in ordinary
535  * element order.
536  */
537  iterator
538  begin() _GLIBCXX_NOEXCEPT
539  { return iterator(this->_M_impl._M_start); }
540 
541  /**
542  * Returns a read-only (constant) iterator that points to the
543  * first element in the %vector. Iteration is done in ordinary
544  * element order.
545  */
546  const_iterator
547  begin() const _GLIBCXX_NOEXCEPT
548  { return const_iterator(this->_M_impl._M_start); }
549 
550  /**
551  * Returns a read/write iterator that points one past the last
552  * element in the %vector. Iteration is done in ordinary
553  * element order.
554  */
555  iterator
556  end() _GLIBCXX_NOEXCEPT
557  { return iterator(this->_M_impl._M_finish); }
558 
559  /**
560  * Returns a read-only (constant) iterator that points one past
561  * the last element in the %vector. Iteration is done in
562  * ordinary element order.
563  */
564  const_iterator
565  end() const _GLIBCXX_NOEXCEPT
566  { return const_iterator(this->_M_impl._M_finish); }
567 
568  /**
569  * Returns a read/write reverse iterator that points to the
570  * last element in the %vector. Iteration is done in reverse
571  * element order.
572  */
574  rbegin() _GLIBCXX_NOEXCEPT
575  { return reverse_iterator(end()); }
576 
577  /**
578  * Returns a read-only (constant) reverse iterator that points
579  * to the last element in the %vector. Iteration is done in
580  * reverse element order.
581  */
582  const_reverse_iterator
583  rbegin() const _GLIBCXX_NOEXCEPT
584  { return const_reverse_iterator(end()); }
585 
586  /**
587  * Returns a read/write reverse iterator that points to one
588  * before the first element in the %vector. Iteration is done
589  * in reverse element order.
590  */
592  rend() _GLIBCXX_NOEXCEPT
593  { return reverse_iterator(begin()); }
594 
595  /**
596  * Returns a read-only (constant) reverse iterator that points
597  * to one before the first element in the %vector. Iteration
598  * is done in reverse element order.
599  */
600  const_reverse_iterator
601  rend() const _GLIBCXX_NOEXCEPT
602  { return const_reverse_iterator(begin()); }
603 
604 #if __cplusplus >= 201103L
605  /**
606  * Returns a read-only (constant) iterator that points to the
607  * first element in the %vector. Iteration is done in ordinary
608  * element order.
609  */
610  const_iterator
611  cbegin() const noexcept
612  { return const_iterator(this->_M_impl._M_start); }
613 
614  /**
615  * Returns a read-only (constant) iterator that points one past
616  * the last element in the %vector. Iteration is done in
617  * ordinary element order.
618  */
619  const_iterator
620  cend() const noexcept
621  { return const_iterator(this->_M_impl._M_finish); }
622 
623  /**
624  * Returns a read-only (constant) reverse iterator that points
625  * to the last element in the %vector. Iteration is done in
626  * reverse element order.
627  */
628  const_reverse_iterator
629  crbegin() const noexcept
630  { return const_reverse_iterator(end()); }
631 
632  /**
633  * Returns a read-only (constant) reverse iterator that points
634  * to one before the first element in the %vector. Iteration
635  * is done in reverse element order.
636  */
637  const_reverse_iterator
638  crend() const noexcept
639  { return const_reverse_iterator(begin()); }
640 #endif
641 
642  // [23.2.4.2] capacity
643  /** Returns the number of elements in the %vector. */
644  size_type
645  size() const _GLIBCXX_NOEXCEPT
646  { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
647 
648  /** Returns the size() of the largest possible %vector. */
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  /**
655  * @brief Resizes the %vector to the specified number of elements.
656  * @param __new_size Number of elements the %vector should contain.
657  *
658  * This function will %resize the %vector to the specified
659  * number of elements. If the number is smaller than the
660  * %vector's current size the %vector is truncated, otherwise
661  * default constructed elements are appended.
662  */
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 
672  /**
673  * @brief Resizes the %vector to the specified number of elements.
674  * @param __new_size Number of elements the %vector should contain.
675  * @param __x Data with which new elements should be populated.
676  *
677  * This function will %resize the %vector to the specified
678  * number of elements. If the number is smaller than the
679  * %vector's current size the %vector is truncated, otherwise
680  * the %vector is extended and new elements are populated with
681  * given data.
682  */
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  /**
693  * @brief Resizes the %vector to the specified number of elements.
694  * @param __new_size Number of elements the %vector should contain.
695  * @param __x Data with which new elements should be populated.
696  *
697  * This function will %resize the %vector to the specified
698  * number of elements. If the number is smaller than the
699  * %vector's current size the %vector is truncated, otherwise
700  * the %vector is extended and new elements are populated with
701  * given data.
702  */
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  /** A non-binding request to reduce capacity() to size(). */
715  void
717  { _M_shrink_to_fit(); }
718 #endif
719 
720  /**
721  * Returns the total number of elements that the %vector can
722  * hold before needing to allocate more memory.
723  */
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 
729  /**
730  * Returns true if the %vector is empty. (Thus begin() would
731  * equal end().)
732  */
733  bool
734  empty() const _GLIBCXX_NOEXCEPT
735  { return begin() == end(); }
736 
737  /**
738  * @brief Attempt to preallocate enough memory for specified number of
739  * elements.
740  * @param __n Number of elements required.
741  * @throw std::length_error If @a n exceeds @c max_size().
742  *
743  * This function attempts to reserve enough memory for the
744  * %vector to hold the specified number of elements. If the
745  * number requested is more than max_size(), length_error is
746  * thrown.
747  *
748  * The advantage of this function is that if optimal code is a
749  * necessity and the user can determine the number of elements
750  * that will be required, the user can reserve the memory in
751  * %advance, and thus prevent a possible reallocation of memory
752  * and copying of %vector data.
753  */
754  void
755  reserve(size_type __n);
756 
757  // element access
758  /**
759  * @brief Subscript access to the data contained in the %vector.
760  * @param __n The index of the element for which data should be
761  * accessed.
762  * @return Read/write reference to data.
763  *
764  * This operator allows for easy, array-style, data access.
765  * Note that data access with this operator is unchecked and
766  * out_of_range lookups are not defined. (For checked lookups
767  * see at().)
768  */
769  reference
770  operator[](size_type __n)
771  { return *(this->_M_impl._M_start + __n); }
772 
773  /**
774  * @brief Subscript access to the data contained in the %vector.
775  * @param __n The index of the element for which data should be
776  * accessed.
777  * @return Read-only (constant) reference to data.
778  *
779  * This operator allows for easy, array-style, data access.
780  * Note that data access with this operator is unchecked and
781  * out_of_range lookups are not defined. (For checked lookups
782  * see at().)
783  */
784  const_reference
785  operator[](size_type __n) const
786  { return *(this->_M_impl._M_start + __n); }
787 
788  protected:
789  /// Safety check used only from at().
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:
798  /**
799  * @brief Provides access to the data contained in the %vector.
800  * @param __n The index of the element for which data should be
801  * accessed.
802  * @return Read/write reference to data.
803  * @throw std::out_of_range If @a __n is an invalid index.
804  *
805  * This function provides for safer data access. The parameter
806  * is first checked that it is in the range of the vector. The
807  * function throws out_of_range if the check fails.
808  */
809  reference
810  at(size_type __n)
811  {
812  _M_range_check(__n);
813  return (*this)[__n];
814  }
815 
816  /**
817  * @brief Provides access to the data contained in the %vector.
818  * @param __n The index of the element for which data should be
819  * accessed.
820  * @return Read-only (constant) reference to data.
821  * @throw std::out_of_range If @a __n is an invalid index.
822  *
823  * This function provides for safer data access. The parameter
824  * is first checked that it is in the range of the vector. The
825  * function throws out_of_range if the check fails.
826  */
827  const_reference
828  at(size_type __n) const
829  {
830  _M_range_check(__n);
831  return (*this)[__n];
832  }
833 
834  /**
835  * Returns a read/write reference to the data at the first
836  * element of the %vector.
837  */
838  reference
840  { return *begin(); }
841 
842  /**
843  * Returns a read-only (constant) reference to the data at the first
844  * element of the %vector.
845  */
846  const_reference
847  front() const
848  { return *begin(); }
849 
850  /**
851  * Returns a read/write reference to the data at the last
852  * element of the %vector.
853  */
854  reference
856  { return *(end() - 1); }
857 
858  /**
859  * Returns a read-only (constant) reference to the data at the
860  * last element of the %vector.
861  */
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
869  /**
870  * Returns a pointer such that [data(), data() + size()) is a valid
871  * range. For a non-empty %vector, data() == &front().
872  */
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
890  /**
891  * @brief Add data to the end of the %vector.
892  * @param __x Data to be added.
893  *
894  * This is a typical stack operation. The function creates an
895  * element at the end of the %vector and assigns the given data
896  * to it. Due to the nature of a %vector this operation can be
897  * done in constant time if the %vector has preallocated space
898  * available.
899  */
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 
927  /**
928  * @brief Removes last element.
929  *
930  * This is a typical stack operation. It shrinks the %vector by one.
931  *
932  * Note that no data is returned, and if the last element's
933  * data is needed, it should be retrieved before pop_back() is
934  * called.
935  */
936  void
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  /**
945  * @brief Inserts an object in %vector before specified iterator.
946  * @param __position A const_iterator into the %vector.
947  * @param __args Arguments.
948  * @return An iterator that points to the inserted data.
949  *
950  * This function will insert an object of type T constructed
951  * with T(std::forward<Args>(args)...) before the specified location.
952  * Note that this kind of operation could be expensive for a %vector
953  * and if it is frequently used the user should consider using
954  * std::list.
955  */
956  template<typename... _Args>
957  iterator
958  emplace(const_iterator __position, _Args&&... __args);
959 
960  /**
961  * @brief Inserts given value into %vector before specified iterator.
962  * @param __position A const_iterator into the %vector.
963  * @param __x Data to be inserted.
964  * @return An iterator that points to the inserted data.
965  *
966  * This function will insert a copy of the given value before
967  * the specified location. Note that this kind of operation
968  * could be expensive for a %vector and if it is frequently
969  * used the user should consider using std::list.
970  */
971  iterator
972  insert(const_iterator __position, const value_type& __x);
973 #else
974  /**
975  * @brief Inserts given value into %vector before specified iterator.
976  * @param __position An iterator into the %vector.
977  * @param __x Data to be inserted.
978  * @return An iterator that points to the inserted data.
979  *
980  * This function will insert a copy of the given value before
981  * the specified location. Note that this kind of operation
982  * could be expensive for a %vector and if it is frequently
983  * used the user should consider using std::list.
984  */
985  iterator
986  insert(iterator __position, const value_type& __x);
987 #endif
988 
989 #if __cplusplus >= 201103L
990  /**
991  * @brief Inserts given rvalue into %vector before specified iterator.
992  * @param __position A const_iterator into the %vector.
993  * @param __x Data to be inserted.
994  * @return An iterator that points to the inserted data.
995  *
996  * This function will insert a copy of the given rvalue before
997  * the specified location. Note that this kind of operation
998  * could be expensive for a %vector and if it is frequently
999  * used the user should consider using std::list.
1000  */
1001  iterator
1002  insert(const_iterator __position, value_type&& __x)
1003  { return emplace(__position, std::move(__x)); }
1004 
1005  /**
1006  * @brief Inserts an initializer_list into the %vector.
1007  * @param __position An iterator into the %vector.
1008  * @param __l An initializer_list.
1009  *
1010  * This function will insert copies of the data in the
1011  * initializer_list @a l into the %vector before the location
1012  * specified by @a position.
1013  *
1014  * Note that this kind of operation could be expensive for a
1015  * %vector and if it is frequently used the user should
1016  * consider using std::list.
1017  */
1018  iterator
1019  insert(const_iterator __position, initializer_list<value_type> __l)
1020  { return this->insert(__position, __l.begin(), __l.end()); }
1021 #endif
1022 
1023 #if __cplusplus >= 201103L
1024  /**
1025  * @brief Inserts a number of copies of given data into the %vector.
1026  * @param __position A const_iterator into the %vector.
1027  * @param __n Number of elements to be inserted.
1028  * @param __x Data to be inserted.
1029  * @return An iterator that points to the inserted data.
1030  *
1031  * This function will insert a specified number of copies of
1032  * the given data before the location specified by @a position.
1033  *
1034  * Note that this kind of operation could be expensive for a
1035  * %vector and if it is frequently used the user should
1036  * consider using std::list.
1037  */
1038  iterator
1039  insert(const_iterator __position, size_type __n, const value_type& __x)
1040  {
1041  difference_type __offset = __position - cbegin();
1042  _M_fill_insert(__position._M_const_cast(), __n, __x);
1043  return begin() + __offset;
1044  }
1045 #else
1046  /**
1047  * @brief Inserts a number of copies of given data into the %vector.
1048  * @param __position An iterator into the %vector.
1049  * @param __n Number of elements to be inserted.
1050  * @param __x Data to be inserted.
1051  *
1052  * This function will insert a specified number of copies of
1053  * the given data before the location specified by @a position.
1054  *
1055  * Note that this kind of operation could be expensive for a
1056  * %vector and if it is frequently used the user should
1057  * consider using std::list.
1058  */
1059  void
1060  insert(iterator __position, size_type __n, const value_type& __x)
1061  { _M_fill_insert(__position, __n, __x); }
1062 #endif
1063 
1064 #if __cplusplus >= 201103L
1065  /**
1066  * @brief Inserts a range into the %vector.
1067  * @param __position A const_iterator into the %vector.
1068  * @param __first An input iterator.
1069  * @param __last An input iterator.
1070  * @return An iterator that points to the inserted data.
1071  *
1072  * This function will insert copies of the data in the range
1073  * [__first,__last) into the %vector before the location specified
1074  * by @a pos.
1075  *
1076  * Note that this kind of operation could be expensive for a
1077  * %vector and if it is frequently used the user should
1078  * consider using std::list.
1079  */
1080  template<typename _InputIterator,
1081  typename = std::_RequireInputIter<_InputIterator>>
1082  iterator
1083  insert(const_iterator __position, _InputIterator __first,
1084  _InputIterator __last)
1085  {
1086  difference_type __offset = __position - cbegin();
1087  _M_insert_dispatch(__position._M_const_cast(),
1088  __first, __last, __false_type());
1089  return begin() + __offset;
1090  }
1091 #else
1092  /**
1093  * @brief Inserts a range into the %vector.
1094  * @param __position An iterator into the %vector.
1095  * @param __first An input iterator.
1096  * @param __last An input iterator.
1097  *
1098  * This function will insert copies of the data in the range
1099  * [__first,__last) into the %vector before the location specified
1100  * by @a pos.
1101  *
1102  * Note that this kind of operation could be expensive for a
1103  * %vector and if it is frequently used the user should
1104  * consider using std::list.
1105  */
1106  template<typename _InputIterator>
1107  void
1108  insert(iterator __position, _InputIterator __first,
1109  _InputIterator __last)
1110  {
1111  // Check whether it's an integral type. If so, it's not an iterator.
1112  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1113  _M_insert_dispatch(__position, __first, __last, _Integral());
1114  }
1115 #endif
1116 
1117  /**
1118  * @brief Remove element at given position.
1119  * @param __position Iterator pointing to element to be erased.
1120  * @return An iterator pointing to the next element (or end()).
1121  *
1122  * This function will erase the element at the given position and thus
1123  * shorten the %vector by one.
1124  *
1125  * Note This operation could be expensive and if it is
1126  * frequently used the user should consider using std::list.
1127  * The user is also cautioned that this function only erases
1128  * the element, and that if the element is itself a pointer,
1129  * the pointed-to memory is not touched in any way. Managing
1130  * the pointer is the user's responsibility.
1131  */
1132  iterator
1133 #if __cplusplus >= 201103L
1134  erase(const_iterator __position)
1135 #else
1136  erase(iterator __position)
1137 #endif
1138  { return _M_erase(__position._M_const_cast()); }
1139 
1140  /**
1141  * @brief Remove a range of elements.
1142  * @param __first Iterator pointing to the first element to be erased.
1143  * @param __last Iterator pointing to one past the last element to be
1144  * erased.
1145  * @return An iterator pointing to the element pointed to by @a __last
1146  * prior to erasing (or end()).
1147  *
1148  * This function will erase the elements in the range
1149  * [__first,__last) and shorten the %vector accordingly.
1150  *
1151  * Note This operation could be expensive and if it is
1152  * frequently used the user should consider using std::list.
1153  * The user is also cautioned that this function only erases
1154  * the elements, and that if the elements themselves are
1155  * pointers, the pointed-to memory is not touched in any way.
1156  * Managing the pointer is the user's responsibility.
1157  */
1158  iterator
1159 #if __cplusplus >= 201103L
1160  erase(const_iterator __first, const_iterator __last)
1161 #else
1162  erase(iterator __first, iterator __last)
1163 #endif
1164  { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1165 
1166  /**
1167  * @brief Swaps data with another %vector.
1168  * @param __x A %vector of the same element and allocator types.
1169  *
1170  * This exchanges the elements between two vectors in constant time.
1171  * (Three pointers, so it should be quite fast.)
1172  * Note that the global std::swap() function is specialized such that
1173  * std::swap(v1,v2) will feed to this function.
1174  */
1175  void
1176  swap(vector& __x)
1177 #if __cplusplus >= 201103L
1178  noexcept(_Alloc_traits::_S_nothrow_swap())
1179 #endif
1180  {
1181  this->_M_impl._M_swap_data(__x._M_impl);
1182  _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1183  __x._M_get_Tp_allocator());
1184  }
1185 
1186  /**
1187  * Erases all the elements. Note that this function only erases the
1188  * elements, and that if the elements themselves are pointers, the
1189  * pointed-to memory is not touched in any way. Managing the pointer is
1190  * the user's responsibility.
1191  */
1192  void
1193  clear() _GLIBCXX_NOEXCEPT
1194  { _M_erase_at_end(this->_M_impl._M_start); }
1195 
1196  protected:
1197  /**
1198  * Memory expansion handler. Uses the member allocation function to
1199  * obtain @a n bytes of memory, and then copies [first,last) into it.
1200  */
1201  template<typename _ForwardIterator>
1202  pointer
1203  _M_allocate_and_copy(size_type __n,
1204  _ForwardIterator __first, _ForwardIterator __last)
1205  {
1206  pointer __result = this->_M_allocate(__n);
1207  __try
1208  {
1209  std::__uninitialized_copy_a(__first, __last, __result,
1210  _M_get_Tp_allocator());
1211  return __result;
1212  }
1213  __catch(...)
1214  {
1215  _M_deallocate(__result, __n);
1216  __throw_exception_again;
1217  }
1218  }
1219 
1220 
1221  // Internal constructor functions follow.
1222 
1223  // Called by the range constructor to implement [23.1.1]/9
1224 
1225  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1226  // 438. Ambiguity in the "do the right thing" clause
1227  template<typename _Integer>
1228  void
1229  _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1230  {
1231  this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1232  this->_M_impl._M_end_of_storage =
1233  this->_M_impl._M_start + static_cast<size_type>(__n);
1234  _M_fill_initialize(static_cast<size_type>(__n), __value);
1235  }
1236 
1237  // Called by the range constructor to implement [23.1.1]/9
1238  template<typename _InputIterator>
1239  void
1240  _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1241  __false_type)
1242  {
1243  typedef typename std::iterator_traits<_InputIterator>::
1244  iterator_category _IterCategory;
1245  _M_range_initialize(__first, __last, _IterCategory());
1246  }
1247 
1248  // Called by the second initialize_dispatch above
1249  template<typename _InputIterator>
1250  void
1251  _M_range_initialize(_InputIterator __first,
1252  _InputIterator __last, std::input_iterator_tag)
1253  {
1254  for (; __first != __last; ++__first)
1255 #if __cplusplus >= 201103L
1256  emplace_back(*__first);
1257 #else
1258  push_back(*__first);
1259 #endif
1260  }
1261 
1262  // Called by the second initialize_dispatch above
1263  template<typename _ForwardIterator>
1264  void
1265  _M_range_initialize(_ForwardIterator __first,
1266  _ForwardIterator __last, std::forward_iterator_tag)
1267  {
1268  const size_type __n = std::distance(__first, __last);
1269  this->_M_impl._M_start = this->_M_allocate(__n);
1270  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1271  this->_M_impl._M_finish =
1272  std::__uninitialized_copy_a(__first, __last,
1273  this->_M_impl._M_start,
1274  _M_get_Tp_allocator());
1275  }
1276 
1277  // Called by the first initialize_dispatch above and by the
1278  // vector(n,value,a) constructor.
1279  void
1280  _M_fill_initialize(size_type __n, const value_type& __value)
1281  {
1282  std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1283  _M_get_Tp_allocator());
1284  this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1285  }
1286 
1287 #if __cplusplus >= 201103L
1288  // Called by the vector(n) constructor.
1289  void
1290  _M_default_initialize(size_type __n)
1291  {
1292  std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1293  _M_get_Tp_allocator());
1294  this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1295  }
1296 #endif
1297 
1298  // Internal assign functions follow. The *_aux functions do the actual
1299  // assignment work for the range versions.
1300 
1301  // Called by the range assign to implement [23.1.1]/9
1302 
1303  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1304  // 438. Ambiguity in the "do the right thing" clause
1305  template<typename _Integer>
1306  void
1307  _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1308  { _M_fill_assign(__n, __val); }
1309 
1310  // Called by the range assign to implement [23.1.1]/9
1311  template<typename _InputIterator>
1312  void
1313  _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1314  __false_type)
1315  {
1316  typedef typename std::iterator_traits<_InputIterator>::
1317  iterator_category _IterCategory;
1318  _M_assign_aux(__first, __last, _IterCategory());
1319  }
1320 
1321  // Called by the second assign_dispatch above
1322  template<typename _InputIterator>
1323  void
1324  _M_assign_aux(_InputIterator __first, _InputIterator __last,
1326 
1327  // Called by the second assign_dispatch above
1328  template<typename _ForwardIterator>
1329  void
1330  _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1332 
1333  // Called by assign(n,t), and the range assign when it turns out
1334  // to be the same thing.
1335  void
1336  _M_fill_assign(size_type __n, const value_type& __val);
1337 
1338 
1339  // Internal insert functions follow.
1340 
1341  // Called by the range insert to implement [23.1.1]/9
1342 
1343  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1344  // 438. Ambiguity in the "do the right thing" clause
1345  template<typename _Integer>
1346  void
1347  _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1348  __true_type)
1349  { _M_fill_insert(__pos, __n, __val); }
1350 
1351  // Called by the range insert to implement [23.1.1]/9
1352  template<typename _InputIterator>
1353  void
1354  _M_insert_dispatch(iterator __pos, _InputIterator __first,
1355  _InputIterator __last, __false_type)
1356  {
1357  typedef typename std::iterator_traits<_InputIterator>::
1358  iterator_category _IterCategory;
1359  _M_range_insert(__pos, __first, __last, _IterCategory());
1360  }
1361 
1362  // Called by the second insert_dispatch above
1363  template<typename _InputIterator>
1364  void
1365  _M_range_insert(iterator __pos, _InputIterator __first,
1366  _InputIterator __last, std::input_iterator_tag);
1367 
1368  // Called by the second insert_dispatch above
1369  template<typename _ForwardIterator>
1370  void
1371  _M_range_insert(iterator __pos, _ForwardIterator __first,
1372  _ForwardIterator __last, std::forward_iterator_tag);
1373 
1374  // Called by insert(p,n,x), and the range insert when it turns out to be
1375  // the same thing.
1376  void
1377  _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1378 
1379 #if __cplusplus >= 201103L
1380  // Called by resize(n).
1381  void
1382  _M_default_append(size_type __n);
1383 
1384  bool
1385  _M_shrink_to_fit();
1386 #endif
1387 
1388  // Called by insert(p,x)
1389 #if __cplusplus < 201103L
1390  void
1391  _M_insert_aux(iterator __position, const value_type& __x);
1392 #else
1393  template<typename... _Args>
1394  void
1395  _M_insert_aux(iterator __position, _Args&&... __args);
1396 
1397  template<typename... _Args>
1398  void
1399  _M_emplace_back_aux(_Args&&... __args);
1400 #endif
1401 
1402  // Called by the latter.
1403  size_type
1404  _M_check_len(size_type __n, const char* __s) const
1405  {
1406  if (max_size() - size() < __n)
1407  __throw_length_error(__N(__s));
1408 
1409  const size_type __len = size() + std::max(size(), __n);
1410  return (__len < size() || __len > max_size()) ? max_size() : __len;
1411  }
1412 
1413  // Internal erase functions follow.
1414 
1415  // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1416  // _M_assign_aux.
1417  void
1418  _M_erase_at_end(pointer __pos)
1419  {
1420  std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1421  this->_M_impl._M_finish = __pos;
1422  }
1423 
1424  iterator
1425  _M_erase(iterator __position);
1426 
1427  iterator
1428  _M_erase(iterator __first, iterator __last);
1429 
1430 #if __cplusplus >= 201103L
1431  private:
1432  // Constant-time move assignment when source object's memory can be
1433  // moved, either because the source's allocator will move too
1434  // or because the allocators are equal.
1435  void
1436  _M_move_assign(vector&& __x, std::true_type) noexcept
1437  {
1438  const vector __tmp(std::move(*this));
1439  this->_M_impl._M_swap_data(__x._M_impl);
1440  if (_Alloc_traits::_S_propagate_on_move_assign())
1441  std::__alloc_on_move(_M_get_Tp_allocator(),
1442  __x._M_get_Tp_allocator());
1443  }
1444 
1445  // Do move assignment when it might not be possible to move source
1446  // object's memory, resulting in a linear-time operation.
1447  void
1448  _M_move_assign(vector&& __x, std::false_type)
1449  {
1450  if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1451  _M_move_assign(std::move(__x), std::true_type());
1452  else
1453  {
1454  // The rvalue's allocator cannot be moved and is not equal,
1455  // so we need to individually move each element.
1456  this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
1457  std::__make_move_if_noexcept_iterator(__x.end()));
1458  __x.clear();
1459  }
1460  }
1461 #endif
1462  };
1463 
1464 
1465  /**
1466  * @brief Vector equality comparison.
1467  * @param __x A %vector.
1468  * @param __y A %vector of the same type as @a __x.
1469  * @return True iff the size and elements of the vectors are equal.
1470  *
1471  * This is an equivalence relation. It is linear in the size of the
1472  * vectors. Vectors are considered equivalent if their sizes are equal,
1473  * and if corresponding elements compare equal.
1474  */
1475  template<typename _Tp, typename _Alloc>
1476  inline bool
1477  operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1478  { return (__x.size() == __y.size()
1479  && std::equal(__x.begin(), __x.end(), __y.begin())); }
1480 
1481  /**
1482  * @brief Vector ordering relation.
1483  * @param __x A %vector.
1484  * @param __y A %vector of the same type as @a __x.
1485  * @return True iff @a __x is lexicographically less than @a __y.
1486  *
1487  * This is a total ordering relation. It is linear in the size of the
1488  * vectors. The elements must be comparable with @c <.
1489  *
1490  * See std::lexicographical_compare() for how the determination is made.
1491  */
1492  template<typename _Tp, typename _Alloc>
1493  inline bool
1494  operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1495  { return std::lexicographical_compare(__x.begin(), __x.end(),
1496  __y.begin(), __y.end()); }
1497 
1498  /// Based on operator==
1499  template<typename _Tp, typename _Alloc>
1500  inline bool
1501  operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1502  { return !(__x == __y); }
1503 
1504  /// Based on operator<
1505  template<typename _Tp, typename _Alloc>
1506  inline bool
1507  operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1508  { return __y < __x; }
1509 
1510  /// Based on operator<
1511  template<typename _Tp, typename _Alloc>
1512  inline bool
1513  operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1514  { return !(__y < __x); }
1515 
1516  /// Based on operator<
1517  template<typename _Tp, typename _Alloc>
1518  inline bool
1519  operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1520  { return !(__x < __y); }
1521 
1522  /// See std::vector::swap().
1523  template<typename _Tp, typename _Alloc>
1524  inline void
1526  { __x.swap(__y); }
1527 
1528 _GLIBCXX_END_NAMESPACE_CONTAINER
1529 } // namespace std
1530 
1531 #endif /* _STL_VECTOR_H */