ost Standard Library algorithms operate on sequences whose boundaries are designated by a pair of iterators: one iterator marks the beginning of the sequence and another marks its end. Combining such a pair of iterators into a single object has several advantages in terms of code simplification, safety, and performance. The following sections will explain how to use the new C++09 range library and show some of its advantages.
The following Standard Library algorithms, which I have selected randomly from the <algorithm> header, exhibit a recurring pattern of verbosity and tedium:
template <class ForwardIterator, class T>
void replace(ForwardIterator first,
const T& old_value,
const T& new_value);
template <class InputIterator, class T>
InputIterator find(InputIterator first,
const T& value);
template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first,
These algorithms, as well as std::sort, std::remove
, and so forth, represent the beginning and the end of the sequence on which they operate by a pair of iterators.
Why not combine these two iterators into a single object? That's exactly what a std::range doesit combines two iterators into a single compact and efficient object.