|
Boost : |
From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-12-09 22:09:49
How about a free-function solution instead?
template <typename Container>
typename boost::reference_iterator_generator<
typename Container::iterator>::type
rbegin(Container& c) {
return make_reverse_iterator(c.end());
}
// similarly for rend and the const versions...
Cheers,
Jeremy
On Sun, 9 Dec 2001, Dan Gohman wrote:
> Here's a class that wraps std::list, acting just like std::list except
> using the boost reverse iterators for rbegin and rend. One could wrap
> other containers in a similar manner. The primary benefit is that the
> boost reverse iterators handle interactions between const and non-const
> iterators better, which makes them more convenient.
>
> Would this be a useful thing, or should one just get in the habit of
> using make_reverse_iterator all the time? The reverse iteration stuff
> arguably doesn't belong in the container classes in the first place, so
> maybe the make_reverse_iterator way is better after all.
>
>
> #include <list>
> #include <boost/iterator_adaptors.hpp>
>
> template<class Type, class Alloc = std::allocator<Type> >
> class list : public std::list<Type, Alloc> {
> private:
> typedef std::list<Type, Alloc> parent_type;
> public:
> typedef boost::reverse_iterator_generator<parent_type::iterator>::type reverse_iterator;
> typedef boost::reverse_iterator_generator<parent_type::const_iterator>::type const_reverse_iterator;
>
> list() {}
> list(size_type n): parent_type(n) {}
> list(size_type n, Type const& t): parent_type(n, t) {}
> list(parent_type const& other): parent_type(other) {}
> template<class InputIterator> list(InputIterator f, InputIterator l): parent_type(f, l) {}
>
> reverse_iterator rbegin() { return reverse_iterator(parent_type::end()); }
> reverse_iterator rend() { return reverse_iterator(parent_type::begin()); }
> const_reverse_iterator rbegin() const { return reverse_iterator(parent_type::end()); }
> const_reverse_iterator rend() const { return reverse_iterator(parent_type::begin()); }
> };
>
> --
> Dan Gohman
> gohmandj_at_[hidden]
>
> Info: http://www.boost.org Send unsubscribe requests to: <mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
----------------------------------------------------------------------
Jeremy Siek http://www.osl.iu.edu/~jsiek/
Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
C++ Booster (http://www.boost.org) office phone: (812) 855-3608
----------------------------------------------------------------------
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk