Range

Definition

Cgal and the STL heavily use the concepts of iterators and iterator ranges to describe linear sequences of elements, and algorithms operating on these.

The Range concept aims at encapsulating an iterator range, by providing access to the first and past-the-end iterators of a range. The advantage is that the syntax for passing ranges is much more concise than passing two arguments separately.

Ranges come in different categories depending on the category of their iterator : mutable or constant (modifiability of the elements pointed to), and forward, bidirectional or random-access. The category can be queried using std::iterator_traits and the corresponding iterator type. Note that the concepts Range and ConstRange do not require anything on the category or the value type of the iterator. It must be precised in the documentation of any model of these concepts. For example, in the case of a vector of points, one would say: This type is a model of Range concept, its iterator type is random-access and its value type is Point.

Boost also offers the Boost.Range library which provides good support for ranges.

Finally, let us note that ranges, in general (especially in template context) need to be passed and returned by (const) reference for efficiency. This is a difference with iterators which are typically passed by value.

Refines

ConstRange

Boost's Range concept

Types

Range::const_iterator
The constant iterator type.


Range::iterator
The iterator type. It must be convertible to const_iterator.


Range::size_type
An unsigned integral type that can represent the size of a range.

Member functions

const_iterator r.begin () const returns the const iterator pointing to the first element.
const_iterator r.end () const returns the past-the-end const iterator.
iterator r.begin () returns the iterator pointing to the first element.
iterator r.end () returns the past-the-end iterator.

size_type r.size () const returns the size of the range.
bool r.empty () const returns whether the range is empty.

Has Models

STL containers