STD::VECTOR(3)
std::vector< _Tp, _Alloc > π (3)
okay bestie, welcome to the Library Functions Manual β section 3, the one where all the good stuff lives. we're talking about std::vector< _Tp, _Alloc > today and honestly? she's the moment. the blueprint. the girl who does it all. π
β¨ NAME β¨
std::vector< _Tp, _Alloc > β a standard container which offers fixed time access to individual elements in any order. π¦
that's it. that's the whole pitch. fixed time access, any order you want, no notes. she's giving main character energy and she hasn't even gotten to the synopsis yet π
okay so that was the intro, but we are SO not done β there is a whole SYNOPSIS and DESCRIPTION and honestly a ridiculous amount of options coming. stay with me, this is gonna be iconic β¨
π SYNOPSIS π
#include <vector>
Inherits std::_Vector_base< _Tp, _Alloc >. β¨
πΈ Inherited by πΈ
Like, the girlies that pulled their whole look from this base class, [private] edition:
std::match_results< _BidirectionalIterator, polymorphic_allocator< sub_match< _BidirectionalIterator > > >[private]std::match_results< const char * >[private]std::match_results< string::const_iterator >[private]std::match_results< const wchar_t * >[private]std::match_results< wstring::const_iterator >[private]- and . (bestie, that's genuinely what the page says, I'm not fixing it, I don't make the rules π)
π Public Types π
The whole wardrobe, aka the typedefs. No cap, these are the aliases you'll be saying out loud in your code:
typedef _Alloc allocator_typetypedef __gnu_cxx::__normal_iterator< const_pointer, vector > const_iteratortypedef _Alloc_traits::const_pointer const_pointertypedef _Alloc_traits::const_reference const_referencetypedef std::reverse_iterator< const_iterator > const_reverse_iteratortypedef ptrdiff_t difference_typetypedef __gnu_cxx::__normal_iterator< pointer, vector > iteratortypedef _Base::pointer pointertypedef _Alloc_traits::reference referencetypedef std::reverse_iterator< iterator > reverse_iteratortypedef size_t size_typetypedef _Tp value_type
That's the full set, all twelve, and yes I counted them for you. π«Ά
π Public Member Functions π
Okay this is the big one, the main character section. Every single one, in order, nothing skipped, periodt.
π Constructors & Destructor
vector()=defaultβ makes you a vector with, like, nothing in it. Empty. Void. A blank canvas bestie. β¨template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> constexpr vector(_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())β builds a vector out of a whole range,__firstto__last, with optional allocator__a.constexpr vector(const allocator_type &__a) noexceptβ also a vector with no elements, but you hand it an allocator__athis time.constexpr vector(const vector &__x)β the copy constructor, she's duplicating herself πconstexpr vector(const vector &__x, const __type_identity_t< allocator_type > &__a)β copy constructor but you get to swap in a different allocator__a.constexpr vector(initializer_list< value_type > __l, const allocator_type &__a=allocator_type())β builds a vector straight from an initializer list__l.constexpr vector(size_type __n, const allocator_type &__a=allocator_type())β__nelements, all default constructed. So many little blank slates.constexpr vector(size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())β__ncopies of your exemplar element__value. Iconic copy-paste energy.vector(vector &&) noexcept=defaultβ the move constructor. She's not copying, she's relocating. π₯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, but with an alternative allocator__m. Look at thatnoexceptclause, she's doing the absolute most and honestly? Slay.constexpr ~vector() noexceptβ destructor. Bye bestie, thanks for the memories. π¦
π§ Random Internal One
template<typename... _Args> constexpr auto _M_emplace_aux(const_iterator __position, _Args &&... __args) -> iteratorβ no description on the page, she's mysterious like that. Just vibes and a trailing return type.
π Assignment
template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> constexpr void assign(_InputIterator __first, _InputIterator __last)β assigns a range to your vector.constexpr void assign(initializer_list< value_type > __l)β assigns an initializer list__lto your vector.constexpr void assign(size_type __n, const value_type &__val)β assigns a given value__valto your vector.constexpr vector & operator=(const vector &__x)β the assignment operator. Copy vibes.constexpr vector & operator=(initializer_list< value_type > __l)β the list assignment operator, takes__l.constexpr vector & operator=(vector &&__x) noexcept(_Alloc_traits::_S_nothrow_move())β move assignment operator. Note thatnoexcept(_Alloc_traits::_S_nothrow_move())β it only promises not to throw if that condition holds. π€
π Access
constexpr reference at(size_type __n)β gives you access to the data inside, with__nas your index.constexpr const_reference at(size_type __n) constβ same deal, const flavored.constexpr const_reference back() const noexceptβ last element, const.constexpr reference back() noexceptβ last element, mutable.constexpr const_iterator begin() const noexceptβ start of the const party.constexpr iterator begin() noexceptβ start of the party.constexpr size_type capacity() const noexceptβ how much room she's got before she has to move house.constexpr const_iterator cbegin() const noexceptβ const begin, the c stands for const, not cute, but also cute.constexpr const_iterator cend() const noexceptβ const end.constexpr void clear() noexceptβ wipes everything. Clean slate era.constexpr const_reverse_iterator crbegin() const noexceptβ const reverse begin.constexpr const_reverse_iterator crend() const noexceptβ const reverse end.constexpr const _Tp * data() const noexceptβ raw pointer to the const data, she's showing you the real her.constexpr _Tp * data() noexceptβ raw pointer to the data, mutable version.constexpr bool empty() const noexceptβ is she empty? Yes or no, no in between.constexpr const_iterator end() const noexceptβ const end of the line.constexpr iterator end() noexceptβ end of the line.constexpr const_reference front() const noexceptβ first element, const.constexpr reference front() noexceptβ first element, mutable.constexpr allocator_type get_allocator() const noexceptβ hands you a copy of the memory allocation object. A copy! Don't forget that part.constexpr const_reference operator[](size_type __n) const noexceptβ subscript access to the data, const,__nis your index. Note: no bounds check on this one, bestie. She will not warn you. π¨constexpr reference operator[](size_type __n) noexceptβ subscript access to the data, mutable, same deal, same lack of checks.constexpr const_reverse_iterator rbegin() const noexceptβ const reverse begin.constexpr reverse_iterator rbegin() noexceptβ reverse begin.constexpr const_reverse_iterator rend() const noexceptβ const reverse end.constexpr reverse_iterator rend() noexceptβ reverse end.
β¨ Modifiers
template<typename... _Args> constexpr iterator emplace(const_iterator __position, _Args &&... __args)β inserts an object into the vector before the specified iterator__position. Constructed in place, no extra copy, she's efficient. πtemplate<typename... _Args> constexpr reference emplace_back(_Args &&... __args)β no description given, just vibes and a reference return.constexpr iterator erase(const_iterator __first, const_iterator __last)β removes a whole range of elements,__firstthrough__last.constexpr iterator erase(const_iterator __position)β removes the element at the given position.template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> constexpr iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)β inserts a range into the vector, before__position.constexpr iterator insert(const_iterator __position, const value_type &__x)β inserts the given value__xinto the vector before the specified iterator.constexpr iterator insert(const_iterator __position, initializer_list< value_type > __l)β inserts an initializer list__linto the vector.constexpr iterator insert(const_iterator __position, size_type __n, const value_type &__x)β inserts__ncopies of__xinto the vector.constexpr iterator insert(const_iterator __position, value_type &&__x)β inserts the given rvalue__xinto the vector before the specified iterator. Moves only.constexpr size_type max_size() const noexceptβ the absolute ceiling on how many she could hold.constexpr void pop_back() noexceptβ removes the last element. Bye.constexpr void push_back(const value_type &__x)β adds data__xto the end of the vector.constexpr void push_back(value_type &&__x)β same but rvalue, no description on the page, she's keeping it short.constexpr void reserve(size_type __n)β attempts to preallocate enough memory for__nelements. Attempts. πconstexpr void resize(size_type __new_size)β resizes the vector to__new_sizeelements.constexpr void resize(size_type __new_size, const value_type &__x)β resizes the vector to__new_sizeelements, using__xfor the new ones.constexpr void shrink_to_fit()β no description, she's just giving you less.constexpr size_type size() const noexceptβ how many elements she actually has right now.constexpr void swap(vector &__x) noexceptβ swaps data with another vector__x.
π‘οΈ Protected Member Functions π‘οΈ
Backstage area. You're not supposed to be here unless you're writing a derived class, but here's the whole list anyway because I don't gatekeep. π«Ά
constexpr pointer _M_allocate(size_t __n)template<typename _ForwardIterator> constexpr pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last)template<typename _ForwardIterator> constexpr void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)template<typename _InputIterator> constexpr void _M_assign_aux(_InputIterator __first, _InputIterator __last, std::input_iterator_tag)template<typename _InputIterator> constexpr void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)template<typename _Integer> constexpr void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)constexpr size_type _M_check_len(size_type __n, const char *__s) constconstexpr void _M_create_storage(size_t __n)constexpr void _M_deallocate(pointer __p, size_t __n)constexpr void _M_default_append(size_type __n)constexpr void _M_default_initialize(size_type __n)template<typename... _Args> constexpr iterator _M_emplace_aux(const_iterator __position, _Args &&... __args)constexpr iterator _M_emplace_aux(const_iterator __position, value_type &&__v)constexpr iterator _M_erase(iterator __first, iterator __last)constexpr iterator _M_erase(iterator __position)constexpr void _M_erase_at_end(pointer __pos) noexceptconstexpr void _M_fill_append(size_type __n, const value_type &__x)constexpr void _M_fill_assign(size_type __n, const value_type &__val)constexpr void _M_fill_initialize(size_type __n, const value_type &__value)constexpr void _M_fill_insert(iterator __pos, size_type __n, const value_type &__x)constexpr const _Tp_alloc_type & _M_get_Tp_allocator() const noexceptconstexpr _Tp_alloc_type & _M_get_Tp_allocator() noexcepttemplate<typename _Arg> constexpr void _M_insert_aux(iterator __position, _Arg &&__arg)template<typename _InputIterator> constexpr void _M_insert_dispatch(iterator __pos, _InputIterator __first, _InputIterator __last, __false_type)template<typename _Integer> constexpr void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, __true_type)constexpr iterator _M_insert_rval(const_iterator __position, value_type &&__v)constexpr void _M_range_check(size_type __n) constβ safety check, and the page is very clear: it's used only fromat(). Only. Fromat(). Remember that when you're out here rawdoggingoperator[]. πtemplate<typename _ForwardIterator> constexpr void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)template<typename _InputIterator> constexpr void _M_range_initialize(_InputIterator __first, _InputIterator __last, std::input_iterator_tag)template<typename _Iterator, typename _Sentinel> constexpr void _M_range_initialize_n(_Iterator __first, _Sentinel __last, size_type __n)template<typename _ForwardIterator> constexpr void _M_range_insert(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)template<typename _InputIterator> constexpr void _M_range_insert(iterator __pos, _InputIterator __first, _InputIterator __last, std::input_iterator_tag)template<typename... _Args> constexpr void _M_realloc_append(_Args &&... __args)template<typename... _Args> constexpr void _M_realloc_insert(iterator __position, _Args &&... __args)constexpr bool _M_shrink_to_fit()
β Static Protected Member Functions β
Static, so they belong to the class itself, not to any one instance. Independent queens. π
static constexpr size_type _S_check_init_len(size_type __n, const allocator_type &__a)static constexpr size_type _S_max_size(const _Tp_alloc_type &__a) noexcept
π¦ Protected Attributes π¦
Just the one, and she's carrying the whole operation on her back:
_Vector_impl _M_impl
okay pausing here, more incoming, don't go anywhere π
β¨ DETAILED DESCRIPTION β¨
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
class std::vector< _Tp, _Alloc >
Okay bestie, gather round, because this is the container. A standard container which offers fixed time access to individual elements in any order. That's the whole vibe. π«Ά
Since
C++98 β yeah she's been iconic since forever, an OG, a legend, no notes. π
Template Parameters
_Tpβ Type of element. (whatever lil guy you're storing in here)_Allocβ Allocator type, defaults toallocator<_Tp>. (the memory plug, and if you don't pick one she picks for you π)
Meets the requirements of a container, a reversible container, and a sequence, including the optional sequence requirements with the exception of push_front and pop_front.
β οΈ heads up bestie: she does not dopush_frontorpop_front. Front insertion is simply not her thing. Everything else in the optional sequence requirements? Slayed. β
In some terminology a vector can be described as a dynamic C-style array, it offers fast and efficient access to individual elements in any order and saves the user from worrying about memory and size allocation. Subscripting ( [] ) access is also provided as with C-style arrays. So like, it's an array but it's grown up and handles its own business. No cap. π
π CONSTRUCTOR & DESTRUCTOR DOCUMENTATION π
vector () [default]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector ()
Creates a vector with no elements. Empty. A blank canvas. An icon before she's even started. β¨
Referenced by insert(), and operator=().
vector (const allocator_type & __a) [inline], [explicit], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (const allocator_type & __a)
Creates a vector with no elements. (Yes, same energy as the one above, but you're bringing your own allocator to the function.)
Parameters
__aβ An allocator object.
vector (size_type __n, const allocator_type & __a = allocator_type()) [inline], [explicit], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (size_type __n, const allocator_type & __a = allocator_type())
Creates a vector with default constructed elements.
Parameters
__nβ The number of elements to initially create.__aβ An allocator.
This constructor fills the vector with __n default constructed elements. So you say "gimme __n of them" and she gimme-__n-of-them. π¦
vector (size_type __n, const value_type & __value, const allocator_type & __a = allocator_type()) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (size_type __n, const value_type & __value, const allocator_type & __a = allocator_type())
Creates a vector with copies of an exemplar element.
Parameters
__nβ The number of elements to initially create.__valueβ An element to copy.__aβ An allocator.
This constructor fills the vector with __n copies of __value. Copy-paste but make it couture. π
vector (const vector< _Tp, _Alloc > & __x) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (const vector< _Tp, _Alloc > & __x)
Vector copy constructor.
Parameters
__xβ A vector of identical element and allocator types.
All the elements of __x are copied, but any unused capacity in __x will not be copied (i.e. capacity() == size() in the new vector).
she copies the fits, not the closet space. Allocator note: the newly-created vector uses a copy of the allocator object used by __x (unless the allocator traits dictate a different object).
vector (vector< _Tp, _Alloc > && ) [default], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (vector< _Tp, _Alloc > && )
Vector move constructor. The newly-created vector contains the exact contents of the moved instance. The contents of the moved instance are a valid, but unspecified vector.
β οΈ "valid, but unspecified" β do NOT go poking around in the moved-from vector expecting anything specific, bestie. She's valid, she's just not telling you what she is. π
vector (const vector< _Tp, _Alloc > & __x, const __type_identity_t< allocator_type > & __a) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (const vector< _Tp, _Alloc > & __x, const __type_identity_t< allocator_type > & __a)
Copy constructor with alternative allocator. Same copy energy, different plug. π§
vector (vector< _Tp, _Alloc > && __rv, const __type_identity_t< allocator_type > & __m) [inline], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (vector< _Tp, _Alloc > && __rv, const __type_identity_t< allocator_type > & __m)
Move constructor with alternative allocator. She's moving and she brought her own plug. Multitasking queen. π
vector (initializer_list< value_type > __l, const allocator_type & __a = allocator_type()) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::vector (initializer_list< value_type > __l, const allocator_type & __a = allocator_type())
Builds a vector from an initializer list.
Parameters
__lβ Aninitializer_list.__aβ An allocator.
Create a vector consisting of copies of the elements in the initializer_list __l.
This will call the element type's copy constructor N times (where N is __l.size()) and do no memory reallocation. Clean, one shot, no messy reshuffling. β¨
vector (_InputIterator __first, _InputIterator __last, const allocator_type & __a = allocator_type()) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
std::vector< _Tp, _Alloc >::vector (_InputIterator __first, _InputIterator __last, const allocator_type & __a = allocator_type())
Builds a vector from a range.
Parameters
__firstβ An input iterator.__lastβ An input iterator.__aβ An allocator.
Create a vector consisting of copies of the elements from [first,last).
If the iterators are forward, bidirectional, or random-access, then this will call the elements' copy constructor N times (where N is distance(first,last)) and do no memory reallocation. But if only input iterators are used, then this will do at most 2N calls to the copy constructor, and logN memory reallocations. iykyk β the iterator category really does change the whole plot here. π¦
~vector () [inline], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::vector< _Tp, _Alloc >::~vector ()
The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.
π¨ READ THIS TWICE, BESTIE: if your vector holds pointers, destructing the vector does NOT delete the things they point at. It deletes the pointers. The pointed-to memory is untouched. That's on you. She's not your mom. π
π MEMBER FUNCTION DOCUMENTATION π
_M_allocate_and_copy [inline], [constexpr], [protected]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _ForwardIterator>
pointer std::vector< _Tp, _Alloc >::_M_allocate_and_copy (size_type __n, _ForwardIterator __first, _ForwardIterator __last)
Memory expansion handler. Uses the member allocation function to obtain n bytes of memory, and then copies [first,last) into it. (internal machinery, protected, she does the heavy lifting behind the curtain π)
Referenced by operator=(), and reserve().
_M_range_check [inline], [constexpr], [protected]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::_M_range_check (size_type __n) const
Safety check used only from at(). the little bouncer at the door checking your ID π«Ά
Referenced by std::vector< _Tp, polymorphic_allocator< _Tp > >::at(), and std::vector< _Tp, polymorphic_allocator< _Tp > >::at().
assign (_InputIterator __first, _InputIterator __last) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
void std::vector< _Tp, _Alloc >::assign (_InputIterator __first, _InputIterator __last)
Assigns a range to a vector.
Parameters
__firstβ An input iterator.__lastβ An input iterator.
This function fills a vector with copies of the elements in the range [__first,__last).
Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. full glow-up, old contents GONE, new contents in. π
assign (initializer_list< value_type > __l) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::assign (initializer_list< value_type > __l)
Assigns an initializer list to a vector.
Parameters
__lβ Aninitializer_list.
This function fills a vector with copies of the elements in the initializer list __l.
Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. new era, who dis. β¨
assign (size_type __n, const value_type & __val) [inline], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::assign (size_type __n, const value_type & __val)
Assigns a given value to a vector.
Parameters
__nβ Number of elements to be assigned.__valβ Value to be assigned.
This function fills a vector with __n copies of the given value. Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. same note, three times, because she means it: the assignment COMPLETELY changes the vector. π₯
at (size_type __n) [inline], [nodiscard], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reference std::vector< _Tp, _Alloc >::at (size_type __n)
Provides access to the data contained in the vector.
Parameters
__nβ The index of the element for which data should be accessed.
Returns β Read/write reference to data.
Exceptions β std::out_of_range If __n is an invalid index.
This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails. she checks before she lets you in β that's the whole difference between her and []. π«Ά
at (size_type __n) const [inline], [nodiscard], [constexpr]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::vector< _Tp, _Alloc >::at (size_type __n) const
Provides access to the data contained in the vector.
Parameters
__nβ The index of the element for which data should be accessed.
Returns β Read-only (constant) reference to data.
Exceptions β std::out_of_range If __n is an invalid index.
This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails. same safety bouncer, but this time you only get to LOOK. π
back () const [inline], [nodiscard], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::vector< _Tp, _Alloc >::back () const
Returns a read-only (constant) reference to the data at the last element of the vector. The final girl. The last one standing. π
back () [inline], [nodiscard], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reference std::vector< _Tp, _Alloc >::back ()
Returns a read/write reference to the data at the last element of the vector. Now you can edit the last one too. Slay. π
begin () const [inline], [nodiscard], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::vector< _Tp, _Alloc >::begin () const
Returns a read-only (constant) iterator that points to the first element in the vector. Iteration is done in ordinary element order. start at the front, go in order, no surprises. π¦
begin () [inline], [nodiscard], [constexpr], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::begin ()
Returns a read/write iterator that points to the first element in the vector. Iteration is done in ordinary element order.
Referenced by std::vector< _Tp, polymorphic_allocator< _Tp > >::vector(), std::vector< _Tp, polymorphic_allocator< _Tp > >::crend(), std::vector< _Tp, polymorphic_allocator< _Tp > >::empty(), std::vector< _Tp, polymorphic_allocator< _Tp > >::erase(), std::vector< _Tp, polymorphic_allocator< _Tp > >::erase(), std::vector< _Tp, polymorphic_allocator< _Tp > >::front(), std::vector< _Tp, polymorphic_allocator< _Tp > >::front(), std::vector< _Tp, polymorphic_allocator< _Tp > >::insert(), std::vector< _Tp, polymorphic_allocator< _Tp > >::insert(), std::vector< _Tp, polymorphic_allocator< _Tp > >::insert(), __gnu_parallel::multiway_merge_exact_splitting(), std::operator<=>(), operator=(), std::operator==(), std::vector< _Tp, polymorphic_allocator< _Tp > >::rend(), and std::vector< _Tp, polymorphic_allocator< _Tp > >::rend().
capacity()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
size_type std::vector< _Tp, _Alloc >::capacity () const
[inline], [nodiscard], [constexpr], [noexcept]
okay bestie, this one tells you the total number of elements the vector can hold before it has to go allocate more memory π like it's the seat count of the venue, and size() is how many people actually showed up. Capacity β₯ size, always, periodt.
Referenced byβoperator=(), andreserve().
cbegin()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::vector< _Tp, _Alloc >::cbegin () const
[inline], [constexpr], [noexcept]
Hands you a read-only (constant) iterator pointing at the very first element in the vector π and iteration happens in ordinary element order, no plot twists.
Referenced byβstd::vector< _Tp, polymorphic_allocator< _Tp > >::erase(),std::vector< _Tp, polymorphic_allocator< _Tp > >::erase(),std::vector< _Tp, polymorphic_allocator< _Tp > >::insert(),std::vector< _Tp, polymorphic_allocator< _Tp > >::insert(), andstd::vector< _Tp, polymorphic_allocator< _Tp > >::insert().
cend()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::vector< _Tp, _Alloc >::cend () const
[inline], [constexpr], [noexcept]
A read-only (constant) iterator that points one past the last element in the vector β¨ (yes one past β the classic off-the-end vibe, it's a marker not a real element, iykyk). Iteration is done in ordinary element order.
clear()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::clear ()
[inline], [constexpr], [noexcept]
Erases all the elements π bye babes, everyone out.
β οΈ Read this part twice, fr fr: this function only erases the elements. If the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. So if you were hoping clear() would clean up your heap tooβ¦ it will not, bestie, that's on you π
Referenced byβoperator=().
crbegin()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::vector< _Tp, _Alloc >::crbegin () const
[inline], [constexpr], [noexcept]
A read-only (constant) reverse iterator pointing at the last element in the vector π¦ iteration is done in reverse element order β so like, backwards but make it const.
crend()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::vector< _Tp, _Alloc >::crend () const
[inline], [constexpr], [noexcept]
A read-only (constant) reverse iterator that points one before the first element in the vector πΈ the reverse-end sentinel, the final boss of the backwards loop. Iteration is done in reverse element order.
data()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
_Tp * std::vector< _Tp, _Alloc >::data ()
[inline], [nodiscard], [constexpr], [noexcept]
Returns a pointer such that [data(), data() + size()) is a valid range π and for a non-empty vector, data() == &front(). Raw pointer access, main character energy, use responsibly π
emplace()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename... _Args>
iterator std::vector< _Tp, _Alloc >::emplace
(const_iterator __position, _Args &&... __args)
[inline], [constexpr]
Inserts an object in the vector before the specified iterator π
Parameters
__positionβ Aconst_iteratorinto the vector.__argsβ Arguments.
Returns
- An iterator that points to the inserted data.
This function will insert an object of type T constructed with T(std::forward<Args>(args)...) before the specified location β¨
Heads up: this kind of operation could be expensive for a vector, and if it is frequently used the user should consider using std::list. No cap β shuffling everything over is a lot of work for our girl.
empty()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
bool std::vector< _Tp, _Alloc >::empty () const
[inline], [nodiscard], [constexpr], [noexcept]
Returns true if the vector is empty π (Thus begin() would equal end(). Cute, right?)
Referenced byβstd::piecewise_constant_distribution< _RealType >::densities(),std::piecewise_linear_distribution< _RealType >::densities(), andstd::discrete_distribution< _IntType >::probabilities().
end() const
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::vector< _Tp, _Alloc >::end () const
[inline], [nodiscard], [constexpr], [noexcept]
A read-only (constant) iterator that points one past the last element in the vector πΈ iteration is done in ordinary element order.
end()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::end ()
[inline], [nodiscard], [constexpr], [noexcept]
A read/write iterator that points one past the last element in the vector π₯ iteration is done in ordinary element order.
Referenced byβstd::vector< _Tp, polymorphic_allocator< _Tp > >::vector(),std::vector< _Tp, polymorphic_allocator< _Tp > >::back(),std::vector< _Tp, polymorphic_allocator< _Tp > >::back(),std::vector< _Tp, polymorphic_allocator< _Tp > >::crbegin(),std::vector< _Tp, polymorphic_allocator< _Tp > >::empty(),__gnu_parallel::multiway_merge_exact_splitting(),std::operator<=>(),operator=(),std::operator==(),std::vector< _Tp, polymorphic_allocator< _Tp > >::rbegin(), andstd::vector< _Tp, polymorphic_allocator< _Tp > >::rbegin().
erase() β the range one
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::erase
(const_iterator __first, const_iterator __last)
[inline], [constexpr]
Remove a range of elements βοΈ
Parameters
__firstβ Iterator pointing to the first element to be erased.__lastβ Iterator pointing to one past the last element to be erased.
Returns
- An iterator pointing to the element pointed to by
__lastprior to erasing (orend()).
This function will erase the elements in the range [__first,__last) and shorten the vector accordingly π
Note β This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. Not me reminding you againβ¦ actually yes, me reminding you again π
erase() β the single-position one
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::erase (const_iterator __position)
[inline], [constexpr]
Remove the element at a given position π―
Parameters
__positionβ Iterator pointing to element to be erased.
Returns
- An iterator pointing to the next element (or
end()).
This function will erase the element at the given position and thus shorten the vector by one π
Note β This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. Same warning, different overload, still true π
front() const
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::vector< _Tp, _Alloc >::front () const
[inline], [nodiscard], [constexpr], [noexcept]
A read-only (constant) reference to the data at the first element of the vector π₯ look but don't touch.
front()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reference std::vector< _Tp, _Alloc >::front ()
[inline], [nodiscard], [constexpr], [noexcept]
A read/write reference to the data at the first element of the vector π₯ go ahead, edit her.
std::_Vector_base< _Tp, _Alloc >::get_allocator()
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
allocator_type std::_Vector_base< _Tp, _Alloc >::get_allocator () const
[inline], [constexpr], [noexcept]
Get a copy of the memory allocation object π
Referencesβget_allocator().Referenced byβget_allocator().
(not her referencing and being referenced by herselfβ¦ the recursion is giving self-care, honestly iconic π β¨)
insert() β the range one
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator,
typename = std::_RequireInputIter<_InputIterator>>
iterator std::vector< _Tp, _Alloc >::insert
(const_iterator __position, _InputIterator __first, _InputIterator __last)
[inline], [constexpr]
Inserts a range into the vector πΈ
Parameters
__positionβ Aconst_iteratorinto the vector.__firstβ An input iterator.__lastβ An input iterator.
Returns
- An iterator that points to the inserted data.
This function will insert copies of the data in the range [__first,__last) into the vector before the location specified by pos β¨
Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.
insert() β single const value_type&
template<typename _Tp, typename _Alloc>
vector< _Tp, _Alloc >::iterator vector::insert
(const_iterator __position, const value_type & __x)
[constexpr]
Inserts a given value into the vector before the specified iterator π
Parameters
__positionβ Aconst_iteratorinto the vector.__xβ Data to be inserted.
Returns
- An iterator that points to the inserted data.
This function will insert a copy of the given value before the specified location π
Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.
Referencesβvector(),std::begin(),std::cbegin(),std::end(),insert(), andstd::move().Referenced byβinsert().
insert() β the initializer_list one
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::insert
(const_iterator __position, initializer_list< value_type > __l)
[inline], [constexpr]
Inserts an initializer_list into the vector π¦
Parameters
__positionβ An iterator into the vector.__lβ Aninitializer_list.
This function will insert copies of the data in the initializer_list l into the vector before the location specified by position β¨
Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.
insert() β __n copies of __x
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::insert
(const_iterator __position, size_type __n, const value_type & __x)
[inline], [constexpr]
Inserts a number of copies of given data into the vector π
Parameters
__positionβ Aconst_iteratorinto the vector.__nβ Number of elements to be inserted.__xβ Data to be inserted.
Returns
- An iterator that points to the inserted data.
This function will insert a specified number of copies of the given data before the location specified by position β¨
Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.
insert() β the rvalue one
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::vector< _Tp, _Alloc >::insert
(const_iterator __position, value_type && __x)
[inline], [constexpr]
Inserts a given rvalue into the vector before the specified iterator π move semantics, she's moving, we love to see it.
Parameters
__positionβ Aconst_iteratorinto the vector.__xβ Data to be inserted.
Returns
- An iterator that points to the inserted data.
This function will insert a copy of the given rvalue before the specified location π
Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.
max_sizeβ the biggestsize()a vector could ever possibly have. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
size_type std::vector< _Tp, _Alloc >::max_size () const
[inline], [nodiscard], [constexpr], [noexcept]
Returns the size() of the largest possible vector. That's it, that's the whole vibe. And bestie, reserve() literally references it, so it's pulling its weight. β¨
operator=(copy) β vector assignment operator, the classic "make this one look like that one" move. π
template<typename _Tp, typename _Alloc>
vector< _Tp, _Alloc > & vector::operator= (const vector< _Tp, _Alloc > & __x)
[constexpr]
Parameters
__xβ a vector of identical element and allocator types.
All the elements of __x get copied over. But heads up: any unused capacity in __x does not get copied. Whether the allocator itself gets copied? That depends on the allocator traits, iykyk. π¦
References: vector(), std::__addressof(), std::_Destroy(), _M_allocate_and_copy(), begin(), capacity(), clear(), std::end(), end(), std::size(), and size().
operator=(initializer list) β vector list assignment operator, for when you roll up with a lil{...}. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
vector & std::vector< _Tp, _Alloc >::operator= (initializer_list< value_type > __l)
[inline], [constexpr]
Parameters
__lβ aninitializer_list.
This one fills a vector with copies of the elements in the initializer list __l.
Note that the assignment completely changes the vector β and the resulting vector's size is the same as the number of elements assigned. No leftovers, no half-measures, main character energy only. π
operator=(move) β vector move assignment operator, the "no thoughts just yeet" version. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
vector & std::vector< _Tp, _Alloc >::operator= (vector< _Tp, _Alloc > && __x)
[inline], [constexpr], [noexcept]
Parameters
__xβ a vector of identical element and allocator types.
The contents of __x get moved into this vector (without copying, if the allocators permit it). Afterwards __x is a valid, but unspecified vector. Which is fancy speak for "it's alive but it's not telling you what it's thinking." π
Whether the allocator is moved depends on the allocator traits.
operator[](const) β subscript access to the data contained in the vector, read-only edition. π§
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::vector< _Tp, _Alloc >::operator[] (size_type __n) const
[inline], [nodiscard], [constexpr], [noexcept]
Parameters
__nβ the index of the element for which data should be accessed.
Returns
- Read-only (constant) reference to data.
This operator allows for easy, array-style, data access. But listen: data access with this operator is unchecked, and out_of_range lookups are not defined. For checked lookups, see at(). πΈ
operator[]β same subscript access, but read/write this time, go off. π₯
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reference std::vector< _Tp, _Alloc >::operator[] (size_type __n)
[inline], [nodiscard], [constexpr], [noexcept]
Parameters
__nβ the index of the element for which data should be accessed.
Returns
- Read/write reference to data.
Again: easy, array-style, data access. Again: access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().) The warning is staying, bestie, it's important. π«Ά
pop_backβ removes the last element. It's giving stack operation. It shrinks the vector by one. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::pop_back ()
[inline], [constexpr], [noexcept]
Note that no data is returned β so if you need the last element's data, grab it before you call pop_back(). Not after. It's gone. Not me learning this the hard way. π
push_backβ add data to the end of the vector. β¨
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::push_back (const value_type & __x)
[inline], [constexpr]
Parameters
__xβ data to be added.
Typical stack operation. The function creates an element at the end of the vector and assigns the given data to it. And because of the nature of a vector, this can be done in constant time if the vector already has preallocated space available. Preallocate, bestie, it's the move. π
rbegin(const) β a read-only (constant) reverse iterator pointing at the last element in the vector. π¦
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::vector< _Tp, _Alloc >::rbegin () const
[inline], [nodiscard], [constexpr], [noexcept]
Iteration is done in reverse element order. Walking backwards, and honestly? Thriving.
rbeginβ the read/write reverse iterator that points to the last element in the vector. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reverse_iterator std::vector< _Tp, _Alloc >::rbegin ()
[inline], [nodiscard], [constexpr], [noexcept]
Iteration is done in reverse element order.
rend(const) β read-only (constant) reverse iterator pointing to one before the first element in the vector. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::vector< _Tp, _Alloc >::rend () const
[inline], [nodiscard], [constexpr], [noexcept]
Iteration is done in reverse element order. Yes, one before the first. That's the reverse-end thing, it's iconic, don't question it. π
rendβ the read/write reverse iterator pointing to one before the first element in the vector. β
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reverse_iterator std::vector< _Tp, _Alloc >::rend ()
[inline], [nodiscard], [constexpr], [noexcept]
Iteration is done in reverse element order.
reserveβ attempt to preallocate enough memory for a specified number of elements. The pre-planning queen. π
template<typename _Tp, typename _Alloc>
void vector::reserve (size_type __n)
[constexpr]
Parameters
__nβ number of elements required.
Exceptions
std::length_errorβ ifnexceedsmax_size().
This function attempts to reserve enough memory for the vector to hold the specified number of elements. If the number requested is more than max_size(), length_error is thrown. π¨
The advantage here: if optimal code is a necessity and you can figure out how many elements you'll need, you can reserve the memory in advance β and thus prevent a possible reallocation of memory and copying of vector data. So basically you're skipping the drama entirely. Slay.
References: std::_Destroy(), _M_allocate_and_copy(), capacity(), max_size(), and std::size().
resize(one-arg) β resizes the vector to the specified number of elements. π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::resize (size_type __new_size)
[inline], [constexpr]
Parameters
__new_sizeβ number of elements the vector should contain.
This function will resize the vector to the specified number of elements. If the number is smaller than the vector's current size, the vector gets truncated. Otherwise, default constructed elements are appended. π±
Referenced by __gnu_parallel::__shrink_and_double(), and __gnu_parallel::multiway_merge_exact_splitting().
resize(two-arg) β resizes the vector, but now you get to pick what fills the new slots. π¨
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::resize (size_type __new_size, const value_type & __x)
[inline], [constexpr]
Parameters
__new_sizeβ number of elements the vector should contain.__xβ data with which new elements should be populated.
This function will resize the vector to the specified number of elements. If the number is smaller than the vector's current size, the vector gets truncated. Otherwise the vector is extended and the new elements are populated with the given data. β¨
shrink_to_fitβ a non-binding request to reducecapacity()tosize(). π«Ά
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::shrink_to_fit ()
[inline], [constexpr]
Non-binding means the vector can literally just say "no π " and keep its capacity. It's a request, not a demand. Respect the boundaries.
sizeβ returns the number of elements in the vector. The moment of truth. π’
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
size_type std::vector< _Tp, _Alloc >::size () const
[inline], [nodiscard], [constexpr], [noexcept]
Returns the number of elements in the vector.
Referenced by __gnu_parallel::__shrink(), __gnu_parallel::__shrink_and_double(), std::vector< _Tp, polymorphic_allocator< _Tp > >::_M_range_check(), __gnu_parallel::list_partition(), operator=(), std::operator==(), std::vector< _Tp, polymorphic_allocator< _Tp > >::resize(), and std::vector< _Tp, polymorphic_allocator< _Tp > >::resize().
swapβ swaps data with another vector. The trade deal of the century. π€
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::vector< _Tp, _Alloc >::swap (vector< _Tp, _Alloc > & __x)
[inline], [constexpr], [noexcept]
Parameters
__xβ a vector of the same element and allocator types.
This exchanges the elements between two vectors in constant time. (Three pointers, so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(v1,v2) will feed into this function. π₯
Whether the allocators are swapped depends on the allocator traits.
π Author
Generated automatically by Doxygen for libstdc++ from the source code.
And that's the whole page, bestie β every last byte of it. π This part came straight out of the libstdc++ docs, the classic std::vector manpage, page footer reading libstdc++ on the left and std::vecto..., _Alloc >(3) on the right. Thanks for reading all five parts, that's commitment, that's loyalty, that's genuinely iconic of you. Go preallocate something. πβ¨π¦
π¦ where this page comes from
- arch rolling: gcc 16.2.1+r23+gd564253eb6c8-1 Β· GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later
- fedora 44: libstdc++-docs 16.0.1-0.10.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
- fedora 44: libstdc++-docs 16.2.1-2.fc44
- fedora 44: libstdc++-docs 16.2.1-3.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms