Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-07-26 11:33:19

On Thu, 26 Jul 2001, Ed Brey wrote:
> From: "Jeremy Siek" <jsiek_at_[hidden]>
> >
> > template <class Range>
> > void sort_heap(Range& range)
> > {
> > std::sort_heap(range.begin(), range.end());
> > }
> >
> When will the reference ever need to be non-const? All the underlying
> algorithms take iterators by value, and hence leave them untouched.

The above sort_heap() function is an example when the reference needs to
be non-const.

Suppose we are using the above sort_heap with std::vector.

std::vector<int> x(n);

The elements in the vector will be modified by the algorithm, so we need
mutable iterators. Therefore, the calls to range.begin() and range.end()
better return iterator and not const_iterator. Therefore, we have to pass
by non-const reference in order to get the mutable iterator.


 Jeremy Siek www:
 Ph.D. Candidate, IU B'ton email: jsiek_at_[hidden]
 Summer Manager, AT&T Research phone: (973) 360-8185

Boost list run by bdawes at, gregod at, cpdaniel at, john at