

Views are ranges that are usually defined on another range and transform the underlying range The standard library already provides many containers, see above. Std::ranges::sized_range which requires that the size of a range is retrievable by std::ranges::size()Ĭontainers are the ranges most well known, they own their elements. There are also range concepts that are independent of input or output or one of the above concepts, e.g. if the iterator of a range models std::forward_iterator, than the range is a std::ranges::forward_range.įor the well-known containers from the standard library this matrix shows which concepts they model: These concepts are derived directly from the respective concepts on the iterators, i.e. You can jump to elements in constant-time Įlements are always stored consecutively in memory types that model a stronger concept, always also model the weaker one): ConceptĬan be iterated from beginning to end at least onceĬan be iterated from beginning to end multiple times Input ranges have different strengths that are realised through more a std::vector is both, but a std::vector const would only be an input range. Output ranges (they can be written to) or both.Į.g. Ranges are typically input ranges (they can be read from), There are different ways to classify ranges, the most important one is by the capabilities of its iterator.

Requires only the existence of begin() and end() on the range. Ranges are an abstraction of “a collection of items”, or “something iterable”. Later you will see that this approach offers even more flexibility than working with iterators. That appears like a container and that std::ranges::sort can sort it. We will discuss later what std::views::reverse(v) does, for now it is enough to understand that it returns something sorting only all elements after the fifth one:ġ std :: ranges :: sort ( std :: views :: drop ( std :: views :: reverse ( v ), 5 )).It is more flexible, because it allows e.g.: Why was this design with iterators chosen? If you want to sort a std::vector v, you have to call std::sort(v.begin(), v.end()) and not std::sort(v). Traditionally most generic algorithms in the C++ standard library, like std::sort, take a pair of iterators If you do, you need to replace the std::ranges:: prefixes with just ranges:: and any std::views:: prefixes Range-v3 library if you want to try any of this. Since none of the large standard libraries ship C++ Ranges right now, you need to use the There is also beginner’s documentation on C++ Concepts over there. This article is based on library documentation that I wrote for the SeqAn3 library. Good Concepts, Wikipedia (although both contain slightly outdated syntax). There are various resources on C++ Concepts, e.g. You don’t need to have any prior knowledge of C++ Ranges, but you should have basic knowledge of C++ iterators and you should have heard of C++ Concepts before. This article is a short introduction for programmers that are new to C++ Ranges. C++ Ranges are one of the major new things in C++20 and “views” are a big part of ranges.
