libstdc++: stl_vector.h Source File

libstdc++

stl_vector.h
Go to the documentation of this file.
1// Vector implementation -*- C++ -*-
2
3// Copyright (C) 2001-2026 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
59#include <bits/stl_iterator_base_funcs.h>
60#include <bits/stdexcept_throw.h>
61#include <bits/concept_check.h>
62#if __cplusplus >= 201103L
63#include <initializer_list>
64#endif
65#if __cplusplus >= 202002L
66# include <compare>
67#endif
68#if __glibcxx_concepts // C++ >= C++20
69# include <bits/ranges_base.h> // ranges::distance
70#endif
71#if __glibcxx_containers_ranges // C++ >= 23
72# include <bits/ranges_algobase.h> // ranges::copy
73# include <bits/ranges_util.h> // ranges::subrange
74#endif
75
76#include <debug/assertions.h>
77
78#if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
79extern "C" void
80__sanitizer_annotate_contiguous_container(const void*, const void*,
81 const void*, const void*);
82#endif
83
84namespace std _GLIBCXX_VISIBILITY(default)
85{
86_GLIBCXX_BEGIN_NAMESPACE_VERSION
87_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
88
89 /// See bits/stl_deque.h's _Deque_base for an explanation.
90 template<typename _Tp, typename _Alloc>
91 struct _Vector_base
92 {
93 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
94 rebind<_Tp>::other _Tp_alloc_type;
95 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
96 pointer;
97
98 struct _Vector_impl_data
99 {
100 pointer _M_start;
101 pointer _M_finish;
102 pointer _M_end_of_storage;
103
104 _GLIBCXX20_CONSTEXPR
105 _Vector_impl_data() _GLIBCXX_NOEXCEPT
106 : _M_start(), _M_finish(), _M_end_of_storage()
107 { }
108
109#if __cplusplus >= 201103L
110 _GLIBCXX20_CONSTEXPR
111 _Vector_impl_data(_Vector_impl_data&& __x) noexcept
112 : _M_start(__x._M_start), _M_finish(__x._M_finish),
113 _M_end_of_storage(__x._M_end_of_storage)
114 { __x._M_start = __x._M_finish = __x._M_end_of_storage = pointer(); }
115#endif
116
117 _GLIBCXX20_CONSTEXPR
118 void
119 _M_copy_data(_Vector_impl_data const& __x) _GLIBCXX_NOEXCEPT
120 {
121 _M_start = __x._M_start;
122 _M_finish = __x._M_finish;
123 _M_end_of_storage = __x._M_end_of_storage;
124 }
125
126 _GLIBCXX20_CONSTEXPR
127 void
128 _M_swap_data(_Vector_impl_data& __x) _GLIBCXX_NOEXCEPT
129 {
130 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
131 // information used by TBAA.
132 _Vector_impl_data __tmp;
133 __tmp._M_copy_data(*this);
134 _M_copy_data(__x);
135 __x._M_copy_data(__tmp);
136 }
137 };
138
139 struct _Vector_impl
140 : public _Tp_alloc_type, public _Vector_impl_data
141 {
142 _GLIBCXX20_CONSTEXPR
143 _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
144 is_nothrow_default_constructible<_Tp_alloc_type>::value)
145#if __cpp_lib_concepts
146 requires is_default_constructible_v<_Tp_alloc_type>
147#endif
148 : _Tp_alloc_type()
149 { }
150
151 _GLIBCXX20_CONSTEXPR
152 _Vector_impl(_Tp_alloc_type const& __a) _GLIBCXX_NOEXCEPT
153 : _Tp_alloc_type(__a)
154 { }
155
156#if __cplusplus >= 201103L
157 // Not defaulted, to enforce noexcept(true) even when
158 // !is_nothrow_move_constructible<_Tp_alloc_type>.
159 _GLIBCXX20_CONSTEXPR
160 _Vector_impl(_Vector_impl&& __x) noexcept
161 : _Tp_alloc_type(std::move(__x)), _Vector_impl_data(std::move(__x))
162 { }
163
164 _GLIBCXX20_CONSTEXPR
165 _Vector_impl(_Tp_alloc_type&& __a) noexcept
166 : _Tp_alloc_type(std::move(__a))
167 { }
168
169 _GLIBCXX20_CONSTEXPR
170 _Vector_impl(_Tp_alloc_type&& __a, _Vector_impl&& __rv) noexcept
171 : _Tp_alloc_type(std::move(__a)), _Vector_impl_data(std::move(__rv))
172 { }
173#endif
174
175#if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
176 template<typename = _Tp_alloc_type>
177 struct _Asan
178 {
179 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
180 ::size_type size_type;
181
182 static _GLIBCXX20_CONSTEXPR void
183 _S_shrink(_Vector_impl&, size_type) { }
184 static _GLIBCXX20_CONSTEXPR void
185 _S_on_dealloc(_Vector_impl&) { }
186
187 typedef _Vector_impl& _Reinit;
188
189 struct _Grow
190 {
191 _GLIBCXX20_CONSTEXPR _Grow(_Vector_impl&, size_type) { }
192 _GLIBCXX20_CONSTEXPR void _M_grew(size_type) { }
193 };
194 };
195
196 // Enable ASan annotations for memory obtained from std::allocator.
197 template<typename _Up>
198 struct _Asan<allocator<_Up> >
199 {
200 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
201 ::size_type size_type;
202
203 // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
204 // mark end of valid region as __curr instead of __prev.
205 static _GLIBCXX20_CONSTEXPR void
206 _S_adjust(_Vector_impl& __impl, pointer __prev, pointer __curr)
207 {
208#if __cpp_lib_is_constant_evaluated
209 if (std::is_constant_evaluated())
210 return;
211#endif
212 __sanitizer_annotate_contiguous_container(__impl._M_start,
213 __impl._M_end_of_storage, __prev, __curr);
214 }
215
216 static _GLIBCXX20_CONSTEXPR void
217 _S_grow(_Vector_impl& __impl, size_type __n)
218 { _S_adjust(__impl, __impl._M_finish, __impl._M_finish + __n); }
219
220 static _GLIBCXX20_CONSTEXPR void
221 _S_shrink(_Vector_impl& __impl, size_type __n)
222 { _S_adjust(__impl, __impl._M_finish + __n, __impl._M_finish); }
223
224 static _GLIBCXX20_CONSTEXPR void
225 _S_on_dealloc(_Vector_impl& __impl)
226 {
227 if (__impl._M_start)
228 _S_adjust(__impl, __impl._M_finish, __impl._M_end_of_storage);
229 }
230
231 // Used on reallocation to tell ASan unused capacity is invalid.
232 struct _Reinit
233 {
234 explicit _GLIBCXX20_CONSTEXPR
235 _Reinit(_Vector_impl& __impl) : _M_impl(__impl)
236 {
237 // Mark unused capacity as valid again before deallocating it.
238 _S_on_dealloc(_M_impl);
239 }
240
241 _GLIBCXX20_CONSTEXPR
242 ~_Reinit()
243 {
244 // Mark unused capacity as invalid after reallocation.
245 if (_M_impl._M_start)
246 _S_adjust(_M_impl, _M_impl._M_end_of_storage,
247 _M_impl._M_finish);
248 }
249
250 _Vector_impl& _M_impl;
251
252#if __cplusplus >= 201103L
253 _Reinit(const _Reinit&) = delete;
254 _Reinit& operator=(const _Reinit&) = delete;
255#endif
256 };
257
258 // Tell ASan when unused capacity is initialized to be valid.
259 struct _Grow
260 {
261 _GLIBCXX20_CONSTEXPR
262 _Grow(_Vector_impl& __impl, size_type __n)
263 : _M_impl(__impl), _M_n(__n)
264 { _S_grow(_M_impl, __n); }
265
266 _GLIBCXX20_CONSTEXPR
267 ~_Grow() { if (_M_n) _S_shrink(_M_impl, _M_n); }
268
269 _GLIBCXX20_CONSTEXPR
270 void _M_grew(size_type __n) { _M_n -= __n; }
271
272#if __cplusplus >= 201103L
273 _Grow(const _Grow&) = delete;
274 _Grow& operator=(const _Grow&) = delete;
275#endif
276 private:
277 _Vector_impl& _M_impl;
278 size_type _M_n;
279 };
280 };
281
282#define _GLIBCXX_ASAN_ANNOTATE_REINIT \
283 typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
284 __attribute__((__unused__)) __reinit_guard(this->_M_impl)
285#define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
286 typename _Base::_Vector_impl::template _Asan<>::_Grow \
287 __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
288#define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
289#define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
290 _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
291#define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
292 _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
293#else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
294#define _GLIBCXX_ASAN_ANNOTATE_REINIT
295#define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
296#define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
297#define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
298#define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
299#endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
300 };
301
302 public:
303 typedef _Alloc allocator_type;
304
305 _GLIBCXX20_CONSTEXPR
306 _Tp_alloc_type&
307 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
308 { return this->_M_impl; }
309
310 _GLIBCXX20_CONSTEXPR
311 const _Tp_alloc_type&
312 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
313 { return this->_M_impl; }
314
315 _GLIBCXX20_CONSTEXPR
316 allocator_type
317 get_allocator() const _GLIBCXX_NOEXCEPT
318 { return allocator_type(_M_get_Tp_allocator()); }
319
320 static _GLIBCXX20_CONSTEXPR size_t
321 _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
322 {
323 // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
324 // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
325 // (even if std::allocator_traits::max_size says we can).
326 const size_t __diffmax =
327 __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
328 const size_t __allocmax =
329 __gnu_cxx::__alloc_traits<_Alloc>::max_size(__a);
330 return (std::min)(__diffmax, __allocmax);
331 }
332
333#if __cplusplus >= 201103L
334 _Vector_base() = default;
335#else
336 _Vector_base() { }
337#endif
338
339 _GLIBCXX20_CONSTEXPR
340 _Vector_base(const allocator_type& __a) _GLIBCXX_NOEXCEPT
341 : _M_impl(__a) { }
342
343 // Kept for ABI compatibility.
344#if !_GLIBCXX_INLINE_VERSION
345 _GLIBCXX20_CONSTEXPR
346 _Vector_base(size_t __n)
347 : _M_impl()
348 { _M_create_storage(__n); }
349#endif
350
351 _GLIBCXX20_CONSTEXPR
352 _Vector_base(size_t __n, const allocator_type& __a)
353 : _M_impl(__a)
354 { _M_create_storage(__n); }
355
356#if __cplusplus >= 201103L
357 _Vector_base(_Vector_base&&) = default;
358
359 // Kept for ABI compatibility.
360# if !_GLIBCXX_INLINE_VERSION
361 _GLIBCXX20_CONSTEXPR
362 _Vector_base(_Tp_alloc_type&& __a) noexcept
363 : _M_impl(std::move(__a)) { }
364
365 _GLIBCXX20_CONSTEXPR
366 _Vector_base(_Vector_base&& __x, const allocator_type& __a)
367 : _M_impl(__a)
368 {
369 if (__x.get_allocator() == __a)
370 this->_M_impl._M_swap_data(__x._M_impl);
371 else
372 {
373 size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
374 _M_create_storage(__n);
375 }
376 }
377# endif
378
379 _GLIBCXX20_CONSTEXPR
380 _Vector_base(const allocator_type& __a, _Vector_base&& __x)
381 : _M_impl(_Tp_alloc_type(__a), std::move(__x._M_impl))
382 { }
383#endif
384
385 _GLIBCXX20_CONSTEXPR
386 ~_Vector_base() _GLIBCXX_NOEXCEPT
387 {
388 ptrdiff_t __n = _M_impl._M_end_of_storage - _M_impl._M_start;
389 if (__n < 0)
390 __builtin_unreachable();
391 _M_deallocate(_M_impl._M_start, size_t(__n));
392 }
393
394 public:
395 _Vector_impl _M_impl;
396
397 _GLIBCXX20_CONSTEXPR
398 pointer
399 _M_allocate(size_t __n)
400 {
401 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
402 return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer();
403 }
404
405 struct _Alloc_result { pointer __ptr; size_t __count; };
406
407 _GLIBCXX20_CONSTEXPR
408 _Alloc_result
409 _M_allocate_at_least(size_t __n)
410 {
411 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
412 _Alloc_result __r;
413 if (__builtin_expect(__n != 0, true))
414 {
415#ifdef __glibcxx_allocate_at_least // C++23
416 if constexpr (requires { _Tr::allocate_at_least(_M_impl, __n); })
417 {
418 auto [__ptr, __count] = _Tr::allocate_at_least(_M_impl, __n);
419 if (__count > __n)
420 {
421 size_t __max = _S_max_size(_M_get_Tp_allocator());
422 if (__builtin_expect(__count > __max, false))
423 __count = __max;
424 }
425 __r = { __ptr, __count };
426 }
427 else
428#endif
429 {
430 __r.__ptr = _Tr::allocate(_M_impl, __n);
431 __r.__count = __n;
432 }
433 }
434 else
435 {
436 __r.__ptr = pointer();
437 __r.__count = 0;
438 }
439 return __r;
440 }
441
442 _GLIBCXX20_CONSTEXPR
443 void
444 _M_deallocate(pointer __p, size_t __n)
445 {
446 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
447 if (__p)
448 _Tr::deallocate(_M_impl, __p, __n);
449 }
450
451 protected:
452
453 _GLIBCXX20_CONSTEXPR
454 void
455 _M_create_storage(size_t __n)
456 {
457 _Alloc_result __r = this->_M_allocate_at_least(__n);
458 this->_M_impl._M_finish = this->_M_impl._M_start = __r.__ptr;
459 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __r.__count;
460 }
461
462#if __glibcxx_containers_ranges // C++ >= 23
463 // Called by insert_range, and indirectly by assign_range, append_range.
464 // Initializes new elements in storage at __ptr and updates __ptr to
465 // point after the last new element.
466 // Provides strong exception safety guarantee.
467 // Requires [ptr, ptr+distance(rg)) is a valid range.
468 template<ranges::input_range _Rg>
469 constexpr void
470 _M_append_range_to(_Rg&& __rg, pointer& __ptr)
471 {
472 __ptr = std::__uninitialized_copy_a(ranges::begin(__rg),
473 ranges::end(__rg),
474 __ptr, _M_get_Tp_allocator());
475 }
476
477 // Called by assign_range, append_range, insert_range.
478 // Requires capacity() >= size()+distance(rg).
479 template<ranges::input_range _Rg>
480 constexpr void
481 _M_append_range(_Rg&& __rg)
482 { _M_append_range_to(std::forward<_Rg>(__rg), _M_impl._M_finish); }
483#endif
484 };
485
486 /**
487 * @brief A standard container which offers fixed time access to
488 * individual elements in any order.
489 *
490 * @ingroup sequences
491 * @headerfile vector
492 * @since C++98
493 *
494 * @tparam _Tp Type of element.
495 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
496 *
497 * Meets the requirements of a <a href="tables.html#65">container</a>, a
498 * <a href="tables.html#66">reversible container</a>, and a
499 * <a href="tables.html#67">sequence</a>, including the
500 * <a href="tables.html#68">optional sequence requirements</a> with the
501 * %exception of @c push_front and @c pop_front.
502 *
503 * In some terminology a %vector can be described as a dynamic
504 * C-style array, it offers fast and efficient access to individual
505 * elements in any order and saves the user from worrying about
506 * memory and size allocation. Subscripting ( @c [] ) access is
507 * also provided as with C-style arrays.
508 */
509 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
510 class vector : protected _Vector_base<_Tp, _Alloc>
511 {
512#ifdef _GLIBCXX_CONCEPT_CHECKS
513 // Concept requirements.
514 typedef typename _Alloc::value_type _Alloc_value_type;
515# if __cplusplus < 201103L
516 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
517# endif
518 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
519#endif
520
521#if __cplusplus >= 201103L
522 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
523 "std::vector must have a non-const, non-volatile value_type");
524# if __cplusplus > 201703L || defined __STRICT_ANSI__
525 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
526 "std::vector must have the same value_type as its allocator");
527# endif
528#endif
529
530 typedef _Vector_base<_Tp, _Alloc> _Base;
531 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
532 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
533 typedef typename _Base::_Alloc_result _Alloc_result;
534
535 public:
536 typedef _Tp value_type;
537 typedef typename _Base::pointer pointer;
538 typedef typename _Alloc_traits::const_pointer const_pointer;
539 typedef typename _Alloc_traits::reference reference;
540 typedef typename _Alloc_traits::const_reference const_reference;
541 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
542 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
543 const_iterator;
544 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
545 typedef std::reverse_iterator<iterator> reverse_iterator;
546 typedef size_t size_type;
547 typedef ptrdiff_t difference_type;
548 typedef _Alloc allocator_type;
549
550 private:
551#if __cplusplus >= 201103L
552 static constexpr bool
553 _S_nothrow_relocate(true_type)
554 {
555 return noexcept(std::__relocate_a(std::declval<pointer>(),
556 std::declval<pointer>(),
557 std::declval<pointer>(),
558 std::declval<_Tp_alloc_type&>()));
559 }
560
561 static constexpr bool
562 _S_nothrow_relocate(false_type)
563 { return false; }
564
565 static constexpr bool
566 _S_use_relocate()
567 {
568 // Instantiating std::__relocate_a might cause an error outside the
569 // immediate context (in __relocate_object_a's noexcept-specifier),
570 // so only do it if we know the type can be move-inserted into *this.
571 return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{});
572 }
573
574 static _GLIBCXX20_CONSTEXPR pointer
575 _S_relocate(pointer __first, pointer __last, pointer __result,
576 _Tp_alloc_type& __alloc) noexcept
577 {
578#pragma GCC diagnostic push
579#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
580 if constexpr (_S_use_relocate())
581 return std::__relocate_a(__first, __last, __result, __alloc);
582 else
583 return __result;
584#pragma GCC diagnostic pop
585 }
586#endif // C++11
587
588 protected:
589 using _Base::_M_allocate;
590 using _Base::_M_allocate_at_least;
591 using _Base::_M_deallocate;
592 using _Base::_M_impl;
593 using _Base::_M_get_Tp_allocator;
594
595 public:
596 // [23.2.4.1] construct/copy/destroy
597 // (assign() and get_allocator() are also listed in this section)
598
599 /**
600 * @brief Creates a %vector with no elements.
601 */
602#if __cplusplus >= 201103L
603 vector() = default;
604#else
605 vector() { }
606#endif
607
608 /**
609 * @brief Creates a %vector with no elements.
610 * @param __a An allocator object.
611 */
612 explicit
613 _GLIBCXX20_CONSTEXPR
614 vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
615 : _Base(__a) { }
616
617#if __cplusplus >= 201103L
618 /**
619 * @brief Creates a %vector with default constructed elements.
620 * @param __n The number of elements to initially create.
621 * @param __a An allocator.
622 *
623 * This constructor fills the %vector with @a __n default
624 * constructed elements.
625 */
626 explicit
627 _GLIBCXX20_CONSTEXPR
628 vector(size_type __n, const allocator_type& __a = allocator_type())
629 : _Base(_S_check_init_len(__n, __a), __a)
630 { _M_default_initialize(__n); }
631
632 /**
633 * @brief Creates a %vector with copies of an exemplar element.
634 * @param __n The number of elements to initially create.
635 * @param __value An element to copy.
636 * @param __a An allocator.
637 *
638 * This constructor fills the %vector with @a __n copies of @a __value.
639 */
640 _GLIBCXX20_CONSTEXPR
641 vector(size_type __n, const value_type& __value,
642 const allocator_type& __a = allocator_type())
643 : _Base(_S_check_init_len(__n, __a), __a)
644 { _M_fill_initialize(__n, __value); }
645#else
646 /**
647 * @brief Creates a %vector with copies of an exemplar element.
648 * @param __n The number of elements to initially create.
649 * @param __value An element to copy.
650 * @param __a An allocator.
651 *
652 * This constructor fills the %vector with @a __n copies of @a __value.
653 */
654 explicit
655 vector(size_type __n, const value_type& __value = value_type(),
656 const allocator_type& __a = allocator_type())
657 : _Base(_S_check_init_len(__n, __a), __a)
658 { _M_fill_initialize(__n, __value); }
659#endif
660
661 /**
662 * @brief %Vector copy constructor.
663 * @param __x A %vector of identical element and allocator types.
664 *
665 * All the elements of @a __x are copied, but any unused capacity in
666 * @a __x will not be copied
667 * (i.e. capacity() == size() in the new %vector).
668 *
669 * The newly-created %vector uses a copy of the allocator object used
670 * by @a __x (unless the allocator traits dictate a different object).
671 */
672 _GLIBCXX20_CONSTEXPR
673 vector(const vector& __x)
674 : _Base(__x.size(),
675 _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
676 {
677 this->_M_impl._M_finish =
678 std::__uninitialized_copy_a(__x.begin(), __x.end(),
679 this->_M_impl._M_start,
680 _M_get_Tp_allocator());
681 }
682
683#if __cplusplus >= 201103L
684 /**
685 * @brief %Vector move constructor.
686 *
687 * The newly-created %vector contains the exact contents of the
688 * moved instance.
689 * The contents of the moved instance are a valid, but unspecified
690 * %vector.
691 */
692 vector(vector&&) noexcept = default;
693
694 /// Copy constructor with alternative allocator
695 _GLIBCXX20_CONSTEXPR
696 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
697 : _Base(__x.size(), __a)
698 {
699 this->_M_impl._M_finish =
700 std::__uninitialized_copy_a(__x.begin(), __x.end(),
701 this->_M_impl._M_start,
702 _M_get_Tp_allocator());
703 }
704
705 private:
706 _GLIBCXX20_CONSTEXPR
707 vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
708 : _Base(__m, std::move(__rv))
709 { }
710
711 _GLIBCXX20_CONSTEXPR
712 vector(vector&& __rv, const allocator_type& __m, false_type)
713 : _Base(__m)
714 {
715 if (__rv.get_allocator() == __m)
716 this->_M_impl._M_swap_data(__rv._M_impl);
717 else if (!__rv.empty())
718 {
719 this->_M_create_storage(__rv.size());
720 this->_M_impl._M_finish =
721 std::__uninitialized_move_a(__rv.begin(), __rv.end(),
722 this->_M_impl._M_start,
723 _M_get_Tp_allocator());
724 __rv.clear();
725 }
726 }
727
728 public:
729 /// Move constructor with alternative allocator
730 _GLIBCXX20_CONSTEXPR
731 vector(vector&& __rv, const __type_identity_t<allocator_type>& __m)
732 noexcept( noexcept(
733 vector(std::declval<vector&&>(), std::declval<const allocator_type&>(),
734 std::declval<typename _Alloc_traits::is_always_equal>())) )
735 : vector(std::move(__rv), __m, typename _Alloc_traits::is_always_equal{})
736 { }
737
738 /**
739 * @brief Builds a %vector from an initializer list.
740 * @param __l An initializer_list.
741 * @param __a An allocator.
742 *
743 * Create a %vector consisting of copies of the elements in the
744 * initializer_list @a __l.
745 *
746 * This will call the element type's copy constructor N times
747 * (where N is @a __l.size()) and do no memory reallocation.
748 */
749 _GLIBCXX20_CONSTEXPR
750 vector(initializer_list<value_type> __l,
751 const allocator_type& __a = allocator_type())
752 : _Base(__a)
753 {
754 _M_range_initialize_n(__l.begin(), __l.end(), __l.size());
755 }
756#endif
757
758 /**
759 * @brief Builds a %vector from a range.
760 * @param __first An input iterator.
761 * @param __last An input iterator.
762 * @param __a An allocator.
763 *
764 * Create a %vector consisting of copies of the elements from
765 * [first,last).
766 *
767 * If the iterators are forward, bidirectional, or
768 * random-access, then this will call the elements' copy
769 * constructor N times (where N is distance(first,last)) and do
770 * no memory reallocation. But if only input iterators are
771 * used, then this will do at most 2N calls to the copy
772 * constructor, and logN memory reallocations.
773 */
774#if __cplusplus >= 201103L
775 template<typename _InputIterator,
776 typename = std::_RequireInputIter<_InputIterator>>
777 _GLIBCXX20_CONSTEXPR
778 vector(_InputIterator __first, _InputIterator __last,
779 const allocator_type& __a = allocator_type())
780 : _Base(__a)
781 {
782#if __glibcxx_concepts // C++ >= C++20
783 if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>
784 || forward_iterator<_InputIterator>)
785 {
786 const auto __n
787 = static_cast<size_type>(ranges::distance(__first, __last));
788 _M_range_initialize_n(__first, __last, __n);
789 return;
790 }
791 else
792#endif
793 _M_range_initialize(__first, __last,
794 std::__iterator_category(__first));
795 }
796#else
797 template<typename _InputIterator>
798 vector(_InputIterator __first, _InputIterator __last,
799 const allocator_type& __a = allocator_type())
800 : _Base(__a)
801 {
802 // Check whether it's an integral type. If so, it's not an iterator.
803 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
804 _M_initialize_dispatch(__first, __last, _Integral());
805 }
806#endif
807
808#if __glibcxx_containers_ranges // C++ >= 23
809 /**
810 * @brief Construct a vector from a range.
811 * @param __rg A range of values that are convertible to `bool`.
812 * @since C++23
813 */
814 template<__detail::__container_compatible_range<_Tp> _Rg>
815 constexpr
816 vector(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
817 : vector(__a)
818 {
819 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
820 {
821 const auto __n = static_cast<size_type>(ranges::distance(__rg));
822 _M_range_initialize_n(ranges::begin(__rg), ranges::end(__rg),
823 __n);
824 }
825 else
826 {
827 auto __first = ranges::begin(__rg);
828 const auto __last = ranges::end(__rg);
829 for (; __first != __last; ++__first)
830 emplace_back(*__first);
831 }
832 }
833#endif
834
835 /**
836 * The dtor only erases the elements, and note that if the
837 * elements themselves are pointers, the pointed-to memory is
838 * not touched in any way. Managing the pointer is the user's
839 * responsibility.
840 */
841 _GLIBCXX20_CONSTEXPR
842 ~vector() _GLIBCXX_NOEXCEPT
843 {
844 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
845 _M_get_Tp_allocator());
846 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC;
847 }
848
849 /**
850 * @brief %Vector assignment operator.
851 * @param __x A %vector of identical element and allocator types.
852 *
853 * All the elements of @a __x are copied, but any unused capacity in
854 * @a __x will not be copied.
855 *
856 * Whether the allocator is copied depends on the allocator traits.
857 */
858 _GLIBCXX20_CONSTEXPR
859 vector&
860 operator=(const vector& __x);
861
862#if __cplusplus >= 201103L
863 /**
864 * @brief %Vector move assignment operator.
865 * @param __x A %vector of identical element and allocator types.
866 *
867 * The contents of @a __x are moved into this %vector (without copying,
868 * if the allocators permit it).
869 * Afterwards @a __x is a valid, but unspecified %vector.
870 *
871 * Whether the allocator is moved depends on the allocator traits.
872 */
873 _GLIBCXX20_CONSTEXPR
874 vector&
875 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
876 {
877 constexpr bool __move_storage =
878 _Alloc_traits::_S_propagate_on_move_assign()
879 || _Alloc_traits::_S_always_equal();
880 _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
881 return *this;
882 }
883
884 /**
885 * @brief %Vector list assignment operator.
886 * @param __l An initializer_list.
887 *
888 * This function fills a %vector with copies of the elements in the
889 * initializer list @a __l.
890 *
891 * Note that the assignment completely changes the %vector and
892 * that the resulting %vector's size is the same as the number
893 * of elements assigned.
894 */
895 _GLIBCXX20_CONSTEXPR
896 vector&
897 operator=(initializer_list<value_type> __l)
898 {
899 this->_M_assign_aux(__l.begin(), __l.end(),
900 random_access_iterator_tag());
901 return *this;
902 }
903#endif
904
905 /**
906 * @brief Assigns a given value to a %vector.
907 * @param __n Number of elements to be assigned.
908 * @param __val Value to be assigned.
909 *
910 * This function fills a %vector with @a __n copies of the given
911 * value. Note that the assignment completely changes the
912 * %vector and that the resulting %vector's size is the same as
913 * the number of elements assigned.
914 */
915 _GLIBCXX20_CONSTEXPR
916 void
917 assign(size_type __n, const value_type& __val)
918 { _M_fill_assign(__n, __val); }
919
920 /**
921 * @brief Assigns a range to a %vector.
922 * @param __first An input iterator.
923 * @param __last An input iterator.
924 *
925 * This function fills a %vector with copies of the elements in the
926 * range [__first,__last).
927 *
928 * Note that the assignment completely changes the %vector and
929 * that the resulting %vector's size is the same as the number
930 * of elements assigned.
931 */
932#if __cplusplus >= 201103L
933 template<typename _InputIterator,
934 typename = std::_RequireInputIter<_InputIterator>>
935 _GLIBCXX20_CONSTEXPR
936 void
937 assign(_InputIterator __first, _InputIterator __last)
938 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
939#else
940 template<typename _InputIterator>
941 void
942 assign(_InputIterator __first, _InputIterator __last)
943 {
944 // Check whether it's an integral type. If so, it's not an iterator.
945 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
946 _M_assign_dispatch(__first, __last, _Integral());
947 }
948#endif
949
950#if __cplusplus >= 201103L
951 /**
952 * @brief Assigns an initializer list to a %vector.
953 * @param __l An initializer_list.
954 *
955 * This function fills a %vector with copies of the elements in the
956 * initializer list @a __l.
957 *
958 * Note that the assignment completely changes the %vector and
959 * that the resulting %vector's size is the same as the number
960 * of elements assigned.
961 */
962 _GLIBCXX20_CONSTEXPR
963 void
964 assign(initializer_list<value_type> __l)
965 {
966 this->_M_assign_aux(__l.begin(), __l.end(),
967 random_access_iterator_tag());
968 }
969#endif
970
971#if __glibcxx_containers_ranges // C++ >= 23
972 /**
973 * @brief Assign a range to the vector.
974 * @param __rg A range of values that are convertible to `value_type`.
975 * @pre `__rg` and `*this` do not overlap.
976 * @since C++23
977 */
978 template<__detail::__container_compatible_range<_Tp> _Rg>
979 constexpr void
980 assign_range(_Rg&& __rg)
981 {
982 static_assert(assignable_from<_Tp&, ranges::range_reference_t<_Rg>>);
983
984 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
985 {
986 const auto __n = size_type(ranges::distance(__rg));
987 if (__n <= size())
988 {
989 auto __res = ranges::copy(__rg, this->_M_impl._M_start);
990 _M_erase_at_end(__res.out);
991 return;
992 }
993
994 reserve(__n);
995 auto __first = ranges::copy_n(ranges::begin(__rg), size(),
996 this->_M_impl._M_start).in;
997 [[maybe_unused]] const auto __diff = __n - size();
998 _GLIBCXX_ASAN_ANNOTATE_GROW(__diff);
999 _Base::_M_append_range(ranges::subrange(std::move(__first),
1000 ranges::end(__rg)));
1001 _GLIBCXX_ASAN_ANNOTATE_GREW(__diff);
1002 }
1003 else // input_range<_Rg> && !sized_range<_Rg>
1004 {
1005 auto __first = ranges::begin(__rg);
1006 const auto __last = ranges::end(__rg);
1007 pointer __ptr = this->_M_impl._M_start;
1008 pointer const __end = this->_M_impl._M_finish;
1009
1010 while (__ptr < __end && __first != __last)
1011 {
1012 *__ptr = *__first;
1013 ++__ptr;
1014 ++__first;
1015 }
1016
1017 if (__first == __last)
1018 _M_erase_at_end(__ptr);
1019 else
1020 {
1021 do
1022 emplace_back(*__first);
1023 while (++__first != __last);
1024 }
1025 }
1026 }
1027#endif // containers_ranges
1028
1029 /// Get a copy of the memory allocation object.
1030 using _Base::get_allocator;
1031
1032 // iterators
1033 /**
1034 * Returns a read/write iterator that points to the first
1035 * element in the %vector. Iteration is done in ordinary
1036 * element order.
1037 */
1038 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1039 iterator
1040 begin() _GLIBCXX_NOEXCEPT
1041 { return iterator(this->_M_impl._M_start); }
1042
1043 /**
1044 * Returns a read-only (constant) iterator that points to the
1045 * first element in the %vector. Iteration is done in ordinary
1046 * element order.
1047 */
1048 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1049 const_iterator
1050 begin() const _GLIBCXX_NOEXCEPT
1051 { return const_iterator(this->_M_impl._M_start); }
1052
1053 /**
1054 * Returns a read/write iterator that points one past the last
1055 * element in the %vector. Iteration is done in ordinary
1056 * element order.
1057 */
1058 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1059 iterator
1060 end() _GLIBCXX_NOEXCEPT
1061 { return iterator(this->_M_impl._M_finish); }
1062
1063 /**
1064 * Returns a read-only (constant) iterator that points one past
1065 * the last element in the %vector. Iteration is done in
1066 * ordinary element order.
1067 */
1068 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1069 const_iterator
1070 end() const _GLIBCXX_NOEXCEPT
1071 { return const_iterator(this->_M_impl._M_finish); }
1072
1073 /**
1074 * Returns a read/write reverse iterator that points to the
1075 * last element in the %vector. Iteration is done in reverse
1076 * element order.
1077 */
1078 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1079 reverse_iterator
1080 rbegin() _GLIBCXX_NOEXCEPT
1081 { return reverse_iterator(end()); }
1082
1083 /**
1084 * Returns a read-only (constant) reverse iterator that points
1085 * to the last element in the %vector. Iteration is done in
1086 * reverse element order.
1087 */
1088 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1089 const_reverse_iterator
1090 rbegin() const _GLIBCXX_NOEXCEPT
1091 { return const_reverse_iterator(end()); }
1092
1093 /**
1094 * Returns a read/write reverse iterator that points to one
1095 * before the first element in the %vector. Iteration is done
1096 * in reverse element order.
1097 */
1098 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1099 reverse_iterator
1100 rend() _GLIBCXX_NOEXCEPT
1101 { return reverse_iterator(begin()); }
1102
1103 /**
1104 * Returns a read-only (constant) reverse iterator that points
1105 * to one before the first element in the %vector. Iteration
1106 * is done in reverse element order.
1107 */
1108 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1109 const_reverse_iterator
1110 rend() const _GLIBCXX_NOEXCEPT
1111 { return const_reverse_iterator(begin()); }
1112
1113#if __cplusplus >= 201103L
1114 /**
1115 * Returns a read-only (constant) iterator that points to the
1116 * first element in the %vector. Iteration is done in ordinary
1117 * element order.
1118 */
1119 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1120 const_iterator
1121 cbegin() const noexcept
1122 { return const_iterator(this->_M_impl._M_start); }
1123
1124 /**
1125 * Returns a read-only (constant) iterator that points one past
1126 * the last element in the %vector. Iteration is done in
1127 * ordinary element order.
1128 */
1129 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1130 const_iterator
1131 cend() const noexcept
1132 { return const_iterator(this->_M_impl._M_finish); }
1133
1134 /**
1135 * Returns a read-only (constant) reverse iterator that points
1136 * to the last element in the %vector. Iteration is done in
1137 * reverse element order.
1138 */
1139 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1140 const_reverse_iterator
1141 crbegin() const noexcept
1142 { return const_reverse_iterator(end()); }
1143
1144 /**
1145 * Returns a read-only (constant) reverse iterator that points
1146 * to one before the first element in the %vector. Iteration
1147 * is done in reverse element order.
1148 */
1149 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1150 const_reverse_iterator
1151 crend() const noexcept
1152 { return const_reverse_iterator(begin()); }
1153#endif
1154
1155 // [23.2.4.2] capacity
1156 /** Returns the number of elements in the %vector. */
1157 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1158 size_type
1159 size() const _GLIBCXX_NOEXCEPT
1160 {
1161 ptrdiff_t __dif = this->_M_impl._M_finish - this->_M_impl._M_start;
1162 if (__dif < 0)
1163 __builtin_unreachable();
1164 return size_type(__dif);
1165 }
1166
1167 /** Returns the size() of the largest possible %vector. */
1168 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1169 size_type
1170 max_size() const _GLIBCXX_NOEXCEPT
1171 { return _Base::_S_max_size(_M_get_Tp_allocator()); }
1172
1173#if __cplusplus >= 201103L
1174 /**
1175 * @brief Resizes the %vector to the specified number of elements.
1176 * @param __new_size Number of elements the %vector should contain.
1177 *
1178 * This function will %resize the %vector to the specified
1179 * number of elements. If the number is smaller than the
1180 * %vector's current size the %vector is truncated, otherwise
1181 * default constructed elements are appended.
1182 */
1183 _GLIBCXX20_CONSTEXPR
1184 void
1185 resize(size_type __new_size)
1186 {
1187 if (__new_size > size())
1188 _M_default_append(__new_size - size());
1189 else if (__new_size < size())
1190 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1191 }
1192
1193 /**
1194 * @brief Resizes the %vector to the specified number of elements.
1195 * @param __new_size Number of elements the %vector should contain.
1196 * @param __x Data with which new elements should be populated.
1197 *
1198 * This function will %resize the %vector to the specified
1199 * number of elements. If the number is smaller than the
1200 * %vector's current size the %vector is truncated, otherwise
1201 * the %vector is extended and new elements are populated with
1202 * given data.
1203 */
1204 _GLIBCXX20_CONSTEXPR
1205 void
1206 resize(size_type __new_size, const value_type& __x)
1207 {
1208 if (__new_size > size())
1209 _M_fill_append(__new_size - size(), __x);
1210 else if (__new_size < size())
1211 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1212 }
1213#else
1214 /**
1215 * @brief Resizes the %vector to the specified number of elements.
1216 * @param __new_size Number of elements the %vector should contain.
1217 * @param __x Data with which new elements should be populated.
1218 *
1219 * This function will %resize the %vector to the specified
1220 * number of elements. If the number is smaller than the
1221 * %vector's current size the %vector is truncated, otherwise
1222 * the %vector is extended and new elements are populated with
1223 * given data.
1224 */
1225 _GLIBCXX20_CONSTEXPR
1226 void
1227 resize(size_type __new_size, value_type __x = value_type())
1228 {
1229 if (__new_size > size())
1230 _M_fill_append(__new_size - size(), __x);
1231 else if (__new_size < size())
1232 _M_erase_at_end(this->_M_impl._M_start + __new_size);
1233 }
1234#endif
1235
1236#if __cplusplus >= 201103L
1237 /** A non-binding request to reduce capacity() to size(). */
1238 _GLIBCXX20_CONSTEXPR
1239 void
1240 shrink_to_fit()
1241 { _M_shrink_to_fit(); }
1242#endif
1243
1244 /**
1245 * Returns the total number of elements that the %vector can
1246 * hold before needing to allocate more memory.
1247 */
1248 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1249 size_type
1250 capacity() const _GLIBCXX_NOEXCEPT
1251 {
1252 ptrdiff_t __dif = this->_M_impl._M_end_of_storage
1253 - this->_M_impl._M_start;
1254 if (__dif < 0)
1255 __builtin_unreachable();
1256 return size_type(__dif);
1257 }
1258
1259 /**
1260 * Returns true if the %vector is empty. (Thus begin() would
1261 * equal end().)
1262 */
1263 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1264 bool
1265 empty() const _GLIBCXX_NOEXCEPT
1266 { return begin() == end(); }
1267
1268 /**
1269 * @brief Attempt to preallocate enough memory for specified number of
1270 * elements.
1271 * @param __n Number of elements required.
1272 * @throw std::length_error If @a n exceeds @c max_size().
1273 *
1274 * This function attempts to reserve enough memory for the
1275 * %vector to hold the specified number of elements. If the
1276 * number requested is more than max_size(), length_error is
1277 * thrown.
1278 *
1279 * The advantage of this function is that if optimal code is a
1280 * necessity and the user can determine the number of elements
1281 * that will be required, the user can reserve the memory in
1282 * %advance, and thus prevent a possible reallocation of memory
1283 * and copying of %vector data.
1284 */
1285 _GLIBCXX20_CONSTEXPR
1286 void
1287 reserve(size_type __n);
1288
1289 // element access
1290 /**
1291 * @brief Subscript access to the data contained in the %vector.
1292 * @param __n The index of the element for which data should be
1293 * accessed.
1294 * @return Read/write reference to data.
1295 *
1296 * This operator allows for easy, array-style, data access.
1297 * Note that data access with this operator is unchecked and
1298 * out_of_range lookups are not defined. (For checked lookups
1299 * see at().)
1300 */
1301 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1302 reference
1303 operator[](size_type __n) _GLIBCXX_NOEXCEPT
1304 {
1305 __glibcxx_requires_subscript(__n);
1306 return *(this->_M_impl._M_start + __n);
1307 }
1308
1309 /**
1310 * @brief Subscript access to the data contained in the %vector.
1311 * @param __n The index of the element for which data should be
1312 * accessed.
1313 * @return Read-only (constant) reference to data.
1314 *
1315 * This operator allows for easy, array-style, data access.
1316 * Note that data access with this operator is unchecked and
1317 * out_of_range lookups are not defined. (For checked lookups
1318 * see at().)
1319 */
1320 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1321 const_reference
1322 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1323 {
1324 __glibcxx_requires_subscript(__n);
1325 return *(this->_M_impl._M_start + __n);
1326 }
1327
1328 protected:
1329 /// Safety check used only from at().
1330 _GLIBCXX20_CONSTEXPR
1331 void
1332 _M_range_check(size_type __n) const
1333 {
1334 if (__n >= this->size())
1335 __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
1336 "(which is %zu) >= this->size() "
1337 "(which is %zu)"),
1338 __n, this->size());
1339 }
1340
1341 public:
1342 /**
1343 * @brief Provides access to the data contained in the %vector.
1344 * @param __n The index of the element for which data should be
1345 * accessed.
1346 * @return Read/write reference to data.
1347 * @throw std::out_of_range If @a __n is an invalid index.
1348 *
1349 * This function provides for safer data access. The parameter
1350 * is first checked that it is in the range of the vector. The
1351 * function throws out_of_range if the check fails.
1352 */
1353 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1354 reference
1355 at(size_type __n)
1356 {
1357 _M_range_check(__n);
1358 return (*this)[__n];
1359 }
1360
1361 /**
1362 * @brief Provides access to the data contained in the %vector.
1363 * @param __n The index of the element for which data should be
1364 * accessed.
1365 * @return Read-only (constant) reference to data.
1366 * @throw std::out_of_range If @a __n is an invalid index.
1367 *
1368 * This function provides for safer data access. The parameter
1369 * is first checked that it is in the range of the vector. The
1370 * function throws out_of_range if the check fails.
1371 */
1372 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1373 const_reference
1374 at(size_type __n) const
1375 {
1376 _M_range_check(__n);
1377 return (*this)[__n];
1378 }
1379
1380 /**
1381 * Returns a read/write reference to the data at the first
1382 * element of the %vector.
1383 */
1384 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1385 reference
1386 front() _GLIBCXX_NOEXCEPT
1387 {
1388 __glibcxx_requires_nonempty();
1389 return *begin();
1390 }
1391
1392 /**
1393 * Returns a read-only (constant) reference to the data at the first
1394 * element of the %vector.
1395 */
1396 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1397 const_reference
1398 front() const _GLIBCXX_NOEXCEPT
1399 {
1400 __glibcxx_requires_nonempty();
1401 return *begin();
1402 }
1403
1404 /**
1405 * Returns a read/write reference to the data at the last
1406 * element of the %vector.
1407 */
1408 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1409 reference
1410 back() _GLIBCXX_NOEXCEPT
1411 {
1412 __glibcxx_requires_nonempty();
1413 return *(end() - 1);
1414 }
1415
1416 /**
1417 * Returns a read-only (constant) reference to the data at the
1418 * last element of the %vector.
1419 */
1420 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1421 const_reference
1422 back() const _GLIBCXX_NOEXCEPT
1423 {
1424 __glibcxx_requires_nonempty();
1425 return *(end() - 1);
1426 }
1427
1428 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1429 // DR 464. Suggestion for new member functions in standard containers.
1430 // data access
1431 /**
1432 * Returns a pointer such that [data(), data() + size()) is a valid
1433 * range. For a non-empty %vector, data() == &front().
1434 */
1435 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1436 _Tp*
1437 data() _GLIBCXX_NOEXCEPT
1438 { return _M_data_ptr(this->_M_impl._M_start); }
1439
1440 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1441 const _Tp*
1442 data() const _GLIBCXX_NOEXCEPT
1443 { return _M_data_ptr(this->_M_impl._M_start); }
1444
1445 // [23.2.4.3] modifiers
1446 /**
1447 * @brief Add data to the end of the %vector.
1448 * @param __x Data to be added.
1449 *
1450 * This is a typical stack operation. The function creates an
1451 * element at the end of the %vector and assigns the given data
1452 * to it. Due to the nature of a %vector this operation can be
1453 * done in constant time if the %vector has preallocated space
1454 * available.
1455 */
1456 _GLIBCXX20_CONSTEXPR
1457 void
1458 push_back(const value_type& __x)
1459 {
1460 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
1461 {
1462 _GLIBCXX_ASAN_ANNOTATE_GROW(1);
1463 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
1464 __x);
1465 ++this->_M_impl._M_finish;
1466 _GLIBCXX_ASAN_ANNOTATE_GREW(1);
1467 }
1468 else
1469 _M_realloc_append(__x);
1470 }
1471
1472#if __cplusplus >= 201103L
1473 _GLIBCXX20_CONSTEXPR
1474 void
1475 push_back(value_type&& __x)
1476 { emplace_back(std::move(__x)); }
1477
1478 template<typename... _Args>
1479#if __cplusplus > 201402L
1480 _GLIBCXX20_CONSTEXPR
1481 reference
1482#else
1483 void
1484#endif
1485 emplace_back(_Args&&... __args);
1486#endif
1487
1488 /**
1489 * @brief Removes last element.
1490 *
1491 * This is a typical stack operation. It shrinks the %vector by one.
1492 *
1493 * Note that no data is returned, and if the last element's
1494 * data is needed, it should be retrieved before pop_back() is
1495 * called.
1496 */
1497 _GLIBCXX20_CONSTEXPR
1498 void
1499 pop_back() _GLIBCXX_NOEXCEPT
1500 {
1501 __glibcxx_requires_nonempty();
1502 --this->_M_impl._M_finish;
1503 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
1504 _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
1505 }
1506
1507#if __cplusplus >= 201103L
1508 /**
1509 * @brief Inserts an object in %vector before specified iterator.
1510 * @param __position A const_iterator into the %vector.
1511 * @param __args Arguments.
1512 * @return An iterator that points to the inserted data.
1513 *
1514 * This function will insert an object of type T constructed
1515 * with T(std::forward<Args>(args)...) before the specified location.
1516 * Note that this kind of operation could be expensive for a %vector
1517 * and if it is frequently used the user should consider using
1518 * std::list.
1519 */
1520 template<typename... _Args>
1521 _GLIBCXX20_CONSTEXPR
1522 iterator
1523 emplace(const_iterator __position, _Args&&... __args)
1524 { return _M_emplace_aux(__position, std::forward<_Args>(__args)...); }
1525
1526 /**
1527 * @brief Inserts given value into %vector before specified iterator.
1528 * @param __position A const_iterator into the %vector.
1529 * @param __x Data to be inserted.
1530 * @return An iterator that points to the inserted data.
1531 *
1532 * This function will insert a copy of the given value before
1533 * the specified location. Note that this kind of operation
1534 * could be expensive for a %vector and if it is frequently
1535 * used the user should consider using std::list.
1536 */
1537 _GLIBCXX20_CONSTEXPR
1538 iterator
1539 insert(const_iterator __position, const value_type& __x);
1540#else
1541 /**
1542 * @brief Inserts given value into %vector before specified iterator.
1543 * @param __position An iterator into the %vector.
1544 * @param __x Data to be inserted.
1545 * @return An iterator that points to the inserted data.
1546 *
1547 * This function will insert a copy of the given value before
1548 * the specified location. Note that this kind of operation
1549 * could be expensive for a %vector and if it is frequently
1550 * used the user should consider using std::list.
1551 */
1552 iterator
1553 insert(iterator __position, const value_type& __x);
1554#endif
1555
1556#if __cplusplus >= 201103L
1557 /**
1558 * @brief Inserts given rvalue into %vector before specified iterator.
1559 * @param __position A const_iterator into the %vector.
1560 * @param __x Data to be inserted.
1561 * @return An iterator that points to the inserted data.
1562 *
1563 * This function will insert a copy of the given rvalue before
1564 * the specified location. Note that this kind of operation
1565 * could be expensive for a %vector and if it is frequently
1566 * used the user should consider using std::list.
1567 */
1568 _GLIBCXX20_CONSTEXPR
1569 iterator
1570 insert(const_iterator __position, value_type&& __x)
1571 { return _M_insert_rval(__position, std::move(__x)); }
1572
1573 /**
1574 * @brief Inserts an initializer_list into the %vector.
1575 * @param __position An iterator into the %vector.
1576 * @param __l An initializer_list.
1577 *
1578 * This function will insert copies of the data in the
1579 * initializer_list @a l into the %vector before the location
1580 * specified by @a position.
1581 *
1582 * Note that this kind of operation could be expensive for a
1583 * %vector and if it is frequently used the user should
1584 * consider using std::list.
1585 */
1586 _GLIBCXX20_CONSTEXPR
1587 iterator
1588 insert(const_iterator __position, initializer_list<value_type> __l)
1589 {
1590 auto __offset = __position - cbegin();
1591 _M_range_insert(begin() + __offset, __l.begin(), __l.end(),
1592 std::random_access_iterator_tag());
1593 return begin() + __offset;
1594 }
1595#endif
1596
1597#if __cplusplus >= 201103L
1598 /**
1599 * @brief Inserts a number of copies of given data into the %vector.
1600 * @param __position A const_iterator into the %vector.
1601 * @param __n Number of elements to be inserted.
1602 * @param __x Data to be inserted.
1603 * @return An iterator that points to the inserted data.
1604 *
1605 * This function will insert a specified number of copies of
1606 * the given data before the location specified by @a position.
1607 *
1608 * Note that this kind of operation could be expensive for a
1609 * %vector and if it is frequently used the user should
1610 * consider using std::list.
1611 */
1612 _GLIBCXX20_CONSTEXPR
1613 iterator
1614 insert(const_iterator __position, size_type __n, const value_type& __x)
1615 {
1616 difference_type __offset = __position - cbegin();
1617 _M_fill_insert(begin() + __offset, __n, __x);
1618 return begin() + __offset;
1619 }
1620#else
1621 /**
1622 * @brief Inserts a number of copies of given data into the %vector.
1623 * @param __position An iterator into the %vector.
1624 * @param __n Number of elements to be inserted.
1625 * @param __x Data to be inserted.
1626 *
1627 * This function will insert a specified number of copies of
1628 * the given data before the location specified by @a position.
1629 *
1630 * Note that this kind of operation could be expensive for a
1631 * %vector and if it is frequently used the user should
1632 * consider using std::list.
1633 */
1634 void
1635 insert(iterator __position, size_type __n, const value_type& __x)
1636 { _M_fill_insert(__position, __n, __x); }
1637#endif
1638
1639#if __cplusplus >= 201103L
1640 /**
1641 * @brief Inserts a range into the %vector.
1642 * @param __position A const_iterator into the %vector.
1643 * @param __first An input iterator.
1644 * @param __last An input iterator.
1645 * @return An iterator that points to the inserted data.
1646 *
1647 * This function will insert copies of the data in the range
1648 * [__first,__last) into the %vector before the location specified
1649 * by @a pos.
1650 *
1651 * Note that this kind of operation could be expensive for a
1652 * %vector and if it is frequently used the user should
1653 * consider using std::list.
1654 */
1655 template<typename _InputIterator,
1656 typename = std::_RequireInputIter<_InputIterator>>
1657 _GLIBCXX20_CONSTEXPR
1658 iterator
1659 insert(const_iterator __position, _InputIterator __first,
1660 _InputIterator __last)
1661 {
1662 difference_type __offset = __position - cbegin();
1663 _M_range_insert(begin() + __offset, __first, __last,
1664 std::__iterator_category(__first));
1665 return begin() + __offset;
1666 }
1667#else
1668 /**
1669 * @brief Inserts a range into the %vector.
1670 * @param __position An iterator into the %vector.
1671 * @param __first An input iterator.
1672 * @param __last An input iterator.
1673 *
1674 * This function will insert copies of the data in the range
1675 * [__first,__last) into the %vector before the location specified
1676 * by @a pos.
1677 *
1678 * Note that this kind of operation could be expensive for a
1679 * %vector and if it is frequently used the user should
1680 * consider using std::list.
1681 */
1682 template<typename _InputIterator>
1683 void
1684 insert(iterator __position, _InputIterator __first,
1685 _InputIterator __last)
1686 {
1687 // Check whether it's an integral type. If so, it's not an iterator.
1688 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1689 _M_insert_dispatch(__position, __first, __last, _Integral());
1690 }
1691#endif
1692
1693#if __glibcxx_containers_ranges // C++ >= 23
1694 /**
1695 * @brief Insert a range into the vector.
1696 * @param __rg A range of values that are convertible to `value_type`.
1697 * @return An iterator that points to the first new element inserted,
1698 * or to `__pos` if `__rg` is an empty range.
1699 * @pre `__rg` and `*this` do not overlap.
1700 * @since C++23
1701 */
1702 template<__detail::__container_compatible_range<_Tp> _Rg>
1703 constexpr iterator
1704 insert_range(const_iterator __pos, _Rg&& __rg);
1705
1706 /**
1707 * @brief Append a range at the end of the vector.
1708 * @param __rg A range of values that are convertible to `value_type`.
1709 * @since C++23
1710 */
1711 template<__detail::__container_compatible_range<_Tp> _Rg>
1712 constexpr void
1713 append_range(_Rg&& __rg)
1714 {
1715 // N.B. __rg may overlap with *this, so we must copy from __rg before
1716 // existing elements or iterators referring to *this are invalidated.
1717 // e.g. in v.append_range(views::concat(v, foo)) rg overlaps v.
1718 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1719 {
1720 const auto __n = size_type(ranges::distance(__rg));
1721
1722 // If there is no existing storage, there are no iterators that
1723 // can be referring to our storage, so it's safe to allocate now.
1724 if (capacity() == 0)
1725 reserve(__n);
1726
1727 const auto __sz = size();
1728 const auto __capacity = capacity();
1729 if ((__capacity - __sz) >= __n)
1730 {
1731 _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
1732 _Base::_M_append_range(__rg);
1733 _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
1734 return;
1735 }
1736
1737 const size_type __ask = _M_check_len(__n, "vector::append_range");
1738
1739 pointer __old_start = this->_M_impl._M_start;
1740 pointer __old_finish = this->_M_impl._M_finish;
1741
1742 auto [__ptr, __got] = this->_M_allocate_at_least(__ask);
1743 const pointer __start = __ptr;
1744 const pointer __mid = __start + __sz;
1745 const pointer __back = __mid + __n;
1746 _Guard_alloc __guard(__start, __got, *this);
1747 allocator_type& __a = _M_get_Tp_allocator();
1748 std::__uninitialized_copy_a(ranges::begin(__rg),
1749 ranges::end(__rg),
1750 __mid, __a);
1751
1752 if constexpr (_S_use_relocate())
1753 _S_relocate(__old_start, __old_finish, __start, __a);
1754 else
1755 {
1756 // RAII type to destroy initialized elements.
1757 struct _Guard_elts
1758 {
1759 pointer _M_first, _M_last; // Elements to destroy
1760 _Tp_alloc_type& _M_alloc;
1761
1762 constexpr
1763 _Guard_elts(pointer __f, pointer __l, _Tp_alloc_type& __a)
1764 : _M_first(__f), _M_last(__l), _M_alloc(__a)
1765 { }
1766
1767 constexpr
1768 ~_Guard_elts()
1769 { std::_Destroy(_M_first, _M_last, _M_alloc); }
1770
1771 _Guard_elts(_Guard_elts&&) = delete;
1772 };
1773 _Guard_elts __guard_elts{__mid, __back, __a};
1774
1775 std::__uninitialized_move_a(__old_start, __old_finish,
1776 __start, __a);
1777
1778 // Let old elements get destroyed by __guard_elts:
1779 __guard_elts._M_first = __old_start;
1780 __guard_elts._M_last = __old_finish;
1781 }
1782
1783 // Now give ownership of old storage to __guard:
1784 __guard._M_storage = __old_start;
1785 __guard._M_len = __capacity;
1786 // Finally, take ownership of new storage:
1787 this->_M_impl._M_start = __start;
1788 this->_M_impl._M_finish = __back;
1789 this->_M_impl._M_end_of_storage = __start + __got;
1790 }
1791 else
1792 {
1793 auto __first = ranges::begin(__rg);
1794 const auto __last = ranges::end(__rg);
1795
1796 // Fill up to the end of current capacity.
1797 for (auto __free = capacity() - size();
1798 __first != __last && __free > 0;
1799 ++__first, (void) --__free)
1800 emplace_back(*__first);
1801
1802 if (__first == __last)
1803 return;
1804
1805 // Copy the rest of the range to a new vector.
1806 vector __tmp(_M_get_Tp_allocator());
1807 for (; __first != __last; ++__first)
1808 __tmp.emplace_back(*__first);
1809 reserve(_M_check_len(__tmp.size(), "vector::append_range"));
1810 ranges::subrange __r(std::make_move_iterator(__tmp.begin()),
1811 std::make_move_iterator(__tmp.end()));
1812 append_range(__r); // This will take the fast path above.
1813 }
1814 }
1815#endif // containers_ranges
1816
1817 /**
1818 * @brief Remove element at given position.
1819 * @param __position Iterator pointing to element to be erased.
1820 * @return An iterator pointing to the next element (or end()).
1821 *
1822 * This function will erase the element at the given position and thus
1823 * shorten the %vector by one.
1824 *
1825 * Note This operation could be expensive and if it is
1826 * frequently used the user should consider using std::list.
1827 * The user is also cautioned that this function only erases
1828 * the element, and that if the element is itself a pointer,
1829 * the pointed-to memory is not touched in any way. Managing
1830 * the pointer is the user's responsibility.
1831 */
1832 _GLIBCXX20_CONSTEXPR
1833 iterator
1834#if __cplusplus >= 201103L
1835 erase(const_iterator __position)
1836 { return _M_erase(begin() + (__position - cbegin())); }
1837#else
1838 erase(iterator __position)
1839 { return _M_erase(__position); }
1840#endif
1841
1842 /**
1843 * @brief Remove a range of elements.
1844 * @param __first Iterator pointing to the first element to be erased.
1845 * @param __last Iterator pointing to one past the last element to be
1846 * erased.
1847 * @return An iterator pointing to the element pointed to by @a __last
1848 * prior to erasing (or end()).
1849 *
1850 * This function will erase the elements in the range
1851 * [__first,__last) and shorten the %vector accordingly.
1852 *
1853 * Note This operation could be expensive and if it is
1854 * frequently used the user should consider using std::list.
1855 * The user is also cautioned that this function only erases
1856 * the elements, and that if the elements themselves are
1857 * pointers, the pointed-to memory is not touched in any way.
1858 * Managing the pointer is the user's responsibility.
1859 */
1860 _GLIBCXX20_CONSTEXPR
1861 iterator
1862#if __cplusplus >= 201103L
1863 erase(const_iterator __first, const_iterator __last)
1864 {
1865 const auto __beg = begin();
1866 const auto __cbeg = cbegin();
1867 return _M_erase(__beg + (__first - __cbeg), __beg + (__last - __cbeg));
1868 }
1869#else
1870 erase(iterator __first, iterator __last)
1871 { return _M_erase(__first, __last); }
1872#endif
1873
1874 /**
1875 * @brief Swaps data with another %vector.
1876 * @param __x A %vector of the same element and allocator types.
1877 *
1878 * This exchanges the elements between two vectors in constant time.
1879 * (Three pointers, so it should be quite fast.)
1880 * Note that the global std::swap() function is specialized such that
1881 * std::swap(v1,v2) will feed to this function.
1882 *
1883 * Whether the allocators are swapped depends on the allocator traits.
1884 */
1885 _GLIBCXX20_CONSTEXPR
1886 void
1887 swap(vector& __x) _GLIBCXX_NOEXCEPT
1888 {
1889#if __cplusplus >= 201103L
1890 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1891 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1892#endif
1893 this->_M_impl._M_swap_data(__x._M_impl);
1894 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1895 __x._M_get_Tp_allocator());
1896 }
1897
1898 /**
1899 * Erases all the elements. Note that this function only erases the
1900 * elements, and that if the elements themselves are pointers, the
1901 * pointed-to memory is not touched in any way. Managing the pointer is
1902 * the user's responsibility.
1903 */
1904 _GLIBCXX20_CONSTEXPR
1905 void
1906 clear() _GLIBCXX_NOEXCEPT
1907 { _M_erase_at_end(this->_M_impl._M_start); }
1908
1909 private:
1910 // RAII guard for allocated storage.
1911 struct _Guard_alloc
1912 {
1913 pointer _M_storage; // Storage to deallocate
1914 size_type _M_len;
1915 _Base& _M_vect;
1916
1917 _GLIBCXX20_CONSTEXPR
1918 _Guard_alloc(pointer __s, size_type __l, _Base& __vect)
1919 : _M_storage(__s), _M_len(__l), _M_vect(__vect)
1920 { }
1921
1922 _GLIBCXX20_CONSTEXPR
1923 ~_Guard_alloc()
1924 {
1925 if (_M_storage)
1926 _M_vect._M_deallocate(_M_storage, _M_len);
1927 }
1928
1929 _GLIBCXX20_CONSTEXPR
1930 pointer
1931 _M_release()
1932 {
1933 pointer __res = _M_storage;
1934 _M_storage = pointer();
1935 return __res;
1936 }
1937
1938 private:
1939 _Guard_alloc(const _Guard_alloc&);
1940 };
1941
1942 protected:
1943 /**
1944 * Memory expansion handler. Uses the member allocation function to
1945 * obtain at least `n` objects worth of memory, copies `[first,last)`
1946 * into it, reports what was actually allocated.
1947 */
1948 template<typename _ForwardIterator>
1949 _GLIBCXX20_CONSTEXPR
1950 _Alloc_result
1951 _M_allocate_and_copy(size_type __n,
1952 _ForwardIterator __first, _ForwardIterator __last)
1953 {
1954 _Alloc_result __r = this->_M_allocate_at_least(__n);
1955 _Guard_alloc __guard(__r.__ptr, __r.__count, *this);
1956 std::__uninitialized_copy_a
1957 (__first, __last, __guard._M_storage, _M_get_Tp_allocator());
1958 (void) __guard._M_release();
1959 return __r;
1960 }
1961
1962 _GLIBCXX20_CONSTEXPR void
1963 _M_replace_storage(pointer __start, pointer __end, size_type __cap)
1964 {
1965 _GLIBCXX_ASAN_ANNOTATE_REINIT;
1966 _M_deallocate(this->_M_impl._M_start,
1967 this->_M_impl._M_end_of_storage - this->_M_impl._M_start);
1968 this->_M_impl._M_start = __start;
1969 this->_M_impl._M_finish = __end;
1970 this->_M_impl._M_end_of_storage = __start + __cap;
1971 }
1972
1973 template<typename _ForwardIterator>
1974 _GLIBCXX20_CONSTEXPR
1975 void
1976 _M_replace_with(size_type __n,
1977 _ForwardIterator __first, _ForwardIterator __last)
1978 {
1979 _Alloc_result __r = _M_allocate_and_copy(__n, __first, __last);
1980 std::_Destroy(this->_M_impl._M_start,
1981 this->_M_impl._M_finish, _M_get_Tp_allocator());
1982 _M_replace_storage(__r.__ptr, __r.__ptr + __n, __r.__count);
1983 }
1984
1985 // Internal constructor functions follow.
1986
1987 // Called by the range constructor to implement [23.1.1]/9
1988
1989#if __cplusplus < 201103L
1990 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1991 // 438. Ambiguity in the "do the right thing" clause
1992 template<typename _Integer>
1993 void
1994 _M_initialize_dispatch(_Integer __int_n, _Integer __value, __true_type)
1995 {
1996 const size_type __n = static_cast<size_type>(__int_n);
1997 // NB this is c++98 code, so has no allocate_at_least.
1998 pointer __start =
1999 _M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator()));
2000 this->_M_impl._M_start = __start;
2001 this->_M_impl._M_end_of_storage = __start + __n;
2002 _M_fill_initialize(__n, __value);
2003 }
2004
2005 // Called by the range constructor to implement [23.1.1]/9
2006 template<typename _InputIterator>
2007 void
2008 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
2009 __false_type)
2010 {
2011 _M_range_initialize(__first, __last,
2012 std::__iterator_category(__first));
2013 }
2014#endif
2015
2016 // Called by the second initialize_dispatch above
2017 template<typename _InputIterator>
2018 _GLIBCXX20_CONSTEXPR
2019 void
2020 _M_range_initialize(_InputIterator __first, _InputIterator __last,
2021 std::input_iterator_tag)
2022 {
2023 __try {
2024 for (; __first != __last; ++__first)
2025#if __cplusplus >= 201103L
2026 emplace_back(*__first);
2027#else
2028 push_back(*__first);
2029#endif
2030 } __catch(...) {
2031 clear();
2032 __throw_exception_again;
2033 }
2034 }
2035
2036 // Called by the second initialize_dispatch above
2037 template<typename _ForwardIterator>
2038 _GLIBCXX20_CONSTEXPR
2039 void
2040 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
2041 std::forward_iterator_tag)
2042 {
2043 _M_range_initialize_n(__first, __last,
2044 std::distance(__first, __last));
2045 }
2046
2047 template<typename _Iterator, typename _Sentinel>
2048 _GLIBCXX20_CONSTEXPR
2049 void
2050 _M_range_initialize_n(_Iterator __first, _Sentinel __last,
2051 size_type __n)
2052 {
2053 _Alloc_result __r = this->_M_allocate_at_least(
2054 _S_check_init_len(__n, _M_get_Tp_allocator()));
2055 pointer __start = __r.__ptr;
2056 this->_M_impl._M_start = this->_M_impl._M_finish = __start;
2057 this->_M_impl._M_end_of_storage = __start + __r.__count;
2058 this->_M_impl._M_finish
2059 = std::__uninitialized_copy_a(_GLIBCXX_MOVE(__first), __last,
2060 __start, _M_get_Tp_allocator());
2061 }
2062
2063 // Called by the first initialize_dispatch above and by the
2064 // vector(n,value,a) constructor.
2065 _GLIBCXX20_CONSTEXPR
2066 void
2067 _M_fill_initialize(size_type __n, const value_type& __value)
2068 {
2069 this->_M_impl._M_finish =
2070 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
2071 _M_get_Tp_allocator());
2072 }
2073
2074#if __cplusplus >= 201103L
2075 // Called by the vector(n) constructor.
2076 _GLIBCXX20_CONSTEXPR
2077 void
2078 _M_default_initialize(size_type __n)
2079 {
2080 this->_M_impl._M_finish =
2081 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
2082 _M_get_Tp_allocator());
2083 }
2084#endif
2085
2086 // Internal assign functions follow. The *_aux functions do the actual
2087 // assignment work for the range versions.
2088
2089 // Called by the range assign to implement [23.1.1]/9
2090
2091 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2092 // 438. Ambiguity in the "do the right thing" clause
2093 template<typename _Integer>
2094 _GLIBCXX20_CONSTEXPR
2095 void
2096 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
2097 { _M_fill_assign(__n, __val); }
2098
2099 // Called by the range assign to implement [23.1.1]/9
2100 template<typename _InputIterator>
2101 _GLIBCXX20_CONSTEXPR
2102 void
2103 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
2104 __false_type)
2105 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
2106
2107 // Called by the second assign_dispatch above
2108 template<typename _InputIterator>
2109 _GLIBCXX20_CONSTEXPR
2110 void
2111 _M_assign_aux(_InputIterator __first, _InputIterator __last,
2112 std::input_iterator_tag);
2113
2114 // Called by the second assign_dispatch above
2115 template<typename _ForwardIterator>
2116 _GLIBCXX20_CONSTEXPR
2117 void
2118 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
2119 std::forward_iterator_tag);
2120
2121 // Called by assign(n,t), and the range assign when it turns out
2122 // to be the same thing.
2123 _GLIBCXX20_CONSTEXPR
2124 void
2125 _M_fill_assign(size_type __n, const value_type& __val);
2126
2127 // Internal insert functions follow.
2128
2129 // Called by the range insert to implement [23.1.1]/9
2130
2131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2132 // 438. Ambiguity in the "do the right thing" clause
2133 template<typename _Integer>
2134 _GLIBCXX20_CONSTEXPR
2135 void
2136 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
2137 __true_type)
2138 { _M_fill_insert(__pos, __n, __val); }
2139
2140 // Called by the range insert to implement [23.1.1]/9
2141 template<typename _InputIterator>
2142 _GLIBCXX20_CONSTEXPR
2143 void
2144 _M_insert_dispatch(iterator __pos, _InputIterator __first,
2145 _InputIterator __last, __false_type)
2146 {
2147 _M_range_insert(__pos, __first, __last,
2148 std::__iterator_category(__first));
2149 }
2150
2151 // Called by the second insert_dispatch above
2152 template<typename _InputIterator>
2153 _GLIBCXX20_CONSTEXPR
2154 void
2155 _M_range_insert(iterator __pos, _InputIterator __first,
2156 _InputIterator __last, std::input_iterator_tag);
2157
2158 // Called by the second insert_dispatch above
2159 template<typename _ForwardIterator>
2160 _GLIBCXX20_CONSTEXPR
2161 void
2162 _M_range_insert(iterator __pos, _ForwardIterator __first,
2163 _ForwardIterator __last, std::forward_iterator_tag);
2164
2165 // Called by insert(p,n,x), and the range insert when it turns out to be
2166 // the same thing.
2167 _GLIBCXX20_CONSTEXPR
2168 void
2169 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
2170
2171 // Called by resize(n,x), and the _M_fill_insert(end(), n, x)
2172 _GLIBCXX20_CONSTEXPR
2173 void
2174 _M_fill_append(size_type __n, const value_type& __x);
2175
2176#if __cplusplus >= 201103L
2177 // Called by resize(n).
2178 _GLIBCXX20_CONSTEXPR
2179 void
2180 _M_default_append(size_type __n);
2181
2182 _GLIBCXX20_CONSTEXPR
2183 bool
2184 _M_shrink_to_fit();
2185#endif
2186
2187#if __cplusplus < 201103L
2188 // Called by insert(p,x)
2189 void
2190 _M_insert_aux(iterator __position, const value_type& __x);
2191
2192 void
2193 _M_realloc_insert(iterator __position, const value_type& __x);
2194
2195 void
2196 _M_realloc_append(const value_type& __x);
2197#else
2198 // A value_type object constructed with _Alloc_traits::construct()
2199 // and destroyed with _Alloc_traits::destroy().
2200 struct _Temporary_value
2201 {
2202 template<typename... _Args>
2203 _GLIBCXX20_CONSTEXPR explicit
2204 _Temporary_value(vector* __vec, _Args&&... __args) : _M_this(__vec)
2205 {
2206 _Alloc_traits::construct(_M_this->_M_impl, _M_ptr(),
2207 std::forward<_Args>(__args)...);
2208 }
2209
2210 _GLIBCXX20_CONSTEXPR
2211 ~_Temporary_value()
2212 { _Alloc_traits::destroy(_M_this->_M_impl, _M_ptr()); }
2213
2214 _GLIBCXX20_CONSTEXPR value_type&
2215 _M_val() noexcept { return _M_storage._M_val; }
2216
2217 private:
2218 _GLIBCXX20_CONSTEXPR _Tp*
2219 _M_ptr() noexcept { return std::__addressof(_M_storage._M_val); }
2220
2221 union _Storage
2222 {
2223 constexpr _Storage() : _M_byte() { }
2224 _GLIBCXX20_CONSTEXPR ~_Storage() { }
2225 _Storage& operator=(const _Storage&) = delete;
2226 unsigned char _M_byte;
2227 _Tp _M_val;
2228 };
2229
2230 vector* _M_this;
2231 _Storage _M_storage;
2232 };
2233
2234 // Called by insert(p,x) and other functions when insertion needs to
2235 // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
2236 template<typename _Arg>
2237 _GLIBCXX20_CONSTEXPR
2238 void
2239 _M_insert_aux(iterator __position, _Arg&& __arg);
2240
2241 template<typename... _Args>
2242 _GLIBCXX20_CONSTEXPR
2243 void
2244 _M_realloc_insert(iterator __position, _Args&&... __args);
2245
2246 template<typename... _Args>
2247 _GLIBCXX20_CONSTEXPR
2248 void
2249 _M_realloc_append(_Args&&... __args);
2250
2251 // Either move-construct at the end, or forward to _M_insert_aux.
2252 _GLIBCXX20_CONSTEXPR
2253 iterator
2254 _M_insert_rval(const_iterator __position, value_type&& __v);
2255
2256 // Try to emplace at the end, otherwise forward to _M_insert_aux.
2257 template<typename... _Args>
2258 _GLIBCXX20_CONSTEXPR
2259 iterator
2260 _M_emplace_aux(const_iterator __position, _Args&&... __args);
2261
2262 // Emplacing an rvalue of the correct type can use _M_insert_rval.
2263 _GLIBCXX20_CONSTEXPR
2264 iterator
2265 _M_emplace_aux(const_iterator __position, value_type&& __v)
2266 { return _M_insert_rval(__position, std::move(__v)); }
2267#endif
2268
2269 // Called by _M_fill_insert, _M_insert_aux etc.
2270 _GLIBCXX20_CONSTEXPR
2271 size_type
2272 _M_check_len(size_type __n, const char* __s) const
2273 {
2274 const size_type __room = max_size() - size();
2275 if (__room < __n)
2276 __throw_length_error(__N(__s));
2277
2278 if (__n < size())
2279 __n = size(); // Grow by (at least) doubling ...
2280 if (__n > __room)
2281 __n = __room; // ... but only as much as will fit.
2282 return size() + __n;
2283 }
2284
2285 // Called by constructors to check initial size.
2286 static _GLIBCXX20_CONSTEXPR size_type
2287 _S_check_init_len(size_type __n, const allocator_type& __a)
2288 {
2289 if (__n > _Base::_S_max_size(_Tp_alloc_type(__a)))
2290 __throw_length_error(
2291 __N("cannot create std::vector larger than max_size()"));
2292 return __n;
2293 }
2294
2295 // Internal erase functions follow.
2296
2297 // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
2298 // _M_assign_aux.
2299 _GLIBCXX20_CONSTEXPR
2300 void
2301 _M_erase_at_end(pointer __pos) _GLIBCXX_NOEXCEPT
2302 {
2303 if (size_type __n = this->_M_impl._M_finish - __pos)
2304 {
2305 std::_Destroy(__pos, this->_M_impl._M_finish,
2306 _M_get_Tp_allocator());
2307 this->_M_impl._M_finish = __pos;
2308 _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n);
2309 }
2310 }
2311
2312 _GLIBCXX20_CONSTEXPR
2313 iterator
2314 _M_erase(iterator __position);
2315
2316 _GLIBCXX20_CONSTEXPR
2317 iterator
2318 _M_erase(iterator __first, iterator __last);
2319
2320#if __cplusplus >= 201103L
2321 private:
2322 // Constant-time move assignment when source object's memory can be
2323 // moved, either because the source's allocator will move too
2324 // or because the allocators are equal.
2325 _GLIBCXX20_CONSTEXPR
2326 void
2327 _M_move_assign(vector&& __x, true_type) noexcept
2328 {
2329 vector __tmp(get_allocator());
2330 this->_M_impl._M_swap_data(__x._M_impl);
2331 __tmp._M_impl._M_swap_data(__x._M_impl);
2332 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2333 }
2334
2335 // Do move assignment when it might not be possible to move source
2336 // object's memory, resulting in a linear-time operation.
2337 _GLIBCXX20_CONSTEXPR
2338 void
2339 _M_move_assign(vector&& __x, false_type)
2340 {
2341 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2342 _M_move_assign(std::move(__x), true_type());
2343 else
2344 {
2345 // The rvalue's allocator cannot be moved and is not equal,
2346 // so we need to individually move each element.
2347 this->_M_assign_aux(std::make_move_iterator(__x.begin()),
2348 std::make_move_iterator(__x.end()),
2349 std::random_access_iterator_tag());
2350 __x.clear();
2351 }
2352 }
2353#endif
2354
2355 template<typename _Up>
2356 _GLIBCXX20_CONSTEXPR
2357 _Up*
2358 _M_data_ptr(_Up* __ptr) const _GLIBCXX_NOEXCEPT
2359 { return __ptr; }
2360
2361#if __cplusplus >= 201103L
2362 template<typename _Ptr>
2363 _GLIBCXX20_CONSTEXPR
2364 typename std::pointer_traits<_Ptr>::element_type*
2365 _M_data_ptr(_Ptr __ptr) const
2366 { return empty() ? nullptr : std::__to_address(__ptr); }
2367#else
2368 template<typename _Ptr>
2369 value_type*
2370 _M_data_ptr(_Ptr __ptr) const
2371 { return empty() ? (value_type*)0 : __ptr.operator->(); }
2372#endif
2373 };
2374
2375#if __cpp_deduction_guides >= 201606
2376 template<typename _InputIterator, typename _ValT
2377 = typename iterator_traits<_InputIterator>::value_type,
2378 typename _Allocator = allocator<_ValT>,
2379 typename = _RequireInputIter<_InputIterator>,
2380 typename = _RequireAllocator<_Allocator>>
2381 vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
2382 -> vector<_ValT, _Allocator>;
2383
2384#if __glibcxx_containers_ranges // C++ >= 23
2385 template<ranges::input_range _Rg,
2386 __allocator_like _Alloc = allocator<ranges::range_value_t<_Rg>>>
2387 vector(from_range_t, _Rg&&, _Alloc = _Alloc())
2388 -> vector<ranges::range_value_t<_Rg>, _Alloc>;
2389#endif
2390#endif
2391
2392 /**
2393 * @brief Vector equality comparison.
2394 * @param __x A %vector.
2395 * @param __y A %vector of the same type as @a __x.
2396 * @return True iff the size and elements of the vectors are equal.
2397 *
2398 * This is an equivalence relation. It is linear in the size of the
2399 * vectors. Vectors are considered equivalent if their sizes are equal,
2400 * and if corresponding elements compare equal.
2401 */
2402 template<typename _Tp, typename _Alloc>
2403 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2404 inline bool
2405 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2406 { return (__x.size() == __y.size()
2407 && std::equal(__x.begin(), __x.end(), __y.begin())); }
2408
2409#if __cpp_lib_three_way_comparison // >= C++20
2410 /**
2411 * @brief Vector ordering relation.
2412 * @param __x A `vector`.
2413 * @param __y A `vector` of the same type as `__x`.
2414 * @return A value indicating whether `__x` is less than, equal to,
2415 * greater than, or incomparable with `__y`.
2416 *
2417 * See `std::lexicographical_compare_three_way()` for how the determination
2418 * is made. This operator is used to synthesize relational operators like
2419 * `<` and `>=` etc.
2420 */
2421 template<typename _Tp, typename _Alloc>
2422 [[nodiscard]]
2423 constexpr __detail::__synth3way_t<_Tp>
2424 operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2425 {
2426 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
2427 __y.begin(), __y.end(),
2428 __detail::__synth3way);
2429 }
2430#else
2431 /**
2432 * @brief Vector ordering relation.
2433 * @param __x A %vector.
2434 * @param __y A %vector of the same type as @a __x.
2435 * @return True iff @a __x is lexicographically less than @a __y.
2436 *
2437 * This is a total ordering relation. It is linear in the size of the
2438 * vectors. The elements must be comparable with @c <.
2439 *
2440 * See std::lexicographical_compare() for how the determination is made.
2441 */
2442 template<typename _Tp, typename _Alloc>
2443 _GLIBCXX_NODISCARD inline bool
2444 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2445 { return std::lexicographical_compare(__x.begin(), __x.end(),
2446 __y.begin(), __y.end()); }
2447
2448 /// Based on operator==
2449 template<typename _Tp, typename _Alloc>
2450 _GLIBCXX_NODISCARD inline bool
2451 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2452 { return !(__x == __y); }
2453
2454 /// Based on operator<
2455 template<typename _Tp, typename _Alloc>
2456 _GLIBCXX_NODISCARD inline bool
2457 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2458 { return __y < __x; }
2459
2460 /// Based on operator<
2461 template<typename _Tp, typename _Alloc>
2462 _GLIBCXX_NODISCARD inline bool
2463 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2464 { return !(__y < __x); }
2465
2466 /// Based on operator<
2467 template<typename _Tp, typename _Alloc>
2468 _GLIBCXX_NODISCARD inline bool
2469 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
2470 { return !(__x < __y); }
2471#endif // three-way comparison
2472
2473 /// See std::vector::swap().
2474 template<typename _Tp, typename _Alloc>
2475 _GLIBCXX20_CONSTEXPR
2476 inline void
2477 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
2478 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2479 { __x.swap(__y); }
2480
2481_GLIBCXX_END_NAMESPACE_CONTAINER
2482
2483#if __cplusplus >= 201703L
2484 namespace __detail::__variant
2485 {
2486 template<typename> struct _Never_valueless_alt; // see <variant>
2487
2488 // Provide the strong exception-safety guarantee when emplacing a
2489 // vector into a variant, but only if move assignment cannot throw.
2490 template<typename _Tp, typename _Alloc>
2491 struct _Never_valueless_alt<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
2492 : std::is_nothrow_move_assignable<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
2493 { };
2494 } // namespace __detail::__variant
2495#endif // C++17
2496
2497_GLIBCXX_END_NAMESPACE_VERSION
2498} // namespace std
2499
2500#endif /* _STL_VECTOR_H */
compare
initializer_list
concept_check.h
ranges_algobase.h
ranges_base.h
ranges_util.h
stdexcept_throw.h
stl_iterator_base_funcs.h
assertions.h
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
std::false_type
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
std::declval
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2741
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std::lexicographical_compare_three_way
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
Definition stl_algobase.h:1867
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std
ISO C++ entities toplevel namespace is std.
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:177
std::_Destroy
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
Definition stl_construct.h:219
std::initializer_list
initializer_list
Definition initializer_list:53
std::is_same
is_same
Definition type_traits:1649
std::is_nothrow_default_constructible
is_nothrow_default_constructible
Definition type_traits:1352
std::is_nothrow_move_assignable
is_nothrow_move_assignable
Definition type_traits:1438
std::size_type
std::allocator
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
std::value_type
std::reverse_iterator
Definition bits/stl_iterator.h:137
std::random_access_iterator_tag
Random-access iterators support a superset of bidirectional iterator operations.
Definition stl_iterator_base_types.h:109
std::iterator
Common iterator class.
Definition stl_iterator_base_types.h:133
std::vector
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:511
std::vector::insert
constexpr iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
Definition vector.tcc:134
std::vector::push_back
constexpr void push_back(const value_type &__x)
Add data to the end of the vector.
Definition stl_vector.h:1458
std::vector::resize
constexpr void resize(size_type __new_size, const value_type &__x)
Resizes the vector to the specified number of elements.
Definition stl_vector.h:1206
std::vector::operator=
constexpr vector & operator=(initializer_list< value_type > __l)
Vector list assignment operator.
Definition stl_vector.h:897
std::vector::_M_allocate_and_copy
constexpr _Alloc_result _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last)
Definition stl_vector.h:1951
std::vector::rbegin
constexpr reverse_iterator rbegin() noexcept
Definition stl_vector.h:1080
std::vector::end
constexpr iterator end() noexcept
Definition stl_vector.h:1060
std::vector::vector
constexpr vector(const vector &__x)
Vector copy constructor.
Definition stl_vector.h:673
std::vector::vector
vector()=default
Creates a vector with no elements.
std::vector::emplace
constexpr iterator emplace(const_iterator __position, _Args &&... __args)
Inserts an object in vector before specified iterator.
Definition stl_vector.h:1523
std::vector::insert
constexpr iterator insert(const_iterator __position, value_type &&__x)
Inserts given rvalue into vector before specified iterator.
Definition stl_vector.h:1570
std::vector::rend
constexpr const_reverse_iterator rend() const noexcept
Definition stl_vector.h:1110
std::vector::begin
constexpr iterator begin() noexcept
Definition stl_vector.h:1040
std::vector::capacity
constexpr size_type capacity() const noexcept
Definition stl_vector.h:1250
std::vector::insert
constexpr iterator insert(const_iterator __position, initializer_list< value_type > __l)
Inserts an initializer_list into the vector.
Definition stl_vector.h:1588
std::vector::~vector
constexpr ~vector() noexcept
Definition stl_vector.h:842
std::vector::begin
constexpr const_iterator begin() const noexcept
Definition stl_vector.h:1050
std::vector::assign
constexpr void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a vector.
Definition stl_vector.h:937
std::vector::assign
constexpr void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
Definition stl_vector.h:917
std::vector::erase
constexpr iterator erase(const_iterator __first, const_iterator __last)
Remove a range of elements.
Definition stl_vector.h:1863
std::vector::swap
constexpr void swap(vector &__x) noexcept
Swaps data with another vector.
Definition stl_vector.h:1887
std::vector::vector
constexpr vector(vector &&__rv, const __type_identity_t< allocator_type > &__m) noexcept(noexcept(vector(std::declval< vector && >(), std::declval< const allocator_type & >(), std::declval< typename _Alloc_traits::is_always_equal >())))
Move constructor with alternative allocator.
Definition stl_vector.h:731
std::vector::data
constexpr _Tp * data() noexcept
Definition stl_vector.h:1437
std::vector::vector
constexpr vector(size_type __n, const allocator_type &__a=allocator_type())
Creates a vector with default constructed elements.
Definition stl_vector.h:628
std::vector::front
constexpr const_reference front() const noexcept
Definition stl_vector.h:1398
std::vector::pop_back
constexpr void pop_back() noexcept
Removes last element.
Definition stl_vector.h:1499
std::vector::operator=
constexpr vector & operator=(vector &&__x) noexcept(_Alloc_traits::_S_nothrow_move())
Vector move assignment operator.
Definition stl_vector.h:875
std::vector::back
constexpr const_reference back() const noexcept
Definition stl_vector.h:1422
std::vector::reserve
constexpr void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
Definition vector.tcc:71
std::vector::at
constexpr reference at(size_type __n)
Provides access to the data contained in the vector.
Definition stl_vector.h:1355
std::vector::resize
constexpr void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
Definition stl_vector.h:1185
std::vector::_M_range_check
constexpr void _M_range_check(size_type __n) const
Safety check used only from at().
Definition stl_vector.h:1332
std::vector::front
constexpr reference front() noexcept
Definition stl_vector.h:1386
std::vector::insert
constexpr iterator insert(const_iterator __position, size_type __n, const value_type &__x)
Inserts a number of copies of given data into the vector.
Definition stl_vector.h:1614
std::vector::operator[]
constexpr const_reference operator[](size_type __n) const noexcept
Subscript access to the data contained in the vector.
Definition stl_vector.h:1322
std::vector::vector
constexpr vector(const allocator_type &__a) noexcept
Creates a vector with no elements.
Definition stl_vector.h:614
std::vector::erase
constexpr iterator erase(const_iterator __position)
Remove element at given position.
Definition stl_vector.h:1835
std::vector::empty
constexpr bool empty() const noexcept
Definition stl_vector.h:1265
std::vector::rend
constexpr reverse_iterator rend() noexcept
Definition stl_vector.h:1100
std::vector::rbegin
constexpr const_reverse_iterator rbegin() const noexcept
Definition stl_vector.h:1090
std::vector::crbegin
constexpr const_reverse_iterator crbegin() const noexcept
Definition stl_vector.h:1141
std::vector::at
constexpr const_reference at(size_type __n) const
Provides access to the data contained in the vector.
Definition stl_vector.h:1374
std::vector::cbegin
constexpr const_iterator cbegin() const noexcept
Definition stl_vector.h:1121
std::vector::vector
constexpr vector(_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
Builds a vector from a range.
Definition stl_vector.h:778
std::vector::vector
constexpr vector(initializer_list< value_type > __l, const allocator_type &__a=allocator_type())
Builds a vector from an initializer list.
Definition stl_vector.h:750
std::vector::end
constexpr const_iterator end() const noexcept
Definition stl_vector.h:1070
std::vector::vector
vector(vector &&) noexcept=default
Vector move constructor.
std::vector::insert
constexpr iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
Inserts a range into the vector.
Definition stl_vector.h:1659
std::vector::clear
constexpr void clear() noexcept
Definition stl_vector.h:1906
std::vector::assign
constexpr void assign(initializer_list< value_type > __l)
Assigns an initializer list to a vector.
Definition stl_vector.h:964
std::vector::get_allocator
constexpr allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_vector.h:317
std::vector< _Tp, polymorphic_allocator< _Tp > >::size
constexpr size_type size() const noexcept
Definition stl_vector.h:1159
std::vector::vector
constexpr vector(size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
Creates a vector with copies of an exemplar element.
Definition stl_vector.h:641
std::vector::operator=
constexpr vector & operator=(const vector &__x)
Vector assignment operator.
Definition vector.tcc:210
std::vector::back
constexpr reference back() noexcept
Definition stl_vector.h:1410
std::vector::crend
constexpr const_reverse_iterator crend() const noexcept
Definition stl_vector.h:1151
std::vector::cend
constexpr const_iterator cend() const noexcept
Definition stl_vector.h:1131
std::vector::operator[]
constexpr reference operator[](size_type __n) noexcept
Subscript access to the data contained in the vector.
Definition stl_vector.h:1303
std::vector::shrink_to_fit
constexpr void shrink_to_fit()
Definition stl_vector.h:1240
std::vector::max_size
constexpr size_type max_size() const noexcept
Definition stl_vector.h:1170
__gnu_cxx::__alloc_traits
Uniform interface to C++98 and C++11 allocators.
Definition ext/alloc_traits.h:51
__gnu_cxx::__alloc_traits::max_size
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
Definition bits/alloc_traits.h:493
std::assignable_from
[concept.assignable], concept assignable_from
Definition concepts:149
std::ranges::sized_range
[range.sized] The sized_range concept.
Definition ranges_base.h:553
std::ranges::input_range
A range for which ranges::begin returns an input iterator.
Definition ranges_base.h:619
std::ranges::forward_range
A range for which ranges::begin returns a forward iterator.
Definition ranges_base.h:624