std::basic_string::reserve — capacity versus size
`reserve(n)` makes room for future elements without changing how many exist. `resize(n)` changes the element count in both strings and vectors.
CKTIS / C++
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
`reserve(n)` makes room for future elements without changing how many exist. `resize(n)` changes the element count in both strings and vectors.
`size()` counts stored elements, not user-perceived characters. Use it to bound copies, check indices, and trim fields without unsigned wraparound.
Extract part of a string into a new owning string. Learn how the length is capped, when the start position throws, and what the C++23 rvalue overload changes.
The path slash operator returns a new path without changing either operand. The right operand’s syntax determines whether it appends to or replaces some or all of the left path.
Set the `std::ios::boolalpha` flag with `setf` to read and write `bool` values using the stream’s locale-specific names rather than numeric representations.
Learn when copying bytes preserves an object's value in C++23, and why that guarantee does not make byte copying a substitute for copying arbitrary objects.
Use `std::nullopt` to return or set an empty `std::optional` without confusing a missing result with a valid value such as zero.
Check at compile time whether the implementation’s `float` conforms to IEEE 754, separately from any storage-size or binary-format requirements.
Use `->` to access a member of the value stored in `std::optional`. In C++23, check that the optional contains a value before using the operator.
`*opt` returns a reference to the contained value. Check that `opt` has a value before dereferencing: index `0` is a valid result, not a missing one.
`if (opt)` checks whether a `std::optional` contains a value, not whether that value is true. An index of `0` and a flag of `false` both count as present.
Choose a memory resource for a PMR vector and see when its elements receive that same resource, in contrast with a vector that uses the default allocator.
Check whether a shared stop state has received a request so work can stop at a safe point. A `true` result does not mean the thread has finished.
`std::string::empty` tests whether a string has zero length. It does not tell you whether the first argument position has already been processed when joining strings.
Find the index of a character or substring and compare it with `std::string::npos` before using it as an index or copying or changing the text.
Compare the result of `std::string::find` with `std::string::npos`. As a count in `substr` or `erase`, the same constant means “through the end,” not a starting position.
Use `back()` to read or change the last element of a non-empty vector. Know when the result becomes invalid and why extracting an element from a heap takes two separate steps.
`begin()` returns the vector's starting iterator. The vector's constness selects the overload, and `end()` marks the position you must not dereference.
`std::vector::clear` destroys all elements and sets the vector’s size to zero while keeping its capacity available for later insertions.
Get a pointer to a vector’s elements, pass it with the element count, and know when the pointer is no longer valid.
Construct one element at the end of a vector from its arguments. Learn what the function returns and when appending invalidates existing references.
Use `v.empty()` to check in constant time whether a vector has no elements. Reserved capacity does not affect the result.
`std::vector::end` returns the position just past the last element, not the last element itself. Use it as an exclusive range boundary and to detect a failed search.
`front()` accesses a vector’s first element in constant time. If the vector might be empty, check `empty()` before calling it.