The ranges library is an extension and generalization of the algorithms
and iterator libraries that makes them more powerful by making them composable and less error-prone.
All algorithms that take iterator pairs have overloads that accept ranges (e.g ranges::sort).
The library creates and manipulates range views, lightweight objects that indirectly represent iterable sequences
(ranges). Ranges are an abstraction on top of [begin, end) iterator pairs, e.g. ranges made by implicit conversion
from containers.
The ranges library was originally developed as the range-v3 library, and was finally merged to ISO C++ as of C++20.
The range-v3 implementation is currently available on more compilers and platforms than the C++20 library.
1 2 3 4 5 6 7 8 91011121314
# Ranges in C++20: https://en.cppreference.com/w/cpp/ranges# Use range-v3 for now: https://github.com/ericniebler/range-v3find_package(range-v3QUIET)if(NOTrange-v3_FOUND)FetchContent_Declare(range-v3URLhttps://github.com/ericniebler/range-v3/archive/0.11.0.zip)FetchContent_GetProperties(range-v3)if(NOTrange-v3_POPULATED)FetchContent_Populate(range-v3)add_library(range-v3INTERFACEIMPORTED)target_include_directories(range-v3INTERFACE"${range-v3_SOURCE_DIR}/include")endif()endif()add_executable(rangesranges.cpp)target_link_libraries(rangesPUBLICrange-v3)
std::strings="The range v3 library";std::regexexpr{"[\\w+]+"};autotokenizer=ranges::views::tokenize(s,expr,0,std::regex_constants::match_default);for(auto&x:tokenizer){std::cout<<x<<",";}