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. Oracle命令行模式,批量执行SQL脚本

    由于项目不同,使用的数据库也不一样,通常MySQL 比较方便简介,相对而言Oracle比较繁琐一点,尤其是堡垒机的连接的时候, 通过堡垒机登陆,数据库服务器,通过下面的脚本执行进入到命令行模式执行SQ ...

  2. leetcode 118. 杨辉三角(python)

    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...

  3. CycleGAN --- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks

    文章地址:http://openaccess.thecvf.com/content_ICCV_2017/papers/Zhu_Unpaired_Image-To-Image_Translation_I ...

  4. 让dcef3支持mp3和h.264 mp4解码播放

    嵌入式Chromium框架(简称CEF) 是一个由Marshall Greenblatt在2008建立的开源项目,它主要目的是开发一个基于Google Chromium的Webbrowser控件.CE ...

  5. websocket初体验

    (function (window) { var wsUri = "ws://echo.websocket.org:9150"; var output; MyWebSocket = ...

  6. Ural Amount of Degrees(数位dp)

    传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...

  7. Linux安装redis服务器和部署

    Linux安装redis和部署 第一步:下载安装包 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 访问https://redis. ...

  8. Maven安装、配置环境变量

    一.首先在官网下载安装maven 1.进入官网 2.找到下载位置 3.点进去后是最新版的,若需要最新版就下这个,需要旧版本接着往下滑 4.下载历史版本 (1)点击"archives" ...

  9. Vuejs中关于computed、methods、watch,mounted的区别

    1.computed是在HTML DOM加载后马上执行的,如赋值: 2.methods则必须要有一定的触发条件才能执行,如点击事件: 3.watch呢?它用于观察Vue实例上的数据变动.对应一个对象, ...

  10. 关于自带的sql developer修改java.exe版本的解决办法

    第一次安装oracle11gR2后,就很好奇的点了一下,当点击应用程序开发下的sql developer后,就弹出一个窗口,要选择一个java.exe的路径,我就讲本机中的JDK1.7下的java.e ...