The snippet library.
Small, practical examples. Search by task or narrow things down below.
Run a task on a thread and join it
C++: Join before reading a result written by the worker thread.
C++✓ Tested
Run cleanup through a scoped destructor
C++: A stack object’s destructor runs when its scope exits, including normal exception unwinding.
C++✓ Tested
Run your first program
C++: Start with a complete program that prints one line. Follow the run instructions below.
C++✓ Tested
Search a sorted vector
Use binary_search after ensuring C++ values are sorted.
C++✓ Tested
Set owner-only read and write permissions
C++: Replace ordinary permission bits with the requested owner permissions.
C++✓ Tested
Share ownership with a counted smart pointer
C++: Copies of shared_ptr keep the owned value alive until the last owner is released.
C++✓ Tested
Sort a vector
Sort C++ integer values in ascending order.
C++✓ Tested
Sort and deduplicate values
Sort a C++ vector and erase adjacent duplicates.
C++✓ Tested
Sort only the smallest requested prefix
C++: partial_sort orders the selected prefix without guaranteeing the order of the remainder.
C++✓ Tested
Sort records by two fields
C++: Tuple comparison implements lexicographic ordering across several keys.
C++✓ Tested
Split comma-separated text
Use getline with a delimiter to split simple C++ text.
C++✓ Tested
Store a reference in a standard container
C++: reference_wrapper preserves reference semantics while behaving like a copyable container element.
C++✓ Tested