boost range zhuan

Official
http://67.223.234.84/boost_doc/libs/range/doc/utility_class.html#sub_range


http://blog.sina.com.cn/s/blog_77eecd340100ru5z.html

range 所有的接口都直接定义在了 namespace boost中。类似于 iterator,range 分为四类:single pass range,forward range,bidirectional range,random access range。 鉴于 std::pair<iterator,iterator> 并不是合适的 range 的候选,boost::range 提供了 iterator_range 和 sub_range 两个类。类 iterator_range 建立在 forward traversal iterator,而类 sub_range 从 iterator_range 继承下来也是 forward range ,且其 template 参数更容易被指定因此更加容易使用。这两个类最大的差别是 sub_range 很容易在 const 中传递,因为很容易确认相应的 const_iterator。 最小的 range 接口要求实现 begin(),end(),size() 成员函数函数,value_type,iterator,const_iterator,difference_type,size_type 的成员 type。 class iterator_range 实现了创建 forward range 最小的 interface。还实现了 empty() 成员函数,类似于 stream 重载了 operator() 用于测试 iterator_range 是否 validation,实现 equal() 测试两个 iterator_range 是否相等,front() 和 end() 用以返回 iterator_range 的第一个和最后一个值。如果是随机访问的还实现了 operator[]。为了方便用户的使用还重载了 <<,==(比较两个 range 是相同的序列),!=,<,还重载实现了类似 make_pair 的 make_iterator_range 函数(或从 range,或者从两个 iterator,或从一个 range 和两个 range_difference<range>::type 构造 iterator_range),copy_range 函数用于复制元素。 类 sub_range 继承了从 iterator_range 的所有的成员,但其只提供了多个构造函数,但是它可以识别 const 和非 const 的对 begin(),end(),front(),end(),operator[] 成员函数的调用。而且 sub_range 是直接调用数据类型,可以自己推导 iterator 的类型。 注意以下的不同的调用方法就可以发现 sub_range 比 iterator_range 更好用。 std::string str("hello"); iterator_range<std::string::iterator> ir = find_first( str, "ll" ); sub_range<std::string> sub = find_first( str, "ll" ); #include <boost/range.hpp> 为了使用的方便,boost 提供了如下的全局函数 begin(),empty(),end(),size(),rbegin(),rend()直接操纵容器。 // Single Pass Range metafunctions template< class T > struct range_value; template< class T > struct range_iterator; template< class T > struct range_const_iterator; // Forward Range metafunctions template< class T > struct range_difference; template< class T > struct range_size; // Bidirectional Range metafunctions template< class T > struct range_reverse_iterator; template< class T > struct range_const_reverse_iterator; // Special metafunctions template< class T > struct range_result_iterator; template< class T > struct range_reverse_result_iterator; // Single Pass Range functions template< class T > typename range_iterator<T>::type begin( T& c ); template< class T > typename range_const_iterator<T>::type begin( const T& c ); template< class T > typename range_iterator<T>::type end( T& c ); template< class T > typename range_const_iterator<T>::type end( const T& c ); template< class T > bool empty( const T& c ); // Forward Range functions template< class T > typename range_size<T>::type size( const T& c ); // Bidirectional Range functions template< class T > typename range_reverse_iterator<T>::type rbegin( T& c ); template< class T > typename range_const_reverse_iterator<T>::type rbegin( const T& c ); template< class T > typename range_reverse_iterator<T>::type rend( T& c ); template< class T > typename range_const_reverse_iterator<T>::type rend( const T& c ); // Special const Range functions template< class T > typename range_const_iterator<T>::type const_begin( const T& r ); template< class T > typename range_const_iterator<T>::type const_end( const T& r ); template< class T > typename range_const_reverse_iterator<T>::type const_rbegin
 http://blog.csdn.net/huang_xw/article/details/8274915

【Boost】boost::range(区间)介绍 分类: [C
++]--[Boost] 2012-12-09 14:32 1561人阅读 评论(0) 收藏 举报 1. 概念 区间的概念类似于STL中的容器概念。一个区间提供了可以访问半开放区间[first,one_past_last)中元素的迭代器,还提供了区间中的元素数量的信息。 1.1 目的 引入区间概念的目的在于:有很多类似于容器的类型,以及用于这些类型的简化算法。 1.2 用于的类型 类似于标准的容器 std::pair<iterator,iterator> 内建数组 2. 示例 构造方法 [cpp] view plaincopyprint? void test_range_construct_string() { typedef std::string::iterator iterator; typedef std::string::const_iterator const_iterator; typedef boost::iterator_range<iterator> irange; typedef boost::iterator_range<const_iterator> cirange; std::string str = "hello world"; const std::string cstr = "const world"; // 1. 基本构建方法 boost::iterator_range<std::string::iterator> ir(str); boost::iterator_range<std::string::const_iterator> cir(str); // 2. 利用make_iterator_range(几种重载函数) irange r = boost::make_iterator_range(str); r = boost::make_iterator_range(str.begin(), str.end()); cirange r2 = boost::make_iterator_range(cstr); r2 = boost::make_iterator_range(cstr.begin(), cstr.end()); r2 = boost::make_iterator_range(str); assert(r == str); assert(r.size() == 11); irange r3 = boost::make_iterator_range(str, 1, -1); assert(boost::as_literal("ello worl") == r3); irange r4 = boost::make_iterator_range(r3, -1, 1); // 这个也可以理解成复制构造 assert(str == r4); std::cout << r4 << std::endl; irange r5 = boost::make_iterator_range(str.begin(), str.begin() + 5); assert(r5 == boost::as_literal("hello")); } 类型变化 [cpp] view plaincopyprint? void test_range_type() { using namespace boost; // 数组 const int SIZE = 9; typedef int array_t[SIZE]; const array_t ca = {1, 2, 3, 4, 5, 6, 7, 8, 10}; assert((is_same<range_iterator<array_t>::type, int* >::value)); assert((is_same<range_value<array_t>::type, int >::value)); assert((is_same<range_difference<array_t>::type, std::ptrdiff_t>::value)); assert((is_same<range_size<array_t>::type, std::size_t >::value)); assert((is_same<range_const_iterator<array_t>::type, const int* >::value)); assert(begin(ca) == ca); assert(end(ca) == ca + size(ca)); assert(empty(ca) == false); }
http://boost.2283326.n4.nabble.com/Iterator-Range-sub-range-of-a-desired-size-td4637719.html

Is there a function in BOOST, given a starting point and a desired size, that returns iterators for a (sub) range? For example, given: a boost::circular_buffer which contains: 1 2 3 4 5 6 7 8 9 10 a starting point which is an iterator pointing to 5 and finally a desired size which is an int s = 4 The result of which would be iterators producing: 5 6 7 8 A possible implementation might be this (but I was hoping there was a BOOST version since I can't find an STD algorithm) template <typename Itr> std::pair<Itr, Itr> getWindowDown(Itr begin, Itr end, Itr start, const int windowSize) { // Down first, then up if need be const Itr bottom = (end - start > windowSize) ? (start + windowSize) : (end); const Itr top = (bottom - begin > windowSize) ? (bottom - windowSize) : (begin); return make_pair(top, bottom); } Thanks, Remove Ads Neil Groves-3 Reply | Threaded | More Oct 29, 2012; 3:33pm Re: Iterator Range, sub range of a desired size Neil Groves-3 320 posts On Mon, Oct 29, 2012 at 4:40 AM, David Kimmel <[hidden email]> wrote: Is there a function in BOOST, given a starting point and a desired size, that returns iterators for a (sub) range? There are two classes in Boost.Range that achieve this purpose. The first, boost::iterator_range<Iterator> can be used to hold sub-ranges. It is not directly constructible from the form firstIterator, count since this would either require that the Iterator is a model of the RandomAccessConcept or would provide sloppy performance guarantees. This behaviour is easily achieved by constructing the iterator_range with boost::make_iterator_range(boost::next(buffer.begin(), 4), buffer.end()). Additionally there is the boost::sub_range<Range> which is very similar to iterator_range except that it preserves const-ness of the parent range. http://www.boost.org/doc/libs/1_51_0/libs/range/doc/html/range/reference/utilities/iterator_range.html http://www.boost.org/doc/libs/1_51_0/libs/range/doc/html/range/reference/utilities/sub_range.html Thanks, Regards, Neil Groves
原文地址:https://www.cnblogs.com/huashiyiqike/p/3909254.html