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] -- : 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的更多相关文章
- boost range
1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...
- Boost.Foreach
BOOST_FOREACH简化了C++的循环遍历序列元素. 支持的序列类型:Boost.Range识别的序列 STL容器 数组 Null-terminated String std::pair of ...
- 一起学习Boost标准库--Boost.StringAlgorithms库
概述 在未使用Boost库时,使用STL的std::string处理一些字符串时,总是不顺手,特别是当用了C#/Python等语言后trim/split总要封装一个方法来处理.如果没有形成自己的com ...
- Boost StateChart实现状态机----秒表例程
Boost 提供了状态机的实现接口,采用了CRTP技术实现,下面以秒表为例子实现一个状态机,这是一个官方的例子,也可以参考资料:Boost Statechart 庫,状态机的状态转换图如下所示: 实现 ...
- boost replace_if replace_all_regex_copy用法
#include <boost/algorithm/string.hpp> // for is_any_of #include <boost/range/algorithm/repl ...
- Boost总结汇总
从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...
- boost algorithm
BOost Algorithm provides algorithms that complement the algorithms from the standard library. Unlike ...
- Boost随机库的简单使用:Boost.Random(STL通用)
文章目录 文章目录 文章内容介绍 Boost随机库的简单使用 生成一个随机的整数 生成一个区间的平均概率随机数 按概率生成一个区间的随机整数 一些经典的分布 与STL的对比 Ref 文章内容介绍 Bo ...
- ES聚合查询实例
查询特定渠道分享数量最大的30个文章的uuid: { , "query": { "bool": { "must": [ { "te ...
随机推荐
- django-ajax post与get请求
客户端 访问 服务器 方式: 地址栏 get a标签 get form表单 get/post ajax ...
- Java第二次考试
代码 package sizeyunsuan; import java.io.FileNotFoundException; import java.io.PrintStream; import jav ...
- debug调试命令
- m2e-wtp的作用
描述 Maven3下的项目结构,target目录下会有一个m2e-wtp文件夹,删除掉会自动生成,有什么作用呢? wtp解释 WTP:Web Tools Project Maven集成WTP The ...
- c#高级编程第七版 学习笔记 第二章 核心c#
第二章 核心C# 本章内容: 声明变量 变量的初始化和作用域 C#的预定义数据类型 在c#程序中使用条件语句.循环和跳转语句执行流 枚举 名称空间 Main()方法 基本的命令行c#编译器选项 使用S ...
- 搭建一个Web Server站点
题:搭建一个Web Server站点.安装web服务,并在本地创建index.html测试 1.安装http服务 yum -y install httpd 2.进入网站目录 cd /var/www/h ...
- Docker Swarm 创建服务
Docker Swarm 创建服务 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理节点:192.168.1.79 工作节点:192.168.1.78 工作节点 ...
- Codeforces 17E Palisection - Manacher
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个串$s$询问,有多少对回文子串有交. 好像很简单的样子. 考虑能不能直接求,感觉有点麻烦.因为要考虑右端点在当前回文子串内还有区间包含 ...
- 单元测试系列之二:Mock工具Jmockit实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...
- 【Git】git error记录之 "unpacking the sent packfile failed on the remote"
错误信息: error: cannot open .git/FETCH_HEAD: Permission denied unpacking the sent packfile failed on th ...