【C++】C++11 STL算法(一):非修改序列操作(Non-modifying sequence operations)
目录
- 一、all_of、any_of、none_of:
- 1、官方说明
- 2、谓词
- 3、STL算法对谓词的说明
- 4、谓词的五种模式
- 5、all_of (C++ 11)
- 6、any_of (C++ 11)
- 7、none_of(C++ 11)
- 8、官方demo:
- 二、for_each
- 1、原型:
- 2 说明:
- 3、官方demo:
- 三、count count_if
- 1、原型:
- 2、说明:
- 3、官方demo
- 四、mismatch
- 1、原型:
- 2、说明:
- 3、官方demo
- 五、find、find_if、find_if_not
- 1、原型:
- 2、说明:
- 3、官方demo
- 六、find_end
- 1、原型:
- 2、说明:
- 3、官方demo
- 七、find_first_of
- 1、原型:
- 2、说明:
- 3、官方demo
- 八、adjacent_find
- 1、原型:
- 2、说明:
- 3、官方demo
- 九、search
- 1、原型:
- 2、说明:
- 3、官方demo
- 十、search_n
- 1、原型:
- 2、说明:
- 3、官方demo
头文件:#include <algorithm>
一、all_of、any_of、none_of:
1、官方说明
检查谓词对于范围中的所有元素、任一元素或没有这样的元素 为真(功C能模板)。
2、谓词
汉语中谓词包括动词和形容词,详解参见https://baike.baidu.com/item/%E8%B0%93%E8%AF%8D。
3、STL算法对谓词的说明
参见:https://en.cppreference.com/w/cpp/named_req/Predicate,简单的来说,就是一个返回布尔值的函数。
这三类算法函数中,都有一个模板参数:UnaryPredicate p(一元谓词):
使用 p 测试迭代器指向的对象,逻辑结构如下:if(pred(*first)) {...}
4、谓词的五种模式
函数、函数指针、lambda表达式、函数对象、库定义的函数对象.参见博客:https://blog.csdn.net/caroline_wendy/article/details/15378055
5、all_of (C++ 11)
原型:
template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );
说明:
在[first, last)范围内的元素全都满足条件p,则返回真true;
6、any_of (C++ 11)
原型:
template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );
说明:
在[first, last)范围内至少有一个元素满足条件p,则返回真true;
7、none_of(C++ 11)
原型:
template< class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );
说明:
在[first, last)范围内没有一个元素满足条件p,则返回真true;
8、官方demo:
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>int main()
{std::vector<int> v(10, 2); // {2,2,2,2,2,2,2,2,2,2}std::partial_sum(v.cbegin(), v.cend(), v.begin()); // {2,4,6,8,10,12,14,16,18,20}std::cout << "Among the numbers: ";std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));std::cout << '\n';if (std::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) { // 谓词p是lambda表达式std::cout << "All numbers are even\n";}if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(), std::placeholders::_1, 2))) { // 谓词p是库函数对象std::cout << "None of them are odd\n";}struct DivisibleBy{const int d;DivisibleBy(int n) : d(n) {}bool operator()(int n) const { return n % d == 0; }};if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) { // 谓词p是函数对象std::cout << "At least one number is divisible by 7\n";}
}
输出:
Among the numbers: 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 7
二、for_each
1、原型:
template< class InputIt, class UnaryFunction >
UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );
2 说明:
将函数f应用到[first, last)范围内的所有元素。
如果f返回结果,则忽略该结果。与其余算法不同,for_each不允许复制序列中的元素,即使它们是可复制的。
3、官方demo:
#include <vector>
#include <algorithm>
#include <iostream>struct Sum
{Sum(): sum{0} { }void operator()(int n) { sum += n; }int sum;
};int main()
{std::vector<int> nums{3, 4, 2, 8, 15, 267};auto print = [](const int& n) { std::cout << " " << n; };std::cout << "before:";std::for_each(nums.begin(), nums.end(), print);std::cout << '\n';std::for_each(nums.begin(), nums.end(), [](int &n){ n++; });// calls Sum::operator() for each numberSum s = std::for_each(nums.begin(), nums.end(), Sum());std::cout << "after: ";std::for_each(nums.begin(), nums.end(), print);std::cout << '\n';std::cout << "sum: " << s.sum << '\n';
}
Output:
before: 3 4 2 8 15 267
after: 4 5 3 9 16 268
sum: 305
三、count count_if
1、原型:
template< class InputIt, class T >
typename iterator_traits<InputIt>::difference_typecount( InputIt first, InputIt last, const T &value );template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_typecount_if( InputIt first, InputIt last, UnaryPredicate p );
2、说明:
返回满足特定条件的元素数量
3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>int main()
{std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };// 返回匹配目标值target的数量int target1 = 3;int target2 = 5;int num_items1 = std::count(v.begin(), v.end(), target1);int num_items2 = std::count(v.begin(), v.end(), target2);std::cout << "number: " << target1 << " count: " << num_items1 << '\n';std::cout << "number: " << target2 << " count: " << num_items2 << '\n';// 可被3整除的数量int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;});std::cout << "number divisible by three: " << num_items3 << '\n';
}
Output:
number: 3 count: 2
number: 5 count: 0
number divisible by three: 3
四、mismatch
1、原型:
template< class InputIt1, class InputIt2 >
std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2 );
2、说明:
找到两个范围不同的第一个位置
3、官方demo
#include <iostream>
#include <string>
#include <algorithm>std::string mirror_ends(const std::string& in)
{return std::string(in.begin(),std::mismatch(in.begin(), in.end(), in.rbegin()).first);
}int main()
{std::cout << mirror_ends("abXYZba") << '\n'<< mirror_ends("abca") << '\n'<< mirror_ends("aba") << '\n';
}
Output:
ab
a
aba
五、find、find_if、find_if_not
1、原型:
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );template< class InputIt, class UnaryPredicate >
InputIt find_if_not( InputIt first, InputIt last, UnaryPredicate q );
2、说明:
找到满足特定条件的第一个元素
UnaryPredicate p:参见all_of、any_of、none_of关于谓词的解释
3、官方demo
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>int main()
{int n1 = 3;int n2 = 5;std::vector<int> v{0, 1, 2, 3, 4};auto result1 = std::find(std::begin(v), std::end(v), n1);auto result2 = std::find(std::begin(v), std::end(v), n2);if (result1 != std::end(v)) {std::cout << "v contains: " << n1 << '\n';} else {std::cout << "v does not contain: " << n1 << '\n';}if (result2 != std::end(v)) {std::cout << "v contains: " << n2 << '\n';} else {std::cout << "v does not contain: " << n2 << '\n';}
}
Output:
v contains: 3
v does not contain: 5
六、find_end
1、原型:
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
2、说明:
查找某个范围内,最后一个和序列2匹配的位置(返回该位置的迭代器)
3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>int main()
{std::vector<int> v{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};std::vector<int>::iterator result;std::vector<int> t1{1, 2, 3};result = std::find_end(v.begin(), v.end(), t1.begin(), t1.end());if (result == v.end()) {std::cout << "sequence not found\n";} else {std::cout << "last occurrence is at: "<< std::distance(v.begin(), result) << "\n"; // std::distance 计算迭代器之间的距离}std::vector<int> t2{4, 5, 6};result = std::find_end(v.begin(), v.end(), t2.begin(), t2.end());if (result == v.end()) {std::cout << "sequence not found\n";} else {std::cout << "last occurrence is at: " << std::distance(v.begin(), result) << "\n";}
}
Output:
last occurrence is at: 8
sequence not found
七、find_first_of
1、原型:
template< class InputIt, class ForwardIt >
InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last );
2、说明:
搜索序列1中可以匹配序列2中任一元素的位置(迭代器)
3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>int main()
{std::vector<int> v{0, 2, 3, 25, 5};std::vector<int> t{3, 19, 10, 2};auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());if (result == v.end()) {std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";} else {std::cout << "found a match at "<< std::distance(v.begin(), result) << "\n";}}
Output:
found a match at 1
八、adjacent_find
1、原型:
template< class ForwardIt >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );template< class ForwardIt, class BinaryPredicate>
ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );
2、说明:
在[first, last)范围内搜索两个连续相同的元素。
3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>int main()
{std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5};auto i1 = std::adjacent_find(v1.begin(), v1.end());if (i1 == v1.end()) {std::cout << "no matching adjacent elements\n";} else {std::cout << "the first adjacent pair of equal elements at: "<< std::distance(v1.begin(), i1) << '\n';}auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());if (i2 == v1.end()) {std::cout << "The entire vector is sorted in ascending order\n";} else {std::cout << "The last element in the non-decreasing subsequence is at: "<< std::distance(v1.begin(), i2) << '\n';}
}
Output:
The first adjacent pair of equal elements at: 4
The last element in the non-decreasing subsequence is at: 7
九、search
1、原型:
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
2、说明:
在序列1中搜索匹配序列2的位置(迭代器)
3、官方demo
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>template <typename Container>
bool in_quote(const Container& cont, const std::string& s)
{return std::search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
}int main()
{std::string str = "why waste time learning, when ignorance is instantaneous?";// str.find() can be used as wellstd::cout << std::boolalpha << in_quote(str, "learning") << '\n'<< in_quote(str, "lemming") << '\n';std::vector<char> vec(str.begin(), str.end());std::cout << std::boolalpha << in_quote(vec, "learning") << '\n'<< in_quote(vec, "lemming") << '\n';
}
Output:
true
false
true
false
十、search_n
1、原型:
template< class ForwardIt, class Size, class T >
ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value );template< class ForwardIt, class Size, class T, class BinaryPredicate >
ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p );
2、说明:
在给定范围内搜索多个连续元素的位置(迭代器)
3、官方demo
#include <iostream>
#include <algorithm>
#include <iterator>template <class Container, class Size, class T>
bool consecutive_values(const Container& c, Size count, const T& v)
{return std::search_n(std::begin(c),std::end(c),count,v) != std::end(c);
}int main()
{const char sequence[] = "1001010100010101001010101";std::cout << std::boolalpha;std::cout << "Has 4 consecutive zeros: "<< consecutive_values(sequence,4,'0') << '\n';std::cout << "Has 3 consecutive zeros: "<< consecutive_values(sequence,3,'0') << '\n';
}
Output:
Has 4 consecutive zeros: false
Has 3 consecutive zeros: true
相关文章:

Python openpyxl 之 Excel 文档简单操作
背景:生活中常常因日常工作,在记录统计方面需频繁处理较多 Excel 表格,这部分工作虽可由人工完成,但这样会显得有些繁琐且可能存在偏差,遂闲时查阅了是否有相关基于python处理Excel表格的学习文档,后获知这…

售价910元!周志华等人英文新书《演化学习》出炉!
点击上方↑↑↑蓝字关注我们~整理 | 琥珀出品 | AI 科技大本营(公众号ID:rgznai100)关于人工智能教育,从学生培养方案,到课程设置、教材,甚至是授课老师,全国各大高校正探索一条新道路。先是从去…

linux 查看 文档 不显示注释 命令
原文:http://www.weiruoyu.cn/?p661 最近发现一个很好的命令,就是linux 查看 文档 不显示注释 的命令[rootlocalhost ha.d]# cat ha.cf |grep -v ^# logfile /var/log/ha-log 转载于:https://blog.51cto.com/weiruoyu/705840

【C++】C++11 STL算法(二):修改序列的操作(Modifying sequence operations)
目录一、copy、copy_if1、原型:2、说明:3、官方demo二、copy_n1、原型:2、说明:3、官方demo三、copy_backward1、原型:1、说明:1、官方demo四、move1、原型:2、说明:3、官方demo五、…

ECharts测量图,功率图
/*** 测量图,功率图1,仪表盘*/ mainpage.prototype.initEcharsGLT1 function(oneJZ){ //if(myChartGLT1 null && myChartGLT1 ! "" && myChartGLT1 ! undefined) {myChartGLT1.dispose(); //每次加载之前清除之前的echar…
北京智源人工智能研究院启动“智源学者计划”,与旷视发布首个智源联合实验室
4月16日,北京智源人工智能研究院与中国人工智能领军企业旷视召开“智源学者计划暨联合实验室发布会”。北京市科委副主任张光连,海淀区委常委、副区长李俊杰,以及来自科技部、北京市科委、海淀区人民政府、朝阳区人民政府、中关村管委会&…

配置隧道模式的IPSec.×××
一、拓扑及IP配置 二、配置清单 R1#show run Building configuration... Current configuration : 1449 bytes ! upgrade fpd auto version 12.4 service timestamps debug datetime msec service timestamps log datetime msec no service password-encryption ! hostname R1 …

【C++】C++11 STL算法(三):分隔操作(Partitioning operations)、排序操作(Sorting operations)
目录分隔操作(Partitioning operations)一、is_partitioned1、原型:2、说明:3、官网demo二、partition1、原型:2、说明:3、官方demo三、partition_copy1、原型:2、说明:3、官方demo四…

浪潮发布重磅产品“元脑”,专注AI全栈能力输出
整理 | 一一出品 | AI科技大本营(ID:rgznai100)4月16日,以“智慧凝聚”为题的IPF2019浪潮云数据中心合作伙伴大会在上海举办。大会重点聚焦浪潮“智慧计算”战略,以AI计算力和创新力,联接、承载、赋能合作伙伴。为了布…

React+Redux+中间件
MVVM是Model-View-ViewModel的缩写。mvvm是一种设计思想。Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑;View 代表UI 组件,它负责将数据模型转化成UI 展现出来,ViewModel 是一个同步View 和 Model的对象。在…

ピエタ~幸せの青い鳥~相关
先打全所有升级补丁 不然没有end4 补丁下载页 4个end出现方法 只看律视角 选项任意→end1 只看愛视角 选项任意→end2 检查一下 这两个流程的CG是否收全了 开启唯视角以后有些CG是找不回的 只看唯视角 选项任意→end3 只看唯视角 最后一个选项选“唯” 此后只要律或愛的视角开…

【C++】C++11 STL算法(四):二分查找法(Binary search operations)、合并操作
目录一、lower_bound1、原型:2、说明:3、官方demo二、upper_bound1、原型:2、说明:3、官方demo三、binary_search1、原型:2、说明:3、官方demo四、equal_range1、原型:2、说明:3、官…

腾讯开源分布式NoSQL存储系统DCache | 技术头条
作者 | 山宝银,腾讯后台高级工程师,专注于分布式 NoSQL 存储领域的技术研发工作,参与腾讯多个自研存储系统的开发,在分布式系统、高可用与高性能服务等领域有较丰富的经验。来源 | 腾讯技术博客当你在电商平台秒杀商品或者在社交网…
老司机带你学爬虫——Python爬虫技术分享
什么是“爬虫”? 简单来说,写一个从web上获取需要数据并按规定格式存储的程序就叫爬虫; 爬虫理论上步骤很简单,第一步获取html源码,第二步分析html并拿到数据。但实际操作,老麻烦了~ 用Python写“爬虫”有哪…
[转载]分享WCF聊天程序--WCFChat
http://www.cnblogs.com/gaoweipeng/archive/2009/09/04/1560260.html 无意中在一个国外的站点下到了一个利用WCF实现聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了测试和部分修改,感觉还不错,分享给大家。…

【C++】C++11 STL算法(五):设置操作(Set operations)、堆操作(Heap operations)
目录设置操作(Set operations)一、includes1、原型:2、说明:3、官方demo二、set_difference1、原型:2、说明:3、官方demo三、set_intersection1、原型:2、说明:3、官方demo四、set_symmetric_difference1、…

63万张!旷视发布最大物体检测数据集Objects365 | 技术头条
编辑 | 琥珀来源 | AI科技大本营(id:rgznai100)昨日,在旷视科技联合北京智源人工智能研究院举办的发布会上,旷视研究院发布了物体检测数据集 Objects365,包含 63 万张图像数量,365 个类别数量&a…
(一)Android Studio 安装部署 华丽躲坑
叨叨两句先 小宇之前一直做前后端开发,只是略懂JS,未接触过Java和Android 近期工作任务也是兴趣使然,开始琢磨DJI二次开发 DJI是我最服气的无人机厂商,无人机稳定性极强,性价比狂高,还给了极度丰富的二次开…

linux 环境配置 安装jdk
一. 下载jdk5.0 for linux 到sun的主页 http://java.sun.com/j2se/1.5.0/download.jsp 下载jdk安装文件jdk-1_5_0_05-linux-i586.bin 二. 解压安装jdk 在shell终端下进入jdk-1_5_0_05-linux-i586.bin文件所在目录,执行命令 ./jdk-1_5_0_05-linux-i586.bin 这时会出现…

【C++】C++11 STL算法(六):最小/最大操作(Minimum/maximum operations)、比较运算(Comparison operations)
目录最小/最大操作(Minimum/maximum operations)一、max1、原型:2、说明:3、官方demo二、max_element1、原型:2、说明:3、官方demo三、min1、原型:2、说明:3、官方demo四、min_element1、原型:2…

springboot之定时任务
定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务。 回顾一下定时线程池。 public static ScheduledExecutorService newScheduledThreadPool(int var0) {return new ScheduledThreadPoolExecutor(var0);}public static ScheduledExec…

10只机器狗拉卡车!井然有序,毫不费力 | 极客头条
整理 | 琥珀出品 | AI科技大本营(ID:rgznai100)看来,这家娱乐网友多年的机器人公司终于要开始实现商用化了!最先备受期待的是它的网红机器狗 SpotMini。今日凌晨,据多家外媒报道,波士顿动力 (Boston Dynami…

linux下查看nginx,apache,mysql,php的编译参数
有时候nginx,apache,mysql,php编译完了想看看编译参数可以用以下方法 nginx编译参数: #/usr/local/nginx/sbin/nginx -V nginx version: nginx/0.6.32 built by gcc 4.1.2 20071124 (Red Hat 4.1.2-42) configure arguments: --us…

【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)
排列操作(Permutation operations) 一、is_permutation 1、原型: template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );template< class ForwardIt…

码书:入门中文NLP必备干货:5分钟看懂“结巴”分词(Jieba)
导读:近年来,随着NLP技术的日益成熟,开源实现的分词工具越来越多,如Ansj、盘古分词等。在本文中,我们选取了Jieba进行介绍和案例展示,主要基于以下考虑:社区活跃。截止本文发布前,Ji…

《你必须掌握的Entity Framework 6.x与Core 2.0》正式出版感想
前言 借书正式出版之际,完整回顾下从写博客到写书整个历程,也算是对自己近三年在技术上的一个总结,整个历程可通过三个万万没想到来概括,请耐心阅读。 写博、写书完整历程回顾 从2013年12月注册博客园账号,注册博客园账…

JSF实现“Hello World!”
我们编写一个在页面上显示是“Hello World! ”,我们至少需要编写一个Page对象和一个对应模板文件(tml)。 第一步,Page对象编写 在Tapestry5中Page是与一个页面对应的POJO对象,它不需要继承Tapestry框架的任何基类或实现…

《权力的游戏》最终季上线!谁是你最喜爱的演员?这里有一份Python教程 | 附源码...
译者 | 刘畅编辑 | 琥珀出品 | AI科技大本营(id:rgznai100)《权力的游戏》最终季已于近日开播,对于全世界翘首以待的粉丝们来说,其最大的魅力就在于“无法预知的人物命运”。那些在魔幻时代的洪流中不断沉浮的人们&…

【C++】C++11 STL算法(八):对未初始化内存的操作(Operations on uninitialized memory)、C库(C library)
对未初始化内存的操作(Operations on uninitialized memory) 一、uninitialized_copy 1、原型: template< class InputIt, class ForwardIt > ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );2、…

OSPF高级设置实现全网互通
OSPF(开放式最短路径优先)是对链路状态路由协议的一种实现,隶属内部网关协议(IGP),故运作于自治系统内部(AS)。采用戴克斯特拉算法(Dijkstras algorithm)被用来计算最短路径树。“Cost”作为路由度量值。链…