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 ...
随机推荐
- Shiro学习资料
这篇博客的作者是张开涛,他写了很多专题文章,值得关注一下. 博客专栏 - 跟我学Shirohttp://www.iteye.com/blogs/subjects/shiro
- 【Java架构:进阶技术】——一篇文章搞掂:JVM调优
Sun官方定义的Java技术体系: Java程序设计语言 各种硬件平台上的Java虚拟机 Class文件格式 Java API类库 来自商业机构和开源社区的第三方Java类库 JDK(Java Dev ...
- (转)k8s集群部署二:flannel网络
转:https://blog.csdn.net/sinat_35930259/article/details/79946146 Overlay Network模式 覆盖网络,在基础网络上叠加的一种虚拟 ...
- 通过VLC的ActiveX进行二次开发,实现一个多媒体播放器 2011-04-10 00:57:23
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25498312&id=218294 通过VLC的ActiveX进行二 ...
- Linux 初始化系统(init)- systemd
wikipedia 浅析 Linux 初始化 init 系统 systemd 中文手册 fedoraproject - systemd 1. Systemd 简介 Systemd 是 Linux 系统 ...
- springMvc中自定义bean转换接收前台传的参数
转载:https://blog.csdn.net/u013476435/article/details/81538099 因前端整体传参时,参数名都不是驼峰写法,类似 music_name,music ...
- C#将字符串Split()成数组
string str="aaajbbbjccc";string[] sArray=str.Split('j');foreach(string i in sArray) Respon ...
- Pikachu漏洞练习平台实验——文件包含(File Inclusion)(六)
1.概述 1.1简介 在 Web 后台开发中,程序员往往为了提高效率以及让代码看起来更加简洁,会使用 “包含” 函数功能.比如把一系列功能函数都写进 function.php 中,之后当某个文件需要调 ...
- vue手写轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL-第六篇DML语句
1.DML主要操作数据表里的数据,主要完成3个任务: 1>insert:插入数据.格式:insert into ... 2>delete:删除数据.格式:delete from ... 3 ...