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. SecondContract 接口类

    package com.test.mvp.mvpdemo.mvp.v6; import com.test.mvp.mvpdemo.mvp.v6.basemvp.IBasePresenter;impor ...

  2. 第九届ECNU Coder K.计软联谊

    题目链接:http://acm.ecnu.edu.cn/contest/16/problem/K/ 题目: K. 计软联谊 Time limit per test: 7.0 seconds Time ...

  3. Windows命令学习

    总: 1.window dos命令不区分大小写  2.指令参数 /a   -a 等价 更倾向于 / 3.命令有疑问: CMD输入help 或者单条命令 /? help time /? 用到的实用命令总 ...

  4. vue对组件以数组方式赋值的问题

    当从后台直接调接口返回数据 直接将数组array赋值给定义的变量,会导致组件无法更改其它值,例如多选框,多选下拉框,会导致无法选中其它的值,也无法取消当前已赋值的选中项 data() { return ...

  5. 【转载】Spring boot学习记录(三)-启动原理解析

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 我们开发任何一个Spring Boot项目,都会用到如下的启动类 @Sprin ...

  6. Hibernate的dtd文件和properties文件

    hibernate-configuration-3.0.dtd <!-- Hibernate file-based configuration document. <!DOCTYPE hi ...

  7. java.lang.IllegalStateException: Cannot forward after response has been committed

    jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.cat ...

  8. [Linux] 017 网络命令与挂载命令

    1. 网络命令:write 命令名称:write 命令所在路径:/usr/bin/write 执行权限:所有用户 语法:write [用户名] 功能描述:给用户发信息,以 Ctrl-d 保存结束 范例 ...

  9. [LuoguP1829]Crash的文明表格(二次扫描与换根+第二类斯特林数)

    Solution: ​ 由于 \[ x^m = \sum_{i=0}^m{~m~\choose i}{~x~\brace i}i! \] ​ 将所求的式子化成这样,挖掘其性质,考虑是否能从儿子转移(或 ...

  10. LTP安装方法

    git clone https://github.com/linux-test-project/ltp.git apt-get install automake make autotools ./co ...