range-v3 for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to range-v3: what it is and why you need it

\n\n

The range-v3 library is a powerful, modern library for working with data sequences in C++, developed by Eric Niebler. It became the precursor and prototype for the C++20 Ranges standard library. range-v3 provides convenient, expressive, and safe tools for processing collections, vectors, lists, and any other iterable objects, using the concept of "lazy evaluation" and algorithm composition.

\n\n

Why is this library needed? Traditional STL algorithms (std::transform, std::copy_if, etc.) work with iterators, which often leads to cumbersome and hard-to-read code. range-v3 solves this problem by allowing you to work with entire ranges as single objects. You can chain operations in pipelines without creating temporary containers, and write code that looks like a sequence of steps rather than nested function calls.

\n\n

Key advantages:

\n
    \n
  • Expressiveness: code becomes like a declarative description of what needs to be done, rather than how to do it.
  • \n
  • Laziness: many operations (filtering, transformation) are performed only when actually needed, improving performance.
  • \n
  • Composition: easily combine different algorithms using the | (pipe) operator.
  • \n
  • Safety: fewer errors related to out-of-bounds access and incorrect iterator usage.
  • \n
  • Compatibility: works with any STL containers and user-defined types that support iteration.
  • \n
\n\n

Installing range-v3

\n\n

Unlike Python or JavaScript, C++ does not have a single package manager like pip or npm. However, range-v3 is a header-only library (consists only of header files), which makes its installation very simple.

\n\n

Method 1: Vcpkg (recommended for Windows, Linux, macOS)

\n

Vcpkg is a cross-platform package manager for C++.

\n
# Install vcpkg if you haven't already\ngit clone https://github.com/Microsoft/vcpkg.git\ncd vcpkg\n./bootstrap-vcpkg.sh  # or bootstrap-vcpkg.bat for Windows\n\n# Install range-v3\n./vcpkg install range-v3\n\n# Connect in your project (CMake):\n# find_package(range-v3 CONFIG REQUIRED)\n# target_link_libraries(my_project PRIVATE range-v3::range-v3 range-v3::meta)
\n\n

Method 2: Conan

\n

Conan is another popular package manager.

\n
# Install Conan\npip install conan\n\n# Add to conanfile.txt:\n[requires]\nrange-v3/0.12.0\n\n[generators]\ncmake\n\n# Then in CMakeLists.txt:\n# find_package(range-v3 CONFIG REQUIRED)\n# target_link_libraries(my_project PRIVATE range-v3::range-v3 range-v3::meta)
\n\n

Method 3: Manual installation (download and copy)

\n

Since the library is header-only, you can simply download it from GitHub and add the include path to your project.

\n
git clone https://github.com/ericniebler/range-v3.git\n# Then in your compiler settings specify:\n# -I /path/to/range-v3/include
\n\n

Method 4: CMake FetchContent (modern approach)

\n

This method automatically downloads the library during build time.

\n
include(FetchContent)\nFetchContent_Declare(\n  range-v3\n  GIT_REPOSITORY https://github.com/ericniebler/range-v3.git\n  GIT_TAG 0.12.0\n)\nFetchContent_MakeAvailable(range-v3)\n\ntarget_link_libraries(my_project PRIVATE range-v3::range-v3 range-v3::meta)
\n\n

Quick start: first example

\n\n

Let's write a simple program that demonstrates the basic capabilities of range-v3. We'll create a vector of numbers, filter even numbers, multiply them by 2, and output the result.

\n\n
#include <iostream>\n#include <vector>\n#include <range/v3/all.hpp>\n\                    

Recommendations