The snippet library.
Small, practical examples. Search by task or narrow things down below.
Copy a file without replacing an existing destination
C++: The default copy_file policy refuses to overwrite an existing destination.
C++✓ Tested
Count elements satisfying a predicate
C++: count_if counts matches without constructing a filtered collection.
C++✓ Tested
Count occurrences in a sorted sequence
C++: equal_range finds the half-open interval containing values equal to the key.
C++✓ Tested
Count the items in a collection
C++: Read the collection’s length instead of hard-coding the loop limit.
C++✓ Tested
Count word frequencies
Accumulate word counts in an ordered C++ map.
C++✓ Tested
Create nested directories
Use C++17 filesystem to create a directory tree.
C++✓ Tested
Delete one known scratch file
C++: filesystem::remove reports whether an item was removed and also supports empty directories.
C++✓ Tested
Deliver a result through a promise and future
C++: A promise sets a shared state and its future retrieves the value.
C++✓ Tested
Divide using a fractional number
C++: Use a floating-point operand to preserve the fractional part of a division.
C++✓ Tested
Explain a calculation with a comment
C++: Keep the code executable while documenting why a fixed input is used.
C++✓ Tested
Extract a substring with an explicit count
C++: The second substr argument is a length rather than an ending index.
C++✓ Tested
Find a median element without fully sorting
C++: nth_element partitions around the element that would occupy the chosen sorted position.
C++✓ Tested