std::vector::insert — insert elements into a vector
Insert a value, repeated copies, an initializer list, or an iterator range before a chosen position. Use the returned iterator and know which existing handles become invalid.
CKTIS / C++
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Insert a value, repeated copies, an initializer list, or an iterator range before a chosen position. Use the returned iterator and know which existing handles become invalid.
`pop_back()` removes the last element of a non-empty vector without returning its value. For a vector that stores a heap, `std::pop_heap` rearranges the elements; `pop_back()` reduces the size.
Append one element by copying or from an rvalue. Learn when growth invalidates pointers and why the old `end()` is invalid even when no reallocation occurs.
`reserve(n)` makes room for at least `n` elements in total without creating any elements. Use it before a batch of appends when you know the final count or a reasonable upper bound.
`size()` returns the current element count in constant time. Use that count, not `capacity()`, to determine which indices are valid.
Exchange two vectors’ contents and capacities in constant time. Check allocator compatibility and learn which iterators remain usable after the swap.
Compare storing data by record and by field, then choose a layout based on access patterns, cache locality, and the cost of converting between layouts.
Use `delete` for an object created with `new T` and `delete[]` for an array created with `new T[n]`. These expressions manage object lifetimes; the corresponding `operator new` and `operator delete` functions, with or without `[]`, manage storage.
The `u8` prefix selects UTF-8 for a string literal. Its length in code units, its number of code points, and its number of grapheme clusters measure different things.
Unary minus negates one operand. In `return -1;`, it produces an `int` value that this function uses to mean “not found”; it is not subtraction.
Unary minus computes the negative of one operand, whereas binary subtraction computes a difference. The promoted type determines what happens at the limits of its range.
Distinguish a literal’s base from its type and from the type used in arithmetic. A `u` suffix alone neither guarantees 32 bits nor forces byte-width arithmetic.
The suffix `u` or `U` selects an unsigned integer type, not a fixed width. `0u` and `1u` are `unsigned int`; a larger value can require a wider type.
`ULL` gives an integer literal the type `unsigned long long` when its value fits, which can change overload selection or how a bit shift is evaluated.
`= delete` prohibits use of a particular function or constructor. The declaration still participates in overload resolution and does not deallocate memory.
Disable copy construction and copy assignment separately, while letting the compiler define a virtual destructor for correct destruction through a base pointer.
UTF-8 text has different lengths depending on what you count: code units, code points or grapheme clusters. The example counts and truncates well-formed UTF-8 without splitting a code point.
Empty braces do not always mean value-initialization. The type determines whether they initialize a scalar, call a constructor, or apply the rules for aggregates.
A structured binding names the components of one object. Choosing `auto`, `auto&`, or `const auto&` changes how that object is stored or referred to.
Give the current `std::map` entry’s key and value separate names so you can read both in a range-based `for` loop without copying the pair.
A forwarding reference T&& lets a template accept lvalues and rvalues while retaining the information needed to forward the argument onward. It requires the appropriate deduction context; && alone is not enough.
A T& reference aliases an object or a function without making a copy. It cannot be reseated after initialization. For object types, const T& restricts modification through the reference and can also bind to a suitable temporary object.
A pointer to member identifies a non-static data member or member function without binding to a particular object. Declare it using C::*, then apply it to an object with .* or through an object pointer with ->*.
The `T*` declarator forms the type “pointer to T”. In `int* p = &value;`, `p` holds a pointer to `value`, not a copy of that integer. The position of `const` determines whether the restriction applies to the pointed-to object or to the pointer itself.