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] -- : 1561人阅读 评论() 收藏 举报
. 概念
区间的概念类似于STL中的容器概念。一个区间提供了可以访问半开放区间[first,one_past_last)中元素的迭代器,还提供了区间中的元素数量的信息。
1.1 目的
引入区间概念的目的在于:有很多类似于容器的类型,以及用于这些类型的简化算法。
1.2 用于的类型
类似于标准的容器
std::pair<iterator,iterator>
内建数组
. 示例
构造方法
[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() == ); irange r3 = boost::make_iterator_range(str, , -);
assert(boost::as_literal("ello worl") == r3);
irange r4 = boost::make_iterator_range(r3, -, ); // 这个也可以理解成复制构造
assert(str == r4);
std::cout << r4 << std::endl; irange r5 = boost::make_iterator_range(str.begin(), str.begin() + );
assert(r5 == boost::as_literal("hello"));
}
类型变化
[cpp] view plaincopyprint?
void test_range_type()
{
using namespace boost; // 数组
const int SIZE = ;
typedef int array_t[SIZE];
const array_t ca = {, , , , , , , , }; 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:
a starting point which is an iterator pointing to
and finally a desired size which is an int s = The result of which would be iterators producing: 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- Reply | Threaded | More
Oct , ; :33pm Re: Iterator Range, sub range of a desired size
Neil Groves-
posts On Mon, Oct , at : 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(), ), 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

boost range zhuan的更多相关文章

  1. boost range

    1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...

  2. Boost.Foreach

    BOOST_FOREACH简化了C++的循环遍历序列元素. 支持的序列类型:Boost.Range识别的序列 STL容器 数组 Null-terminated String std::pair of ...

  3. 一起学习Boost标准库--Boost.StringAlgorithms库

    概述 在未使用Boost库时,使用STL的std::string处理一些字符串时,总是不顺手,特别是当用了C#/Python等语言后trim/split总要封装一个方法来处理.如果没有形成自己的com ...

  4. Boost StateChart实现状态机----秒表例程

    Boost 提供了状态机的实现接口,采用了CRTP技术实现,下面以秒表为例子实现一个状态机,这是一个官方的例子,也可以参考资料:Boost Statechart 庫,状态机的状态转换图如下所示: 实现 ...

  5. boost replace_if replace_all_regex_copy用法

    #include <boost/algorithm/string.hpp> // for is_any_of #include <boost/range/algorithm/repl ...

  6. Boost总结汇总

    从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...

  7. boost algorithm

    BOost Algorithm provides algorithms that complement the algorithms from the standard library. Unlike ...

  8. Boost随机库的简单使用:Boost.Random(STL通用)

    文章目录 文章目录 文章内容介绍 Boost随机库的简单使用 生成一个随机的整数 生成一个区间的平均概率随机数 按概率生成一个区间的随机整数 一些经典的分布 与STL的对比 Ref 文章内容介绍 Bo ...

  9. ES聚合查询实例

    查询特定渠道分享数量最大的30个文章的uuid: { , "query": { "bool": { "must": [ { "te ...

随机推荐

  1. 2019.04.12 Head First

    第一节 认识python python.exe -V python 会进入解释器 quit()命令会退出解释器 IDEL,一个python的集成开发环境,能够利用颜色突出语法的编辑器,一个调试工具,P ...

  2. Python基础(十一) 类继承

    类继承: 继承的想法在于,充份利用已有类的功能,在其基础上来扩展来定义新的类. Parent Class(父类) 与 Child Class(子类): 被继承的类称为父类,继承的类称为子类,一个父类, ...

  3. 54.超大数据快速导入MySQL

    超大数据快速导入MySQL  ----千万级数据只需几十分钟本地测试方法1.首先需要修改本地mysql的编码和路径,找到my.ini.2.在里面添加或修改 character-set-server=u ...

  4. 分治法——快速排序(quicksort)

    先上代码 #include <iostream> using namespace std; int partition(int a[],int low, int high) { int p ...

  5. Sitecore开发 IP地理定位服务入门

    如果您是营销人员或开发人员,并且有兴趣在Sitecore安装中使用Sitecore IP Geolocation服务,那么本文就是为您准备的. 借助Sitecore IP地理定位服务,您网站的访问者可 ...

  6. Apache Zeppelin 初识

    今天得知了一个Apache的孵化项目zeppelin,特了解一下. Zeppelin是一个Apache的孵化项目.一个基于web的笔记本,支持交互式数据分析.你可以用SQL.Scala等做出数据驱动的 ...

  7. mybatis源码解析12---ResultSetHandler解析

    说完了StatementHandler和ParameterHandler,接下来就需要对查询的结果进行处理了,而对于sql结果的处理是由ResultSetHandler处理的,ResultHandle ...

  8. EasyUI中使用textbox赋值,setValue和setText顺序问题

    注意两点: 当text和value的值不同时,一定要先赋值Value,然后赋值Text,否则text和value全部为Value的值. 如果只setValue,则使用getText和getValue得 ...

  9. [转载] HTTP 协议中 URI 和 URL 的区别

    出处:https://blog.csdn.net/qq_26975307/article/details/54429760 HTTP = Hyper Text Transfer ProtocolURI ...

  10. window安装ab压力测试并使用

    ab是Apache HTTP server benchmarking tool的缩写,可以用以测试HTTP请求的服务器性能,也是业界比较流行和简单易用的一种压力测试工具包 1.下载ab工具 进入apa ...