Say I have a vector or other container class,
like vector<int> vec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.
Thanks for any suggestion.
You could start with a simple adaptor like this:
template < typename Iterator >
class range {
Iterator start;
Iterator past_end;
public:
typedef typename iterator_traits< Iterator >::size_type size_type;
range ( Iterator const & from, Iterator const & to )
: start ( from )
, past_end ( to )
{}
size_type size ( void ) const {
typename iterator_traits< Iterator >::difference_type
dist = std::distance( this->start, this->past_end );
assert( dist >= 0 );
return ( dist );
}
Iterator begin ( void ) const {
return( start );
}
Iterator end ( void ) const {
return( this->past_end );
}
bool empty ( void ) const {
return( this->start == this->past_end );
}
}; // class range<>
The use of the assert is debatable. Ideally, one would check the validity of
the range upon construction and throw something if [from,to) is not sound.
However, that might be inefficient (for non-random access iterators) or
impossible (if from and to point to different containers).
Best
Kai-Uwe Bux
> Best
>
> Kai-Uwe Bux