当前位置: 首页 > news >正文

C++系列-STL中find相关的算法

STL中find相关的算法

  • 💢find相关的算法
    • 💢💢find,find_if举例
    • 💢💢find_first_of举例
    • 💢💢find_end举例
    • 💢💢adjacent_find举例


秋词二首
刘禹锡刘禹锡〔唐代〕

自古逢秋悲寂寥,我言秋日胜春朝。
晴空一鹤排云上,便引诗情到碧霄


💢find相关的算法

Column 1Column 2
find(beg, end, val)利用==运算符,对[begin, end)的元素与val进行比较,当匹配时结束搜索,返回该元素的迭代器
find_if(beg, end, pred)使用谓词代替find中的==操作符,执行find
find_first_of(beg1, end1, beg2, end2)在[beg1, end1)范围内查找[beg2, end2)中任意一个元素的第一次出现,返回该元素[beg1, end1)中的迭代器
find_first_of(beg1, end1, beg2, end2, pred)使用谓词代替==操作符,执行find_first_of
find_end(beg1, end1, beg2, end2)在[beg1, end1)范围内查找[beg2, end2)这一串元素的最后一次出现,返回序列匹配到的在第一个序列中的迭代器
find_end(beg1, end1, beg2, end2, pred)使用谓词代替==执行执行find_end, 返回值同find_end
adjacent_find(beg, end)在[beg, end)范围内查找相邻相同的元素,找到则返回第一个元素的迭代器,否则返回end迭代器
adjacent_find(beg, end, pred)使用谓词代替==,找到则返回第一个元素的迭代器,否则返回end迭代器

💢💢find,find_if举例

👉 👉 👉find和find_if只从头找第一个,找到第一个满足条件的就返回。
find(vec1.begin(), vec1.end(), 3),从vec1中找==3的元素,返回其对应的迭代器。
find_if(vec3.begin(), vec3.end(), [](int val) -> bool {return val > 10;}),从vec3中满足后面的谓词的元素,即元素值大于10,返回器对应的迭代器。

code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;template<class T>
class MyPrint
{
public:void operator()(T& val){cout << val << " ";}
};void test01()
{// find举例vector<int> vec1{1, 2, 3, 4, 5, 3, 3};// find(beg, end, val) find等于3vector<int>::iterator it = find(vec1.begin(), vec1.end(), 3);if (it != vec1.end()){cout << "*it:" << *it << endl;cout << "[it:vec1.end())中的元素: " << endl;for_each(it, vec1.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void test02()
{// find_if举例vector<int> vec3{1, 3, 10, 11, 12, 3};// find_if(beg, end, pred),一元谓词判断是否大于10vector<int>::iterator it = find_if(vec3.begin(), vec3.end(), [](int val) -> bool {return val > 10;});if (it != vec3.end()){cout << "*it:" << *it << endl;cout << "[it:vec3.end())中的元素: " << endl;for_each(it, vec3.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void main()
{test01();test02();system("pause");
}result:
*it:3
[it:vec1.end())中的元素:
3 4 5 3 3*it:11
[it:vec3.end())中的元素:
11 12 3

💢💢find_first_of举例

👉 👉 👉find_first_of只从头找第一个,可能第二个序列中有多个元素在第一个序列中都能找到,但是以它们在第一个序列中最靠前的一个为最终找到的结果。
示例中第二个序列vec12{8, 5, 7, 10, 12, 3},10能在第一个序列vec11{1, 3, 10, 11, 12, 3}中找到,3也可以,但是3更靠前,所以返回第一个序列中3对应的迭代器。

code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;template<class T>
class MyPrint
{
public:void operator()(T& val){cout << val << " ";}
};void test03()
{// find_first_of举例vector<int> vec11{1, 3, 10, 11, 12, 3};vector<int> vec12{8, 5, 7, 10, 12, 3};vector<int>::iterator it = find_first_of(vec11.begin(), vec11.end(), vec12.begin(), vec12.end());if (it != vec11.end()){cout << "*it:" << *it << endl;cout << "[it:vec12.end())中的元素: " << endl;for_each(it, vec11.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void test04()
{// find_first_of举例, pred的目标,第二组里的任意一个元素大于第一组的任意一个元素(都从头开始找),如果满足就算找到了,返回第一组元素对应的迭代器vector<int> vec11{18, 3, 10, 11, 12, 3};vector<int> vec12{0, 1};vector<int>::iterator it = find_first_of(vec11.begin(), vec11.end(), vec12.begin(), vec12.end(), [](int val1, int val2) ->bool {return val2 > val1;});if (it != vec11.end()){cout << "找到了,*it:" << *it << endl << endl;}else{cout << "找不到" << endl << endl;}
}void main()
{test03();test04();system("pause");
}result:
*it:3
[it:vec12.end())中的元素:
3 10 11 12 3找不到

💢💢find_end举例

code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;template<class T>
class MyPrint
{
public:void operator()(T& val){cout << val << " ";}
};void test01()
{// find_end举例,以第二个容器为基准,从第一个容器中找是否有连续的元素和第二个容器的元素完全相同,// 也就是说看第二个容器是否是第一个的子序列,并找出最后一次匹配的迭代器(此迭代器为第一个序列的迭代器)vector<int> vec11{1, 3, 10, 11, 12, 3, 10, 5};vector<int> vec12{3, 10};vector<int>::iterator it = find_end(vec11.begin(), vec11.end(), vec12.begin(), vec12.end());if (it != vec11.end()){cout << "*it:" << *it << endl;cout << "[it:vec12.end())中的元素: " << endl;for_each(it, vec11.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void test02()
{// find_end举例,以第二个容器为基准,从第一个容器中是否有连续的元素均满足小于第二个容器中的元素,// 找到最后一组,并把对应的第一个容器对应的迭代器返回。vector<int> vec11{1, 3, 10, 11, 12, 3, 10, 5, 15};vector<int> vec12{3, 10};vector<int>::iterator it = find_end(vec11.begin(), vec11.end(), vec12.begin(), vec12.end(), [](int val1, int val2) -> bool {return val1>=val2;});if (it != vec11.end()){cout << "*it:" << *it << endl;cout << "[it:vec12.end())中的元素: " << endl;for_each(it, vec11.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void main()
{test01();test02();system("pause");
}result:
*it:3
[it:vec12.end())中的元素:
3 10 5*it:5
[it:vec12.end())中的元素:
5 15

💢💢adjacent_find举例

code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;template<class T>
class MyPrint
{
public:void operator()(T& val){cout << val << " ";}
};void test01()
{// adjacent_find举例,查找是否有相邻的元素相等,返回相等的第一个元素的迭代器vector<int> vec1{1, 3, 10, 10, 12, 10, 10, 5};vector<int>::iterator it = adjacent_find(vec1.begin(), vec1.end());if (it != vec1.end()){cout << "*it:" << *it << endl;cout << "[it到vec1.end())中的元素: " << endl;for_each(it, vec1.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void test02()
{// adjacent_find举例,查找是否有相邻的元素中的第一个整除第二个,返回满足条件的第一个元素的迭代器vector<int> vec1{1, 3, 10, 4, 12, 6, 10, 5};vector<int>::iterator it = adjacent_find(vec1.begin(), vec1.end(), [](int val1, int val2) -> bool {return val1 % val2 == 0;});if (it != vec1.end()){cout << "*it:" << *it << endl;cout << "[it到vec1.end())中的元素: " << endl;for_each(it, vec1.end(), MyPrint<int>());cout << endl << endl;}else{cout << "找不到" << endl << endl;}
}void main()
{test01();test02();system("pause");
}result:
*it:10
[it到vec1.end())中的元素:
10 10 12 10 10 5*it:12
[it到vec1.end())中的元素:
12 6 10 5

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Java】多态性【主线学习笔记】
  • qt QGraphicsScene场景坐标和场景内GraphicsItem局部坐标的相互转换
  • 2024.9 学习笔记
  • axure判断
  • 3分钟带你快速了解 Java 接口
  • 模板:软件验收文档
  • 【STM32系统】基于STM32设计的智能垃圾桶(语音、颜色识别、称重、光强、烟雾、人体识别、步进电机、水泵)——文末资料下载
  • 157-安全开发-Python 自动化挖掘项目SRC 目标FOFA 资产Web 爬虫解析库
  • oracle 条件取反
  • discuz论坛3.4 截图粘贴图片发帖后显示不正常问题
  • Qt_控件的QWidget属性介绍
  • C/C++调试工具GDB简单介绍
  • 蓝桥杯真题——数星星
  • gitlab无法push(pre-receive hook declined)
  • vue3 响应式 API:readonly() 与 shallowReadonly()
  • php的引用
  • 2019年如何成为全栈工程师?
  • Android Studio:GIT提交项目到远程仓库
  • ComponentOne 2017 V2版本正式发布
  • fetch 从初识到应用
  • flutter的key在widget list的作用以及必要性
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Laravel核心解读--Facades
  • Linux Process Manage
  • Median of Two Sorted Arrays
  • MobX
  • Python语法速览与机器学习开发环境搭建
  • Redis中的lru算法实现
  • windows下使用nginx调试简介
  • 大主子表关联的性能优化方法
  • 前端技术周刊 2019-01-14:客户端存储
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 事件委托的小应用
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 小李飞刀:SQL题目刷起来!
  • 用 Swift 编写面向协议的视图
  • 智能网联汽车信息安全
  • 自定义函数
  • gunicorn工作原理
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​queue --- 一个同步的队列类​
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • #includecmath
  • #include到底该写在哪
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (06)金属布线——为半导体注入生命的连接
  • (八)Flask之app.route装饰器函数的参数
  • (补充)IDEA项目结构
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (面试必看!)锁策略
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (七)Activiti-modeler中文支持
  • (一) storm的集群安装与配置