FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
C++Filesystem operations

Copy a file without replacing an existing destination

C++: The default copy_file policy refuses to overwrite an existing destination.

C++✓ Tested
C++Sequence algorithms

Count elements satisfying a predicate

C++: count_if counts matches without constructing a filtered collection.

C++✓ Tested
C++Ordering & set operations

Count occurrences in a sorted sequence

C++: equal_range finds the half-open interval containing values equal to the key.

C++✓ Tested
C++Getting Started

Count the items in a collection

C++: Read the collection’s length instead of hard-coding the loop limit.

C++✓ Tested
C++Collections

Count word frequencies

Accumulate word counts in an ordered C++ map.

C++✓ Tested
C++Files & paths

Create nested directories

Use C++17 filesystem to create a directory tree.

C++✓ Tested
C++Filesystem operations

Delete one known scratch file

C++: filesystem::remove reports whether an item was removed and also supports empty directories.

C++✓ Tested
C++Tasks & synchronization

Deliver a result through a promise and future

C++: A promise sets a shared state and its future retrieves the value.

C++✓ Tested
C++Getting Started

Divide using a fractional number

C++: Use a floating-point operand to preserve the fractional part of a division.

C++✓ Tested
C++Getting Started

Explain a calculation with a comment

C++: Keep the code executable while documenting why a fixed input is used.

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

Extract a substring with an explicit count

C++: The second substr argument is a length rather than an ending index.

C++✓ Tested
C++Ordering & set operations

Find a median element without fully sorting

C++: nth_element partitions around the element that would occupy the chosen sorted position.

C++✓ Tested