std::optional::operator-> — access a member of the contained value
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.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
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.
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.