0Pricing
C++ Academy · Lesson

C++20 Ranges Library Introduction

Use the std::ranges algorithms that take ranges instead of iterator pairs.

C++20 Ranges Library Introduction is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Ranges?

Traditional STL takes iterator pairs (v.begin(), v.end()). C++20 ranges take a single range value and add composability.

Including Ranges

Include <ranges>. The namespaces are std::ranges for algorithms and std::views for lazy views.

#include <ranges>
#include <algorithm>

Range Algorithms

Every standard algorithm has a ranges counterpart that takes a range instead of two iterators.

std::vector<int> v = {3, 1, 4, 1, 5};
std::ranges::sort(v);             // sort the whole range
auto it = std::ranges::find(v, 4);

Projection Functions

Range algorithms accept an optional projection — a function applied to each element before comparison. Avoids writing custom comparators.

struct Person { std::string name; int age; };
std::vector<Person> people;
std::ranges::sort(people, {}, &Person::age);
// sort by age, default comparison

What is a Range?

A range is anything that supports begin() and end(). Containers, arrays, and (in C++20) any custom type that exposes a sentinel.

Sentinels

C++20 generalizes end() to a sentinel — does not have to be the same type as the iterator. Useful for input streams and infinite ranges.

Views: Lazy Range Pipelines

A view is a non-owning range that produces elements on demand. Compose them with the pipe operator |.

auto squares = v
    | std::views::filter([](int x) { return x % 2 == 0; })
    | std::views::transform([](int x) { return x * x; });

for (int x : squares) std::cout << x << " ";

Lazy Evaluation

Views do not allocate or run until iterated. filter | transform | take(5) only produces the first 5 transformed values — nothing more.

Borrowed Ranges and Lifetime

Views borrow their source range. If the source dies, the view dangles. Be careful with views over temporaries — though C++20 added safeguards.

Eager vs Lazy

Algorithms produce results eagerly (compute now). Views are lazy (compute on iteration). Choose based on whether you need the result materialized.

Performance

Lazy pipelines avoid intermediate containers. The compiler often optimizes pipelines as if they were a single loop. Inspect with godbolt to verify.

Migration Path

If you cannot use C++20, libraries like Eric Niebler s range-v3 provide the same model in C++14/17. Worth learning today to ease migration.

Quick Check

What is the main advantage of C++20 ranges over the iterator-pair STL style?

Recap

C++20 ranges modernize STL algorithms — pass a single range, use projections, and build lazy pipelines with views. The pipe operator composes filters, transforms, and takes into clear, declarative code.

Frequently asked questions

Is the “C++20 Ranges Library Introduction” lesson free?

Yes — the full text of “C++20 Ranges Library Introduction” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “C++20 Ranges Library Introduction”?

Use the std::ranges algorithms that take ranges instead of iterator pairs. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “C++20 Ranges Library Introduction” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Iterator Categories input forward bidirectional random
  2. Common Iterator Patterns begin end advance
  3. C++20 Ranges Library Introduction
  4. Range Adaptors views::filter transform take
← Back to C++ Academy