std::views::enumerate — iteration with an index
C++23 std::views::enumerate creates a lazy view that associates each range element with a zero-based index. It lets a loop access both the index and the element without maintaining a separate counter.
CKTIS / C++
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
C++23 std::views::enumerate creates a lazy view that associates each range element with a zero-based index. It lets a loop access both the index and the element without maintaining a separate counter.
std::views::filter creates a lazy view of elements satisfying a predicate. It does not build a separate result container and preserves the order of selected elements.
std::views::iota creates a lazy view of values obtained by incrementing. The two-argument form excludes its end bound, while the one-argument form has no reachable end.
std::views::join creates a lazy view of elements from a range of ranges. It flattens one level, preserves order, and skips empty inner ranges without building a new container.
std::views::keys creates a lazy view of the index-0 elements in a range of pairs or tuples. It lets you traverse map keys, but also works with other compatible ranges without creating a separate collection of keys.
std::views::reverse exposes a range's elements from last to first without rearranging the source. It requires a range that can be converted into a bidirectional view. If the view exposes mutable references, writing through them changes the original elements.
C++23 std::views::slide creates a lazy view of consecutive, overlapping windows of length n. The windows refer to elements in the underlying range rather than forming separate containers. It requires at least a forward_range and n > 0.
std::views::split creates a view of parts separated by an element or pattern. Parts refer to the underlying data rather than becoming separate strings; adjacent and trailing delimiters can produce empty parts.
std::views::take, declared in <ranges>, creates a view of at most count leading elements of a range. It preserves their order, does not create a separate container, and supports both direct calls and pipeline notation.
std::views::take_while creates a lazy view of the initial range elements satisfying a predicate. It ends before the first element that fails the condition and does not resume searching for later matches.
std::views::transform creates a view that computes a function's result for an element only when that element is accessed. It does not create a result container; use it directly or in a pipeline with the | operator.
std::views::values creates a lazy view of index-1 elements from pairs or tuples. It lets you traverse map values or modify the second fields of pairs without creating a separate container.
std::views::zip creates a lazy view of elements at corresponding positions in several ranges. Iteration stops at the shortest range, and reference access lets you modify mutable source elements.
std::visit, declared in <variant>, invokes a callable with the active value from each supplied std::variant object. With no variants, std::visit(visitor) invokes visitor with no arguments if that invocation is valid. Alternatives are selected at runtime, but the invocation is checked at compile time for every combination of alternatives.
std::void_t from <type_traits> maps any valid pack of type arguments to void. Combined with SFINAE, it can detect whether a nested type is available without making the entire program ill-formed.
std::weak_ptr<T> observes an object managed by std::shared_ptr without keeping it alive. Its lock() member obtains a temporary shared owner or an empty std::shared_ptr if ownership has already expired.
std::wstring is an alias for std::basic_string<wchar_t>, defined in <string>. It manages string storage and supports appending, comparison, and element access. It does not guarantee a portable Unicode encoding.
PImpl moves a class's data into a hidden implementation object. It reduces header dependencies and helps preserve the public class layout, but does not guarantee ABI compatibility by itself. The key mechanics are an incomplete nested type and an out-of-line destructor.
An allocator determines how a container obtains and releases storage. std::pmr resources let you select allocation behavior at runtime without changing the container type, but require careful ordering of object lifetimes.
Program architecture defines responsibilities, dependency direction, and object ownership. The example combines strategy pattern, factory registry, dependency injection, and decorator, while treating singleton as a separate decision about access and lifetime.
Asynchrony separates submitting work from obtaining its result. Concurrency lets tasks make progress over overlapping periods, but does not guarantee parallel execution. In C++, std::async and std::future support this separation with an explicit waiting point.
A class can define its own operator new and operator delete to control storage allocation and release. Overloads taking std::align_val_t handle alignment and must cooperate with the appropriate storage-release function.
A collection is a general term for a group of elements. The C++ standard library provides containers with different storage and access rules: sequences, associative containers, and adaptors.
C++ provides // comments extending to the end of a line and /* ... */ block comments. They let you explain code and write documentation without executing their contents. Block comments cannot be nested.