1.Algorithms

Boost.Range is library that, on the first sight, provides algorithms similar to those provided by the standard library. For example, you will find the function boost::copy(), which does the same thing as std::copy(). However, std::copy() expects two parameters while boost::copy() expects a range.

You can think of a range as two iterators that refer to the beginning and end of a group of elements that you can lterate over. Because all containers support iterators, every container can be thought of as a range. Since all algorithms from Boost.Range expect a range as a first parameter, a container like std::vector can be passed directly. You don't have to call begin() and end() and then pass two iterators separately. This protects you from mistakes such as passing the begin and end iterator in the wrong order or passining iterators that belong to two different containers.

#include <boost/range/algorithm.hpp>
#include <array>
#include <iostream> int main() {
std::array<int, > a{{, , , , , }};
std::cout << boost::count(a, ) << std::endl;
return ;
}

输出 3

All algorithms from Boost.Range requirre the first parameter to be a range. An object of type std::array can be passed to boost::count() directly since containers are ranges. Because boost::count() is equivalent to std::count(), you must pass in the value that the elements in the range will be compared with.

#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <array>
#include <iterator>
#include <iostream> int main() {
std::array<int, > a{{, , , , , }};
boost::random_shuffle(a);
boost::copy(a, std::ostream_iterator<int>{std::cout, ","});
std::cout << std::endl << *boost::max_element(a) << std::endl;
std::cout << boost::accumulate(a, ) << std::endl;
return ;
}

boost::random_shffle() works like std::random_shuffle(), changing the order of elements in a range randomly.  boost::copy works like std::copy. boost::max_element() and boost::accumulate() work like the identically named algorithms from the standard library. Like std::max_element(), boost::max_element() returns an iterator to the element with the greatest number.

#include <boost/range/algorithm_ext.hpp>
#include <array>
#include <deque>
#include <iterator>
#include <iostream> int main() {
std::array<int, > a{{, , , , , }};
std::cout << std::boolalpha << boost::is_sorted(a) << std::endl;
std::deque<int> d;
boost;:push_back(d, a);
boost::remove_erase(d, );
boost::copy_n(d, , std::ostream_iterator<int>{std::cout, ","});
return ;
}

boost::is_sorted() tests whether elements in a range are sorted. A predicate can be passed as the second parameter to boost::is_sorted() to check, for example, whether a range is sorted in descending order.

boost::push_back() expects as its first parameter a container and its second parameter a range.

boost::remove_erase() removes the number 2 from d. This algorithm combines a call to the function std::remove() and a call to the member function erase() of the respective container.

2. Adaptors

You can think of adaptors as filters. They return a new range based on another range. Data isn't necessarily copied. Since a range is just a pair of iterators, an adaptor returns a new pair. The pair can still be used to iterate over the original range but may, for example, skip certain required.

The difference between algorithms and adaptors is that  algorithms iterate over a range and process data, while adaptors return a new range - new iterators - that determines what elements the iteration returns.

#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <array>
#include <iterator>
#include <iostream> int main() {
std::array<int, > a{{, , , , , }};
boost::copy(boost::adaptors::filter(a, [](int i) { return i > ;}), std::ostream_iterator<int>{std::cout, ","});
return ;
}

boost::adaptors::filter() expects as its first parameter a range to filter and as its second parameter a predicate. boost::adaptors::filter() does not change the range a. it returns a new range. Since a range isn't much different from a pair of iterators, the new range also refers to a. However, the iterators for the new range skip all numbers that are less than or equal 2.

#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <array>
#include <map>
#include <string>
#include <utility>
#include <iterator>
#include <iostream> int main() {
std::array<int, > a{{, , }};
std::map<std::string, int*> m;
m.insert(std::make_pair("a", &a[]));
m.insert(std::make_pair("b", &a[]));
m.insert(std::make_pair("c", &a[])); boost::copy(boost::adaptors::keys(m), std::ostream_iterator<std::string>{std::cout, ","});
boost::copy(boost::adaptors::indirect(boost::adaptors::values(m)), std::ostream_iterator<int>{std::cout, ","});
return ;
}

uses adaptors, boost::adaptors::keys() and boost::adaptors::values() to access keys and values in a container of type std::map. It also shows how adaptors can be nested. Because m stores pointers to the values to be printed, rather than the values themselves, the range returned by boost::adaptors::values() is passed to boost::adaptors::indirect(). This adaptor can always be used when a range consists of pointers, but an iteratorion should return the values the pointers refer to.

boost range的更多相关文章

  1. boost range zhuan

    Officialhttp://67.223.234.84/boost_doc/libs/range/doc/utility_class.html#sub_range http://blog.sina. ...

  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. Why is HttpContext.Current null during the Session_End event?

    Why is HttpContext.Current null during the Session_End event? On Session_End there is no communicati ...

  2. Jenkins 添加新用户

    下面重点记录下jenkins安装后的一些配置: (1)添加用户权限 jenkins初次登陆后,要先注册一个用户作为管理员: 依次点击“系统管理”->“Configure Global Secur ...

  3. KEIL建立新唐MCU的工程时,移植官网程序报错变量未定义问题解决方法

    最近在使用新唐的MCU,新唐的MCU使用还算方便,你安装好KEIL之后再安装 Nu-Link_Keil_Driver_V3.00.6909 驱动即可建立新唐的MCU工程,注意的是因为新唐MCU是C51 ...

  4. Spark SQL实战

    一.程序 package sparklearning import org.apache.log4j.Logger import org.apache.spark.SparkConf import o ...

  5. Nginx证书配置:tomcat证书jks文件转nginx证书.cet和key文件

    Nginx证书配置:tomcat证书jks文件转nginx证书.cet和key文件1.查看jks文件中的entry. keytool -list -keystore server.jks Enter ...

  6. Python的一些高级特性

    内容基本上来自于廖雪峰老师的blog相当于自己手打了一遍,加强加强理解吧. http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493 ...

  7. CSS实现背景图片屏幕自适应

    在做登陆页面等的首页的时候,经常会遇到需要放一张背景大图的情况,并且需要图片按比例缩放,来适应不同屏幕的大小. html代码如下: <!DOCTYPE html> <html lan ...

  8. TCP协议-流量控制

    流量控制是通过滑动窗口来实现控制的.使用了坚持定时器,防止发送失败导致死锁.

  9. MySQL-第四篇索引

    1.创建索引的作用 创建索引的唯一作用就是加速对表的查询.索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O. 2.索引和表一样也是数据库中的一种对象,但它必须从属于某张表,不能独立存 ...

  10. 攻防世界--open-source

    1.打开源码 打开源码 #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { ) ...