STD::VECTOR(3cxx)
π std::vector< _Tp, _Alloc > β the glow-up manual π
okay so bestie, welcome to the std::vector< _Tp, _Alloc >(3cxx) page. This is the iconic standard container that gives you fixed time access to individual elements in any order. periodt. β¨
πΈ NAME πΈ
std::vector< _Tp, _Alloc > β a standard container which offers fixed time access to individual elements in any order. Thatβs the whole name vibe, no cap. π«Ά
β¨ SYNOPSIS β¨
#include <vector>
Inherits std::_Vector_base< _Tp, _Alloc >.
Inherited by std::match_results< _Bi_iter, _Alloc > [private].
Lowkey important family tree info, bestie. π
π Public Types π
Hereβs the whole type-alias crew, exactly as listed. No skipping, no shrinking the list. π
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
π«Ά Public Member Functions π«Ά
This is the main-character list, bestie. Every single one is here. π
vector ()=defaultβ creates a vector with no elements. Empty. Clean slate. πtemplate<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> constexpr vector (_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())β builds a vector from a range.constexpr vector (const allocator_type &__a) noexceptβ creates a vector with no elements. Still empty, but with your allocator energy. β¨constexpr vector (const vector &__x)β vector copy constructor.constexpr vector (const vector &__x, const __type_identity_t< allocator_type > &__a)β copy constructor with an alternative allocator.constexpr vector (initializer_list< value_type > __l, const allocator_type &__a=allocator_type())β builds a vector from an initializer list.constexpr vector (size_type __n, const allocator_type &__a=allocator_type())β creates a vector with default constructed elements.constexpr vector (size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())β creates a vector with copies of an exemplar element.vector (vector &&) noexcept=defaultβ vector move constructor. Move it, donβt copy it. π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 an alternative allocator.constexpr ~vector () noexceptβ the destructor signature, right here. No extra description in the original, bestie. πtemplate<typename... _Args> constexpr auto _M_emplace_aux (const_iterator __position, _Args &&... __args) -> iteratorβ signature only in the original.template<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> constexpr void assign (_InputIterator __first, _InputIterator __last)β assigns a range to a vector.constexpr void assign (initializer_list< value_type > __l)β assigns an initializer list to a vector.constexpr void assign (size_type __n, const value_type &__val)β assigns a given value to a vector.constexpr reference at (size_type __n)β provides access to the data contained in the vector.constexpr const_reference at (size_type __n) constβ provides access to the data contained in the vector, const flavor. π§constexpr const_reference back () const noexceptβ signature only in the original.constexpr reference back () noexceptβ signature only in the original.constexpr const_iterator begin () const noexceptβ signature only in the original.constexpr iterator begin () noexceptβ signature only in the original.constexpr size_type capacity () const noexceptβ signature only in the original.constexpr const_iterator cbegin () const noexceptβ signature only in the original.constexpr const_iterator cend () const noexceptβ signature only in the original.constexpr void clear () noexceptβ signature only in the original.constexpr const_reverse_iterator crbegin () const noexceptβ signature only in the original.constexpr const_reverse_iterator crend () const noexceptβ signature only in the original.constexpr const _Tp * data () const noexceptβ signature only in the original.constexpr _Tp * data () noexceptβ signature only in the original.template<typename... _Args> constexpr iterator emplace (const_iterator __position, _Args &&... __args)β inserts an object in vector before specified iterator.template<typename... _Args> constexpr reference emplace_back (_Args &&... __args)β signature only in the original.constexpr bool empty () const noexceptβ signature only in the original.constexpr const_iterator end () const noexceptβ signature only in the original.constexpr iterator end () noexceptβ signature only in the original.constexpr iterator erase (const_iterator __first, const_iterator __last)β remove a range of elements.constexpr iterator erase (const_iterator __position)β remove element at given position.constexpr const_reference front () const noexceptβ signature only in the original.constexpr reference front () noexceptβ signature only in the original.constexpr allocator_type get_allocator () const noexceptβ get a copy of the memory allocation object.template<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> constexpr iterator insert (const_iterator __position, _InputIterator __first, _InputIterator __last)β inserts a range into the vector.constexpr iterator insert (const_iterator __position, const value_type &__x)β inserts given value into vector before specified iterator.constexpr iterator insert (const_iterator __position, initializer_list< value_type > __l)β inserts an initializer_list into the vector.constexpr iterator insert (const_iterator __position, size_type __n, const value_type &__x)β inserts a number of copies of given data into the vector.constexpr iterator insert (const_iterator __position, value_type &&__x)β inserts given rvalue into vector before specified iterator.constexpr size_type max_size () const noexceptβ signature only in the original.constexpr vector & operator= (const vector &__x)β vector assignment operator.constexpr vector & operator= (initializer_list< value_type > __l)β vector list assignment operator.constexpr vector & operator= (vector &&__x) noexcept(_Alloc_traits::_S_nothrow_move())β vector move assignment operator.constexpr const_reference operator[] (size_type __n) const noexceptβ subscript access to the data contained in the vector.constexpr reference operator[] (size_type __n) noexceptβ subscript access to the data contained in the vector.constexpr void pop_back () noexceptβ removes last element.constexpr void push_back (const value_type &__x)β add data to the end of the vector.constexpr void push_back (value_type &&__x)β signature only in the original.constexpr const_reverse_iterator rbegin () const noexceptβ signature only in the original.constexpr reverse_iterator rbegin () noexceptβ signature only in the original.constexpr const_reverse_iterator rend () const noexceptβ signature only in the original.constexpr reverse_iterator rend () noexceptβ signature only in the original.constexpr void reserve (size_type __n)β attempt to preallocate enough memory for specified number of elements.constexpr void resize (size_type __new_size)β resizes the vector to the specified number of elements.constexpr void resize (size_type __new_size, const value_type &__x)β resizes the vector to the specified number of elements.constexpr void shrink_to_fit ()β signature only in the original.constexpr size_type size () const noexceptβ signature only in the original.constexpr void swap (vector &__x) noexceptβ swaps data with another vector.
π Protected Member Functions π
Protected means the internals are doing their thing behind the scenes, bestie. Every listed signature is still here, fr fr. π¦
constexpr pointer _M_allocate (size_t __n)β signature only in the original.template<typename _ForwardIterator > constexpr pointer _M_allocate_and_copy (size_type __n, _ForwardIterator __first, _ForwardIterator __last)β signature only in the original.template<typename _ForwardIterator > constexpr void _M_assign_aux (_ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)β signature only in the original.template<typename _InputIterator > constexpr void _M_assign_aux (_InputIterator __first, _InputIterator __last, std::input_iterator_tag)β signature only in the original.template<typename _InputIterator > constexpr void _M_assign_dispatch (_InputIterator __first, _InputIterator __last, __false_type)β signature only in the original.template<typename _Integer > constexpr void _M_assign_dispatch (_Integer __n, _Integer __val, __true_type)β signature only in the original.constexpr size_type _M_check_len (size_type __n, const char *__s) constβ signature only in the original.constexpr void _M_create_storage (size_t __n)β signature only in the original.constexpr void _M_deallocate (pointer __p, size_t __n)β signature only in the original.constexpr void _M_default_append (size_type __n)β signature only in the original.constexpr void _M_default_initialize (size_type __n)β signature only in the original.template<typename... _Args> constexpr iterator _M_emplace_aux (const_iterator __position, _Args &&... __args)β signature only in the original.constexpr iterator _M_emplace_aux (const_iterator __position, value_type &&__v)β signature only in the original.constexpr iterator _M_erase (iterator __first, iterator __last)β signature only in the original.constexpr iterator _M_erase (iterator __position)β signature only in the original.constexpr void _M_erase_at_end (pointer __pos) noexceptβ signature only in the original.constexpr void _M_fill_assign (size_type __n, const value_type &__val)β signature only in the original.constexpr void _M_fill_initialize (size_type __n, const value_type &__value)β signature only in the original.constexpr void _M_fill_insert (iterator __pos, size_type __n, const value_type &__x)β signature only in the original.constexpr const _Tp_alloc_type & _M_get_Tp_allocator () const noexceptβ signature only in the original.constexpr _Tp_alloc_type & _M_get_Tp_allocator () noexceptβ signature only in the original.template<typename _Arg > constexpr void _M_insert_aux (iterator __position, _Arg &&__arg)β signature only in the original.template<typename _InputIterator > constexpr void _M_insert_dispatch (iterator __pos, _InputIterator __first, _InputIterator __last, __false_type)β signature only in the original.template<typename _Integer > constexpr void _M_insert_dispatch (iterator __pos, _Integer __n, _Integer __val, __true_type)β signature only in the original.constexpr iterator _M_insert_rval (const_iterator __position, value_type &&__v)β signature only in the original.constexpr void _M_range_check (size_type __n) constβ safety check used only fromat().template<typename _ForwardIterator > constexpr void _M_range_initialize (_ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)β signature only in the original.template<typename _InputIterator > constexpr void _M_range_initialize (_InputIterator __first, _InputIterator __last, std::input_iterator_tag)β signature only in the original.template<typename _ForwardIterator > constexpr void _M_range_insert (iterator __pos, _ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag)β signature only in the original.template<typename _InputIterator > constexpr void _M_range_insert (iterator __pos, _InputIterator __first, _InputIterator __last, std::input_iterator_tag)β signature only in the original.template<typename... _Args> constexpr void _M_realloc_append (_Args &&... __args)β signature only in the original.template<typename... _Args> constexpr void _M_realloc_insert (iterator __position, _Args &&... __args)β signature only in the original.constexpr bool _M_shrink_to_fit ()β signature only in the original.
π Static Protected Member Functions π
Static protected internals, still exactly as listed. π
static constexpr size_type _S_check_init_len (size_type __n, const allocator_type &__a)β signature only in the original.static constexpr size_type _S_max_size (const _Tp_alloc_type &__a) noexceptβ signature only in the original.
π§ Protected Attributes π§
_Vector_impl _M_implβ the protected attribute listed in the original. Thatβs the whole note, no extra lore. π
π Detailed Description π
okay bestie, settle in, because this is the part where we actually get to know the star of the show π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
class std::vector< _Tp, _Alloc >
It's "A standard container which offers fixed time access to individual elements in any order." β like, any order, zero drama, constant time, she simply does not miss π₯
Since: C++98 (she's an icon, she's been iconic)
π Template Parameters π
_Tpβ the type of the elements. whatever you're storing, she'll hold it π«Ά_Allocβ the allocator type, and it defaults toallocator<_Tp>. yes it's optional, no you don't have to touch it, yes it's giving behind-the-scenes main character energy β¨
She meets the requirements of a container, a reversible container, and a sequence β and that includes the optional sequence requirements except for push_front and pop_front. like, no front-pushing babe, that's not her thing, go talk to std::deque π
In some terminology you could describe a vector as a dynamic C-style array. she gives you fast and efficient access to individual elements in any order, and she saves you from worrying about memory and size allocation entirely β not your problem anymore, bestie, she handles it π Subscripting ([]) access is also provided, just like with C-style arrays. taste recognizes taste π
π§ Constructor & Destructor Documentation π§
okay there are a LOT of these and every single one is valid, let's go number by number because we do NOT skip β¨
β vector() β the default one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> std::vector<
_Tp, _Alloc >::vector () [default]
Creates a vector with no elements. empty, fresh, unbothered, moisturized πΈ
β vector(const allocator_type&) β empty but with your own allocator
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (const allocator_type & __a)
[inline], [explicit], [constexpr], [noexcept]
Creates a vector with no elements. same vibe as above, she's just carrying your allocator.
Parameters
__aβ an allocator object π
β vector(size_type, const allocator_type&) β n default-constructed cuties
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (size_type __n, const allocator_type
& __a = allocator_type()) [inline], [explicit], [constexpr]
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. no cap, __n of them, all freshly defaulted π
β vector(size_type, const value_type&, const allocator_type&) β n copies of one vibe
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (size_type __n, const value_type &
__value, const allocator_type & __a = allocator_type()) [inline],
[constexpr]
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. she said "I'll have what she's having" Γ __n ππ
β copy constructor
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (const vector< _Tp, _Alloc > & __x)
[inline], [constexpr]
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 takes the elements, she leaves the extra room, she's not greedy π
The newly-created vector uses a copy of the allocator object used by __x (unless the allocator traits dictate a different object). noted, allocator traits get the final say, we respect them π«Ά
References std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::end().
β move constructor
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> std::vector<
_Tp, _Alloc >::vector (vector< _Tp, _Alloc > &&) [default], [noexcept]
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 β so like, don't go reading her afterwards, she's fine but she's not yours anymore π
β copy constructor, but make it a different allocator
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (const vector< _Tp, _Alloc > & __x,
const __type_identity_t< allocator_type > & __a) [inline], [constexpr]
Copy constructor with alternative allocator. same copies, your allocator, slay β¨
β move constructor, but make it a different allocator
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (vector< _Tp, _Alloc > && __rv, const
__type_identity_t< allocator_type > & __m) [inline], [constexpr],
[noexcept]
Move constructor with alternative allocator. moving, but with your allocator this time. iconic behavior honestly π
β vector(initializer_list<value_type>, const allocator_type&) β the curly-brace one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::vector (initializer_list< value_type > __l,
const allocator_type & __a = allocator_type()) [inline], [constexpr]
Builds a vector from an initializer list. yes the {1, 2, 3} thing, she's right there π
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. efficient queen behavior, we love to see it π
β vector(_InputIterator, _InputIterator, const allocator_type&) β the range one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator , typename =
std::_RequireInputIter<_InputIterator>> constexpr std::vector< _Tp,
_Alloc >::vector (_InputIterator __first, _InputIterator __last, const
allocator_type & __a = allocator_type()) [inline], [constexpr]
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).
Now listen, the efficiency here is iterator-dependent and that's important: 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. so basically: give her good iterators and she'll treat you right π«Ά
References std::__iterator_category().
β destructor
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
std::vector< _Tp, _Alloc >::~vector () [inline], [constexpr],
[noexcept]
The dtor only erases the elements, and β important caveat bestie, read this twice β 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. she cleans up the container, she does not chase your pointers, that's on you π
References std::_Destroy().
π Member Function Documentation π
π§ _M_allocate_and_copy (protected, don't touch unless you're built different)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>>
template<typename _ForwardIterator > constexpr pointer std::vector< _Tp,
_Alloc >::_M_allocate_and_copy (size_type __n, _ForwardIterator __first,
_ForwardIterator __last) [inline], [constexpr], [protected]
Memory expansion handler. Uses the member allocation function to obtain n bytes of memory, and then copies [first,last) into it. she's a [protected] girlie, she's behind the curtain, she's the whole reason it works π
π§ _M_range_check (protected)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::_M_range_check (size_type __n) const
[inline], [constexpr], [protected]
Safety check used only from at(). she's the bouncer at the door, she checks IDs, no exceptions (well β exceptions are literally her job, but you know what I mean) π
References std::vector< _Tp, _Alloc >::size().
Referenced by std::vector< _Tp, _Alloc >::at(), and std::vector< _Tp, _Alloc >::at().
β¨ assign β the range version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator , typename =
std::_RequireInputIter<_InputIterator>> constexpr void std::vector< _Tp,
_Alloc >::assign (_InputIterator __first, _InputIterator __last)
[inline], [constexpr]
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. not a merge, not an append β a full glow-up, she's a different girl now π
References std::__iterator_category().
β¨ assign β the initializer_list version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::assign (initializer_list< value_type >
__l) [inline], [constexpr]
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. same deal as before, she's reborn with new content β¨
β¨ assign β the n-copies-of-val version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::assign (size_type __n, const value_type
& __val) [inline], [constexpr]
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. copies on copies on copies, that's the assignment π
π¦ at β the safe one (non-const)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reference std::vector< _Tp, _Alloc >::at (size_type __n) [inline],
[constexpr]
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__nis 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, unlike some people ([], we're looking at you π) π
References std::vector< _Tp, _Alloc >::_M_range_check().
π¦ at β same but read-only (const)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reference std::vector< _Tp, _Alloc >::at (size_type __n) const
[inline], [constexpr]
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__nis 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. look but don't touch edition, we love a boundaries girlie π«Ά
References std::vector< _Tp, _Alloc >::_M_range_check().
π back β const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reference std::vector< _Tp, _Alloc >::back () const [inline],
[constexpr], [noexcept]
Returns a read-only (constant) reference to the data at the last element of the vector. the final girl, the last one standing π
References std::vector< _Tp, _Alloc >::end().
π back β non-const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reference std::vector< _Tp, _Alloc >::back () [inline], [constexpr],
[noexcept]
Returns a read/write reference to the data at the last element of the vector. and yes you can write to her, go off π
References std::vector< _Tp, _Alloc >::end().
Referenced by std::piecewise_constant_distribution< _RealType >::max(), and std::piecewise_linear_distribution< _RealType >::max(). look at her, doing cross-header community service ππ
π begin β const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_iterator std::vector< _Tp, _Alloc >::begin () const [inline],
[constexpr], [noexcept]
Returns a read-only (constant) iterator that points to the first element in the vector. Iteration is done in ordinary element order. no funny business, just order, front to back πΈ
π begin β non-const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::begin () [inline], [constexpr],
[noexcept]
Returns a read/write iterator that points to the first element in the vector. Iteration is done in ordinary element order. go ahead, mutate her, she can take it π
Referenced by: std::vector< _Tp, _Alloc >::vector(), std::match_results< _Bi_iter, _Alloc >::begin(), std::vector< _Tp, _Alloc >::crend(), std::vector< _Tp, _Alloc >::empty(), std::vector< _Tp, _Alloc >::erase(), std::vector< _Tp, _Alloc >::erase(), std::vector< _Tp, _Alloc >::front(), std::vector< _Tp, _Alloc >::front(), std::vector< _Tp, _Alloc >::insert(), std::vector< _Tp, _Alloc >::insert(), std::vector< _Tp, _Alloc >::insert(), __gnu_parallel::multiway_merge_exact_splitting(), std::operator<=>(), std::vector< _Tp, _Alloc >::operator=(), std::operator==(), std::vector< _Tp, _Alloc >::rend(), and std::vector< _Tp, _Alloc >::rend().
π
capacity()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
size_type std::vector< _Tp, _Alloc >::capacity () const [inline],
[constexpr], [noexcept]
Okay bestie, this one is the how much room do I have check π. It hands you back the total number of elements that the vector can hold before needing to allocate more memory. That's it. It's the closet size, not how many outfits are actually in there. β¨
π cbegin()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_iterator std::vector< _Tp, _Alloc >::cbegin () const [inline],
[constexpr], [noexcept]
Gives you a read-only (constant) iterator that points to the first element in the vector. Iteration happens in ordinary element order β no fancy business. This is the "look but don't touch" pass π
Referenced by: std::vector< _Tp, _Alloc >::erase(), std::vector< _Tp, _Alloc >::erase(), std::vector< _Tp, _Alloc >::insert(), std::vector< _Tp, _Alloc >::insert(), and std::vector< _Tp, _Alloc >::insert().
π¦ cend()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_iterator std::vector< _Tp, _Alloc >::cend () const [inline],
[constexpr], [noexcept]
Same deal as cbegin(), but it's a read-only (constant) iterator that points one past the last element in the vector. Classic one-past-the-end situation, iteration in ordinary element order. It does not point at a real element, bestie β it's the finish line, not a runner π
π§Ή clear()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::clear () [inline], [constexpr],
[noexcept]
Erases all the elements. Poof, gone, the closet is empty πΈ
Hold up though β 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 your responsibility. Not mine, not the vector's. Yours. π
π crbegin()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reverse_iterator std::vector< _Tp, _Alloc >::crbegin () const
[inline], [constexpr], [noexcept]
Returns a read-only (constant) reverse iterator that points to the last element in the vector. Iteration is done in reverse element order β so you're walking backwards through the fit checks π
References: std::vector< _Tp, _Alloc >::end().
π crend()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reverse_iterator std::vector< _Tp, _Alloc >::crend () const
[inline], [constexpr], [noexcept]
Returns a read-only (constant) reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order. It's the far end of the backwards walk, don't expect it to dereference to anything π
References: std::vector< _Tp, _Alloc >::begin().
π«Ά data()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
_Tp * std::vector< _Tp, _Alloc >::data () [inline], [constexpr],
[noexcept]
Returns a pointer such that [data(), data() + size()) is a valid range. And for a non-empty vector, data() == &front() β cute little fact, no cap β¨
πͺ emplace()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>>
template<typename... _Args> constexpr iterator std::vector< _Tp, _Alloc
>::emplace (const_iterator __position, _Args &&... __args) [inline],
[constexpr]
Inserts an object in vector before specified iterator. Main character energy for the variadic pack π
Parameters
__positionβ Aconst_iteratorinto the vector.__argsβ Arguments.
Returns
An iterator that points to the inserted data.
So like, this function will insert an object of type T constructed with T(std::forward<Args>(args)...) before the specified location. Iconic. But note β this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list. Lowkey a glow-up-with-a-cost situation πΈ
π«§ empty()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
bool std::vector< _Tp, _Alloc >::empty () const [inline], [constexpr],
[noexcept]
Returns true if the vector is empty. (Thus begin() would equal end() β vibe check passed β
)
References: std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::end().
Referenced by: std::piecewise_constant_distribution< _RealType >::densities(), std::piecewise_linear_distribution< _RealType >::densities(), std::match_results< _Bi_iter, _Alloc >::end(), std::piecewise_constant_distribution< _RealType >::intervals(), std::piecewise_linear_distribution< _RealType >::intervals(), std::discrete_distribution< _IntType >::max(), std::piecewise_constant_distribution< _RealType >::max(), std::piecewise_linear_distribution< _RealType >::max(), std::piecewise_constant_distribution< _RealType >::min(), std::piecewise_linear_distribution< _RealType >::min(), and std::discrete_distribution< _IntType >::probabilities().
π end() (const version)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_iterator std::vector< _Tp, _Alloc >::end () const [inline],
[constexpr], [noexcept]
Returns a read-only (constant) iterator that points one past the last element in the vector. Ordinary element order, as always. No touching π
π end() (the read/write one)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::end () [inline], [constexpr],
[noexcept]
Returns a read/write iterator that points one past the last element in the vector. Iteration in ordinary element order. This is the one you're allowed to mess with π
Referenced by: std::vector< _Tp, _Alloc >::vector(), std::vector< _Tp, _Alloc >::back(), std::vector< _Tp, _Alloc >::back(), std::vector< _Tp, _Alloc >::crbegin(), std::vector< _Tp, _Alloc >::empty(), std::match_results< _Bi_iter, _Alloc >::end(), __gnu_parallel::multiway_merge_exact_splitting(), std::operator<=>(), std::vector< _Tp, _Alloc >::operator=(), std::operator==(), std::vector< _Tp, _Alloc >::rbegin(), std::vector< _Tp, _Alloc >::rbegin(), and std::vector< _Tp, _Alloc >::resize().
βοΈ erase() β range version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::erase (const_iterator __first,
const_iterator __last) [inline], [constexpr]
Remove a range of elements. Cleaning out a whole section of the closet π§Ή
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 __last prior to erasing (or end()).
This function will erase the elements in the range [__first,__last) and shorten the vector accordingly.
Note: This operation could be expensive, and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the elements β if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. Fr fr, that's on you π
References: std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::cbegin().
ποΈ erase() β single element version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::erase (const_iterator __position)
[inline], [constexpr]
Remove element at given position. Just the one, one single element, no group drama π
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 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, bestie, it did not go away π
References: std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::cbegin().
π front() (const version)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reference std::vector< _Tp, _Alloc >::front () const [inline],
[constexpr], [noexcept]
Returns a read-only (constant) reference to the data at the first element of the vector. First in line, but you can't change its outfit π
References: std::vector< _Tp, _Alloc >::begin().
π front() (the read/write one)
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reference std::vector< _Tp, _Alloc >::front () [inline], [constexpr],
[noexcept]
Returns a read/write reference to the data at the first element of the vector. Now you can change the outfit. Slay π
References: std::vector< _Tp, _Alloc >::begin().
Referenced by: std::piecewise_constant_distribution< _RealType >::min(), and std::piecewise_linear_distribution< _RealType >::min().
π§Ί get_allocator()
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
allocator_type std::_Vector_base< _Tp, _Alloc >::get_allocator () const
[inline], [constexpr], [noexcept]
Get a copy of the memory allocation object. That's the whole thing. A little photocopy of the memory manager, for your records π«Ά
Referenced by: std::match_results< _Bi_iter, _Alloc >::get_allocator().
π₯ insert() β range version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator , typename =
std::_RequireInputIter<_InputIterator>> constexpr iterator std::vector<
_Tp, _Alloc >::insert (const_iterator __position, _InputIterator
__first, _InputIterator __last) [inline], [constexpr]
Inserts a range into the vector. Bulk upload energy β¨
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. It's giving "great for vibes, mid for performance" π
References: std::__iterator_category(), std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::cbegin().
π₯ insert() β single value version
template<typename _Tp , typename _Alloc > constexpr vector< _Tp, _Alloc
>::iterator vector::insert (const_iterator __position, const value_type
& __x) [constexpr]
Inserts given value into vector before specified iterator. One (1) little guy, placed with intent π
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. You've heard this before, and you'll hear it again π₯
References: std::begin(), std::cbegin(), std::end(), and std::move().
π₯ insert() β initializer_list version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::insert (const_iterator __position,
initializer_list< value_type > __l) [inline], [constexpr]
Inserts an initializer_list into the vector. Brace-init besties, this one's for you π
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. π
Fair warning bestie: this kind of operation could be expensive for a vector, and if it is frequently used the user should consider using std::list. π fr fr, that's the official advice, not me being dramatic.
References: std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::cbegin().
πΈ insert β the "n copies" edition
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
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. π«Ά
References: std::vector< _Tp, _Alloc >::begin(), and std::vector< _Tp, _Alloc >::cbegin().
π¦ insert β the rvalue edition
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
iterator std::vector< _Tp, _Alloc >::insert (const_iterator __position,
value_type && __x) [inline], [constexpr]
Inserts given rvalue into vector before specified iterator. yep, moving-coded π
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. π
References: std::move().
β max_size
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
size_type std::vector< _Tp, _Alloc >::max_size () const [inline],
[constexpr], [noexcept]
Returns the size() of the largest possible vector. that's the ceiling, the limit, the "no further" π€
π operator= β the copy assignment one
template<typename _Tp , typename _Alloc > constexpr vector< _Tp, _Alloc > &
vector::operator= (const vector< _Tp, _Alloc > & __x) [constexpr]
Vector assignment operator. classic copy-paste-that-vibe energy π
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. (only the real ones show up to the party, the empty chairs stay home πͺ)
Whether the allocator is copied depends on the allocator traits. lowkey important, don't skip that line.
References: std::__addressof(), std::_Destroy(), std::vector< _Tp, _Alloc >::begin(), std::begin(), std::vector< _Tp, _Alloc >::end(), std::end(), std::vector< _Tp, _Alloc >::size(), and std::size().
π operator= β the initializer_list one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
vector & std::vector< _Tp, _Alloc >::operator= (initializer_list<
value_type > __l) [inline], [constexpr]
Vector list assignment operator. so v = {1, 2, 3}; coded π
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. like, total glow-up, not a single old element survives π
π operator= β the move assignment one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
vector & std::vector< _Tp, _Alloc >::operator= (vector< _Tp, _Alloc > &&
__x) [inline], [constexpr], [noexcept]
Vector move assignment operator. fast, efficient, no copying π
Parameters
__xβ a vector of identical element and allocator types.
The contents of __x are moved into this vector (without copying, if the allocators permit it). Afterwards __x is a valid, but unspecified vector. β οΈ that means "it exists, don't assume anything else about it" β she's alive but she's not the same girl.
Whether the allocator is moved depends on the allocator traits.
References: std::move().
π operator[] β the const one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reference std::vector< _Tp, _Alloc >::operator[] (size_type __n)
const [inline], [constexpr], [noexcept]
Subscript access to the data contained in the vector. read-only mode, look don't touch π
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. Note that data access with this operator is unchecked and out_of_range lookups are not defined. π (For checked lookups see at().) ngl bestie, unfiltered and unguarded β she will not protect you.
πΈ operator[] β the non-const one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reference std::vector< _Tp, _Alloc >::operator[] (size_type __n)
[inline], [constexpr], [noexcept]
Subscript access to the data contained in the vector. this one you can touch π
Parameters
__nβ the index of the element for which data should be accessed.
Returns β read/write reference to data.
This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. π (For checked lookups see at().) same energy, same warning, no cap.
π¦ pop_back
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::pop_back () [inline], [constexpr],
[noexcept]
Removes last element. This is a typical stack operation. It shrinks the vector by one. bye bestie π
Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called. β οΈ like grab the info first, because once she's gone she's gone.
β¨ push_back β the lvalue-ish one
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::push_back (const value_type & __x)
[inline], [constexpr]
Add data to the end of the vector. party's at the back π
Parameters
__xβ data to be added.
This is a typical stack operation. The function creates an element at the end of the vector and assigns the given data to it. Due to the nature of a vector this operation can be done in constant time if the vector has preallocated space available. π₯ that "if" is doing a lot of work, keep an eye on it.
π rbegin β const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reverse_iterator std::vector< _Tp, _Alloc >::rbegin () const
[inline], [constexpr], [noexcept]
Returns a read-only (constant) reverse iterator that points to the last element in the vector. Iteration is done in reverse element order. going backwards, staying unbothered πΆββοΈ
References: std::vector< _Tp, _Alloc >::end().
β rbegin β non-const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reverse_iterator std::vector< _Tp, _Alloc >::rbegin () [inline],
[constexpr], [noexcept]
Returns a read/write reverse iterator that points to the last element in the vector. Iteration is done in reverse element order. same walk, but you can edit as you go π
References: std::vector< _Tp, _Alloc >::end().
π«Ά rend β const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
const_reverse_iterator std::vector< _Tp, _Alloc >::rend () const
[inline], [constexpr], [noexcept]
Returns a read-only (constant) reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order. the finish line of the backwards walk π
References: std::vector< _Tp, _Alloc >::begin().
π§ rend β non-const version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
reverse_iterator std::vector< _Tp, _Alloc >::rend () [inline],
[constexpr], [noexcept]
Returns a read/write reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order.
References: std::vector< _Tp, _Alloc >::begin().
π reserve
template<typename _Tp , typename _Alloc > constexpr void vector::reserve
(size_type __n) [constexpr]
Attempt to preallocate enough memory for specified number of elements. planning ahead, main character energy β¨
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. β οΈ overreaching gets you yeeted.
The advantage of this function is that if optimal code is a necessity and the user can determine the number of elements that will be required, the user can reserve the memory in advance, and thus prevent a possible reallocation of memory and copying of vector data. π₯ big win, no cap.
References: std::_Destroy(), and std::size().
π resize β the one-argument version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::resize (size_type __new_size) [inline],
[constexpr]
Resizes the vector to the specified number of elements. trim or extend, dealer's choice π
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 is truncated, otherwise default constructed elements are appended. π±
References: std::vector< _Tp, _Alloc >::size().
Referenced by __gnu_parallel::__shrink_and_double(), and __gnu_parallel::multiway_merge_exact_splitting().
π resize β the two-argument version
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::resize (size_type __new_size, const
value_type & __x) [inline], [constexpr]
Resizes the vector to the specified number of elements. but this time the newbies get a whole personality π
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 is truncated, otherwise the vector is extended and new elements are populated with given data.
References: std::vector< _Tp, _Alloc >::end(), and std::vector< _Tp, _Alloc >::size().
π¦ shrink_to_fit
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::shrink_to_fit () [inline], [constexpr]
A non-binding request to reduce capacity() to size(). β οΈ key word: non-binding β she's asking politely, not demanding, so don't be shocked if the capacity lowkey stays the same. π
π size
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
size_type std::vector< _Tp, _Alloc >::size () const [inline],
[constexpr], [noexcept]
Returns the number of elements in the vector. the headcount, the guest list π
Referenced by __gnu_parallel::__shrink(), __gnu_parallel::__shrink_and_double(), std::vector< _Tp, _Alloc >::_M_range_check(), __gnu_parallel::list_partition(), std::discrete_distribution< _IntType >::max(), std::vector< _Tp, _Alloc >::operator=(), std::operator==(), std::vector< _Tp, _Alloc >::resize(), and std::vector< _Tp, _Alloc >::resize(). (yes, resize is in there twice, that's literally what the page says π« )
π swap
template<typename _Tp , typename _Alloc = std::allocator<_Tp>> constexpr
void std::vector< _Tp, _Alloc >::swap (vector< _Tp, _Alloc > & __x)
[inline], [constexpr], [noexcept]
Swaps data with another vector. trading lives, iconic behavior π
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 to this function.
Whether the allocators are swapped depends on the allocator traits. lowkey load-bearing detail, don't sleep on it.
Referenced by std::match_results< _Bi_iter, _Alloc >::swap().
β¨ AUTHOR β¨
So like, nobody actually wrote this page with their human hands, bestie β it was generated automatically by Doxygen for libstdc++ from the source code. The docs robot did its little thing and spat this whole page out. Iconic behaviour honestly π«Ά
And that's the whole page, babes! π You made it to the end β main character energy fr fr π
This little masterpiece lives in the libstdc++ docs, filed under the extremely long and fancy name std::vector< _Tp, _Alloc >(3cxx), and it was auto-generated by Doxygen straight from the source code β no cap, not a single human typed it πβ¨
Now go forth and push_back responsibly, bestie πΈπ¦π
π¦ where this page comes from
- ubuntu noble: libstdc++-14-doc 14-20240412-0ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms