Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Class sub_range

The sub_range class inherits all its functionality from the iterator_range class. The sub_range class is often easier to use because one must specify the Forward Range template argument instead of an iterator. Moreover, the sub_range class can propagate constness since it knows what a corresponding const_iterator is.

Synopsis

namespace boost
{
    template< class ForwardRange >
    class sub_range : public iterator_range< typename range_iterator<ForwardRange>::type >
    {
    public: 
        typedef typename range_iterator<ForwardRange>::type iterator;
        typedef typename range_iterator<const ForwardRange>::type  const_iterator;
        typedef typename iterator_difference<iterator>::type difference_type;
    
    public: // construction, assignment
        template< class ForwardTraversalIterator >
        sub_range( ForwardTraversalIterator Begin, ForwardTraversalIterator End );

        template< class ForwardRange2 >
        sub_range( ForwardRange2& r );
         
        template< class ForwardRange2 >
        sub_range( const Range2& r );
         
        template< class ForwardRange2 >
        sub_range& operator=( ForwardRange2& r );

        template< class ForwardRange2 >
        sub_range& operator=( const ForwardRange2& r );    
    
    public:  // Forward Range functions 
        iterator        begin();
        const_iterator  begin() const;
        iterator        end();
        const_iterator  end() const;    
        
    public: // convenience 
        value_type&       front();
        const value_type& front() const;
        value_type&       back();
        const value_type& back() const;
        // for Random Access Range only: 
        value_type&       operator[]( size_type at );
        const value_type& operator[]( size_type at ) const;
    
    public:
        // rest of interface inherited from iterator_range
    };
    
} // namespace 'boost'

The class should be trivial to use as seen below. Imagine that we have an algorithm that searches for a sub-string in a string. The result is an iterator_range, that delimits the match. We need to store the result from this algorithm. Here is an example of how we can do it with and without sub_range

std::string str("hello");
iterator_range<std::string::iterator> ir = find_first( str, "ll" );
sub_range<std::string>               sub = find_first( str, "ll" );


PrevUpHomeNext