# Boost.Range

## 簡介

• 泛型的區間算法的更簡單的實現和規範
• 更靈活、緊湊和可維護的客戶代碼
• 安全使用內建數組

```
//
// 例子：在泛型算法中取出邊界
//
template< class ForwardReadableRange, class T >    inline typename boost::range_iterator< ForwardReadableRange >::type    find( ForwardReadableRange& c, const T& value )    {       return std::find( boost::begin( c ), boost::end( c ), value );    }    template< class ForwardReadableRange, class T >    inline typename boost::range_const_iterator< ForwardReadableRange >::type    find( const ForwardReadableRange& c, const T& value )    {       return std::find( boost::begin( c ), boost::end( c ), value );    }    //    // 替代第一個值並返回其索引    //    template< class ForwardReadableWriteableRange, class T >    inline typename boost::range_size< ForwardReadableWriteableRange >::type    my_generic_replace( ForwardReadableWriteableRange& c, const T& value, const T& replacement )    {       typename boost::range_iterator< ForwardReadableWriteableRange >::type found = find( c, value );       if( found != boost::end( c ) )           *found = replacement;       return std::distance( boost::begin( c ), found );    }    //    // 用法    //    const int N = 5;    std::vector<int> my_vector;    int values[] = { 1,2,3,4,5,6,7,8,9 };
my_vector.assign( values, boost::end( values ) );
typedef std::vector<int>::iterator iterator;    std::pair<iterator,iterator>       my_view( boost::begin( my_vector ),                                                boost::begin( my_vector ) + N );    char  str_val[] = "a string";    char* str       = str_val;    std::cout << my_generic_replace( my_vector, 4, 2 );    std::cout << my_generic_replace( my_view, 4, 2 );    std::cout << my_generic_replace( str, 'a', 'b' );
// 打印出 '3', '5' 和 '0'
```