boost range
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的更多相关文章
- boost range zhuan
Officialhttp://67.223.234.84/boost_doc/libs/range/doc/utility_class.html#sub_range http://blog.sina. ...
- 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 ...
随机推荐
- eclipse 启动 tomcat 报错:Server mylocalhost was unable to start within 45 seconds
这个专门转载一篇博文也是为了讽刺一下自己二逼的程序员职业,哈哈. eclipse启动tomcat服务器报错:Server mylocalhost was unable to start within ...
- Flask学习 3 url_for的使用
#!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: flask0 ...
- iOS打印各种类型数据
整型占位符说明 : %d : 十进制整数, 正数无符号, 负数有 “-” 符号; %o : 八进制无符号整数, 没有 0 前缀; %x : 十六进制无符号整数, 没有 0x 前缀; %u : 十进制无 ...
- ADO.NET-EF:ADO.NET Entity Framework 百科
ylbtech-ADO.NET-EF:ADO.NET Entity Framework 百科 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 ...
- day23—JavaScript实现DIV盒子拖拽(原生方式)
转行学开发,代码100天——2018-04-08 <!doctype html> <html> <head> <meta charset="utf- ...
- 测开之路七十一:监控平台之js
监控平台的js //datetimepicker的初始化函数(主要是对选择时间的下拉框)function init_datetimepicker() { //初始化格式和规则 $('#start'). ...
- pycharm5.0.4简易使用说明
前言:学习自动化,需要使用pycharm,以下是简易使用说明 1.注册破解 2.行号和背景色 3.打断点 1.注册破解 打开pycharm5.0.4,点击菜单栏的help->register.. ...
- java Json 技术记录
1.Json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括commons-beanutils.jar,commons-c ...
- Drone 的插件 - Docker 插件
Drone 插件市场 Drone 插件文档 原文地址 - Docker 插件的手册 Docker 插件可以用于构建镜像及发布镜像到 Docker registry.下面的 pipeline 配置,就使 ...
- oracle两表中的两列进行模糊匹配的方法
SELECT T2.列名,T1.列名 FROM 主表 T1, 匹配表 T2 WHERE T1.匹配列 LIKE CONCAT('%',concat(T2.匹配列,'%')); 注意: a ...