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.

  1. #include <boost/range/algorithm.hpp>
  2. #include <array>
  3. #include <iostream>
  4.  
  5. int main() {
  6. std::array<int, > a{{, , , , , }};
  7. std::cout << boost::count(a, ) << std::endl;
  8. return ;
  9. }

输出 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.

  1. #include <boost/range/algorithm.hpp>
  2. #include <boost/range/numeric.hpp>
  3. #include <array>
  4. #include <iterator>
  5. #include <iostream>
  6.  
  7. int main() {
  8. std::array<int, > a{{, , , , , }};
  9. boost::random_shuffle(a);
  10. boost::copy(a, std::ostream_iterator<int>{std::cout, ","});
  11. std::cout << std::endl << *boost::max_element(a) << std::endl;
  12. std::cout << boost::accumulate(a, ) << std::endl;
  13. return ;
  14. }

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.

  1. #include <boost/range/algorithm_ext.hpp>
  2. #include <array>
  3. #include <deque>
  4. #include <iterator>
  5. #include <iostream>
  6.  
  7. int main() {
  8. std::array<int, > a{{, , , , , }};
  9. std::cout << std::boolalpha << boost::is_sorted(a) << std::endl;
  10. std::deque<int> d;
  11. boost;:push_back(d, a);
  12. boost::remove_erase(d, );
  13. boost::copy_n(d, , std::ostream_iterator<int>{std::cout, ","});
  14. return ;
  15. }

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.

  1. #include <boost/range/algorithm.hpp>
  2. #include <boost/range/adaptors.hpp>
  3. #include <array>
  4. #include <iterator>
  5. #include <iostream>
  6.  
  7. int main() {
  8. std::array<int, > a{{, , , , , }};
  9. boost::copy(boost::adaptors::filter(a, [](int i) { return i > ;}), std::ostream_iterator<int>{std::cout, ","});
  10. return ;
  11. }

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.

  1. #include <boost/range/algorithm.hpp>
  2. #include <boost/range/adaptors.hpp>
  3. #include <array>
  4. #include <map>
  5. #include <string>
  6. #include <utility>
  7. #include <iterator>
  8. #include <iostream>
  9.  
  10. int main() {
  11. std::array<int, > a{{, , }};
  12. std::map<std::string, int*> m;
  13. m.insert(std::make_pair("a", &a[]));
  14. m.insert(std::make_pair("b", &a[]));
  15. m.insert(std::make_pair("c", &a[]));
  16.  
  17. boost::copy(boost::adaptors::keys(m), std::ostream_iterator<std::string>{std::cout, ","});
  18. boost::copy(boost::adaptors::indirect(boost::adaptors::values(m)), std::ostream_iterator<int>{std::cout, ","});
  19. return ;
  20. }

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. 洛谷P1242 新汉诺塔(dfs,模拟退火)

    洛谷P1242 新汉诺塔 最开始的思路是贪心地将盘子从大到小依次从初始位置移动到目标位置. 方法和基本的汉诺塔问题的方法一样,对于盘子 \(i\) ,将盘子 \(1\to i-1\) 放置到中间柱子上 ...

  2. input只输入数字和小数后两位

    html:<input  name="" type="tel" value="" placeholder="请输入金额&qu ...

  3. 【ABAP系列】SAP ABAP 总结常用术语简称解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 总结常用术语简 ...

  4. c# 调用 webService

    开局几张  照着做就完事 说明下 这个wsdl 文件是根据别人提供的webService 接口 打开后改变后缀来的 这样就引用完成了 接下来就是重点了  怎么调用 localhost.WsSyncDu ...

  5. urllib.parse解析链接

    1. urlparse() 解析链接,注意,返回值比3多一个params的属性 from urllib.parse import urlparse result = urlparse('http:// ...

  6. deb包转换为rpm包格式

    在Debian系列中安装软件包可以使用apt或者dpkg安装deb包,但是在CentOs, Redhat等则只能安装RPM包,如果希望在Redhat或者CentOS下也安装Deb包的话是不可行的, 但 ...

  7. POJ-1287.Network(Kruskal + Prim + Prim堆优化)

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19674   Accepted: 10061 Desc ...

  8. 《剑指offer》面试题7 用两个栈实现队列 Java版

    书中方法:队列是先进先出的,栈是先进后出的,试想把一串数压入A栈,接着一个个出栈并压入B栈,便会完成"头在下"到"头在上"的转变.B栈内还有元素时,直接出栈表示 ...

  9. seaborn教程3——数据集的分布可视化

    原文转载:https://segmentfault.com/a/1190000015006667 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风 ...

  10. JVM(18)之 Class文件

    开发十年,就只剩下这套架构体系了! >>>   关于类加载机制的相关知识在前面的博文中暂时先讲那么多.中间留下了很多问题,从本篇博文开始,我们来一一解决.    从我们最陌生而又最熟 ...