Ch8: Algorithms

Overview

This chapter introduces algorithms in modern C++. An algorithm is a reusable operation that works on a range of elements. Instead of writing loops manually, you can sort, search, count, copy, or transform entire containers with a single function call.

All containers introduced so far — ::fast_io::vector, ::fast_io::string, ::fast_io::array, ::fast_io::span, ::fast_io::string_view, ::fast_io::cstring_view, ::fast_io::index_span, and C‑style arrays — are contiguous ranges. Algorithms work naturally on these.

This chapter focuses on the structure behind algorithms: how they operate on ranges, how range and iterator categories determine what algorithms can do, and how projections let algorithms operate on derived values. Later sections introduce typical algorithms and the mathematical models used to describe their performance.

This book avoids the terms “zero‑overhead” and “zero‑cost”. Without context, these terms are meaningless. They ignore real costs such as binary size, memory usage, cache behavior, network transfer, update latency, and human complexity.

Contents