|
Boost : |
From: Kevin S. Van Horn (kevin.vanhorn_at_[hidden])
Date: 2001-10-26 13:09:34
On Fri, 26 Oct 2001, Peter Dimov wrote:
> Not only that. A range allows me to (quickly for random access iterators)
> retrieve the middle element, or iterate backwards, while a (first, pred)
> construct does not.
>
> For input (and perhaps forward) iterators, a (first, pred) representation is
> more convenient; random access (and perhaps bidirectional) iterators like
> [first, last).
This raises an interesting thought. It would be nice to be able to use
[first, last) or [first, count), whichever is more convenient, with the
same algorithm. For example, the following would allow sort(first, last)
or sort(first, n):
template <typename RandIter, typename T>
RandIter enditer(RandIter it, T end)
{
return enditer_aux<RandIter, T>::fct(it, end);
}
template <typename RandIter, typename T>
struct enditer_aux
{
static RandIter fct(RandIter it, T n) {
boost::function_requires<IntegerConcept<T> >();
return it + n;
}
};
template <typename RandIter>
struct enditer_aux<RandIter, RandIter>
{
static RandIter fct(RandIter it, RandIter itend) {
return itend;
}
};
template <typename RandIter, typename T>
void sort(RandIter it, T end)
{
RandIter itend = enditer(it, end);
...
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk