FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
C++Getting Started

Store a value in a named variable

C++: Give a value a descriptive name and print it.

C++✓ Tested
C++Containers & compound values

Store and inspect a fixed set of bits

C++: bitset offers named operations and a readable binary representation.

C++✓ Tested
C++Numbers & algorithms

Sum vector values

Use std::accumulate with a wide initial value.

C++✓ Tested
C++Sequence algorithms

Test any and all elements with predicates

C++: These algorithms stop as soon as their result is known.

C++✓ Tested
C++Ownership & optional values

Transfer unique ownership with move

C++: Moving a unique_ptr leaves the source empty and gives ownership to the destination.

C++✓ Tested
C++Sequence algorithms

Transform a vector into a separate result

C++: Pre-size the output before writing through its iterator.

C++✓ Tested
C++Text validation & parsing

Trim surrounding ASCII whitespace

C++: Find the first and last non-whitespace bytes before taking a substring.

C++✓ Tested
C++Containers & compound values

Unpack a tuple with structured bindings

C++: Structured bindings give names to the parts of a returned compound value.

C++✓ Tested
C++Getting Started

Update a running count

C++: Change a variable after its initial value has been assigned.

C++✓ Tested
C++Containers & compound values

Use a fixed-size container with bounds checking

C++: std::array retains its element count and supports checked access through at.

C++✓ Tested
C++Containers & compound values

Use a hash map for direct key lookup

C++: unordered_map provides key lookup without promising iteration order.

C++✓ Tested
C++Containers & compound values

Use a last-in first-out stack

C++: Read top before pop because pop does not return the removed value.

C++✓ Tested