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 ...
随机推荐
- Cartographer源码阅读(7):轨迹推算和位姿推算的原理
其实也就是包括两个方面的内容:类似于运动模型的位姿估计和扫描匹配,因为需要计算速度,所以时间就有必要了! 1. PoseExtrapolator解决了IMU数据.里程计和位姿信息进行融合的问题. 该类 ...
- [svc]共享内存
ipc是什么? 进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法. 进程间为何不能直接共享数据? 如何解决ipc问题? ...
- 监听器&上传下载&I18N
监听器(Listener) 监听Java对象 的方法调用和属性改变() web的一个组件 事件驱动编程:事件源,事件名称,事件响应函数,事件对象 以后在Spring中的配置 WEB中有哪些监听器? ...
- appium环境搭建-运行
appium是测试移动端的测试工具 首先要下载手机模拟器,或者连接真机.我用的夜神模拟器.安装打开它.安装这个有很高的兼容性要求,我也是小白,摸索了三天才弄出来 一.原理如图: 二.需要安装的软件: ...
- 细说flask数据库迁移
什么情况下要用数据库迁移? 在开发过程中,需要修改数据库模型,而且还要在修改之后更新数据库.最直接的方式就是删除旧表,但这样会丢失数据. 更好的解决办法是使用数据库迁移框架,它可以追踪数据库模式的变化 ...
- 用命令行打开sublime
在linux下装了linux后默认并不能通过运行命令的方式打开,这就让我们不能像vim一样可以通过 vim <fileName> 来打开文件. 不过我们可以通过把sublime的执行文件放 ...
- Zookeeper 3.5启动时 8080端口被占用
今天闲来无事,学习Zookeeper,下载了Zookeeper的最新版本3.5.启动以后显示: ZooKeeper JMX enabled by default Using config: /opt/ ...
- P1744 采购特价商品 最短路径
P1744 采购特价商品 图论-----最短路径算法 弗洛伊德算法 O(n^3) 代码: #include<iostream> #include<cstdio> #inclu ...
- Linux 安装vsftpd和ftp客户端
1.下载安装包:ftp-0.17-54.el6.x86_64.zip和vsftpd-2.2.2-11.el6_4.1.x86_64.zip 可以直接在Linux底下用yum install vsftp ...
- 使用hector-slam和Kinect V1建图
一.建图实际操作 下载源码测试源码,depthimage_to_laserscan,参考https://blog.csdn.net/u010925447/article/details/5649468 ...