【C++】STL-常用算法-常用查找算法
0.前言
1.find
#include <iostream>
using namespace std;// 常用查找算法 find
#include<vector>
#include<algorithm>//查找 内置数据类型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找 容器中 是否有 5 这个元素vector<int>::iterator it = find(v.begin(), v.end(), 5); // 返回迭代器类型 if (it == v.end()){cout << "没找到" << endl;}else{cout << "找到: " << *it << endl;}
}//查找 自定义数据类型class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}//重载 == 让底层find知道如何对比person数据类型bool operator==(const Person&p) //加const 防止修改数据{if (p.m_Name == this->m_Name && p.m_Age == this->m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test02()
{//创建数据Person p1("a", 10);Person p2("b", 20);Person p3("c", 30);//放入容器中vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);Person p5("b", 20);vector<Person>::iterator it = find(v.begin(), v.end(), p5);if (it == v.end()){cout << "未找到" << endl;}else{cout << "找到: 姓名: " << it->m_Name << " age:" << (*it).m_Age << endl;}
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
2.find_if
#include <iostream>
using namespace std;// 常用查找算法 find_if
#include<vector>//1.查找内置数据类型class Greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(), v.end(), Greater5());if (it == v.end()){cout << "no find" << endl;}else{cout << "find element: " << (*it) << endl;}
}//2、查找自定义数据类型class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};//方式一 利用仿函数
//class greater_Age20
//{
//public:
// bool operator()(const Person& p)
// {
// return p.m_Age > 20;
// }
//};//方式二 利用普通函数
bool greater_Age20(const Person& p)
{return p.m_Age > 20;
}void test02()
{//创建对象Person p1("a", 10);Person p2("b", 20);Person p3("c", 30);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);//查找年龄大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), greater_Age20);if (it == v.end()){cout << "no find" << endl;}else{cout << "find the element: name:" << it->m_Name << " age:" << it->m_Age << endl;}
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
3.adjacent_find
#include <iostream>
using namespace std;// 常用查找算法 adjacent_find
#include<vector>
#include<algorithm>void test01()
{//创建对象vector<int>v;v.push_back(1);v.push_back(2);v.push_back(1);v.push_back(3);v.push_back(4);v.push_back(3);v.push_back(3);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "no find adjacent duplcate elements " << endl;}else{cout << "find adjacent duplcate elements: " << *pos << endl;}
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
4.binary_search
#include <iostream>
using namespace std;// 常用查找算法 binary_search 二分查找
#include<vector>
#include<algorithm>void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//v.push_back(2); 如果是无序序列,结果未知!//查找容器中是否有9元素//注意:容器必须是有序的序列(从小到大)bool ret = binary_search(v.begin(), v.end(),9);if (ret){cout << "find point element" << endl;}else{cout << "no find" << endl;}
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
5.count
#include <iostream>
using namespace std;// 常用查找算法 count
#include<vector>
#include<algorithm>//1.统计内置数据类型
void test01()
{vector<int>v;v.push_back(1);v.push_back(4);v.push_back(1);v.push_back(3);int num = count(v.begin(), v.end(), 1);cout << "the nmber of point element: " << num << endl;
}//2.统计自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person& p){if (p.m_Age == this->m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
void test02()
{//创建对象Person p1("刘备", 35);Person p2("薇恩", 24);Person p3("皮城", 35);Person p4("光辉", 40);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);Person p5("卡米尔", 35);int num = count(v.begin(), v.end(), p5);cout << "跟卡米尔同岁的人有多少个: " << num << endl;
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
6.count_if
#include <iostream>
using namespace std;// 常用查找算法 count_if
#include<vector>
#include<algorithm>//1、统计内置数据类型//利用仿函数
class Greater3
{
public:bool operator()(int val){return val > 3;}
};void test01()
{vector<int>v;v.push_back(1);v.push_back(6);v.push_back(3);v.push_back(2);v.push_back(4);v.push_back(3);int num = count_if(v.begin(), v.end(), Greater3());cout << "the number of element greater than 3 : " << num << endl;
}//2、统计自定义数据类型class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};//利用普通函数
bool age_Greater35(const Person& p)
{if (p.m_Age > 35){return true;}else{return false;}
}void test02()
{//创建对象Person p1("刘备", 35);Person p2("薇恩", 24);Person p3("皮城", 35);Person p4("光辉", 40);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计岁数大于35的人物的个数int num = count_if(v.begin(), v.end(), age_Greater35);cout << "the number of character whose age greater than 35 :" << num << endl;
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
相关文章:

【C++】STL-常用算法-常用查找算法
0.前言 1.find #include <iostream> using namespace std;// 常用查找算法 find #include<vector> #include<algorithm>//查找 内置数据类型 void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}//查找 容器中 是否有 5 这个元…...

vue3 webpack打包流程及安装 (1)
npm run build 也可以打包 如果没有特殊需求 可以使用 效果其实是差不多的 --------------------------------------------------------------------------------------------------------------------------------- webpack网址 : 起步 | webpack 中文文档 (docsc…...

【C++】内联函数 ① ( 内联函数引入 | 内联函数语法 )
文章目录 一、内联函数引入1、内联函数引入2、代码示例 - 宏代码片段 与 内联函数 二、内联函数语法1、内联函数语法说明2、代码示例 - 内联函数基本语法 一、内联函数引入 1、内联函数引入 " 内联函数 " 是 C 语言中的一种特殊函数 , 其目的是为了提高程序的执行效率…...
聊聊springboot的ConfigurationProperties的绑定
序 本文主要研究一下springboot的ConfigurationProperties的绑定 ConfigurationPropertiesBindingPostProcessor org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java /*** {link BeanPostProcessor} to bind {link PropertySo…...
Mysql和Oracle的语法区别?
Mysql和Oracle是两种不同的关系型数据库。 MySQL通常在中小型应用程序、Web应用程序和小型企业中广泛使用,因为它易于学习和部署,而且成本较低。 Oracle数据库通常用于大型企业和复杂的企业级应用程序,因为它提供了高度可扩展性、高可用性…...
F - LIS on Tree
F - LIS on Tree (atcoder.jp) 问题描述:树上LIS。 普通LIS。O(n * n)。 void solve() {int n; cin>>n;vector<int> f(n 1),a(n1);for(int i 1; i < n; i) {cin>>a[i];f[i] 1;for(int j 1; j < i; j) {if(a[i] > a[j]) f[i] max…...

2023 年全国大学生数学建模B题目-多波束测线问题
B题目感觉属于平面几何和立体几何的问题,本质上需要推导几何变换情况,B题目属于有标准答案型,没太大的把握不建议选择,可发挥型不大。 第一问 比较简单,就一个2维平面的问题,但有点没理解,这个…...

qt creater11 翻译国际化教程教程:
先出效果图。 闲聊几句:qt这个翻译很方便,能直接导出项目里所有文字。 具体步骤如下: 在Qt中,我们可以使用QTranslator类来实现多语言切换。以下是一般步骤: 1. 在你的源代码中,所有需要翻译的字符串都…...

【AWS实验 】在 AWS Fargate 上使用 Amazon ECS 部署应用程序
文章目录 实验概览目标实验环境任务 1:连接到实验命令主机任务 2:将应用程序容器化任务 3:构建 Web2048 容器任务 4:创建 Amazon ECR 存储库并推送 Docker 映像任务 5:创建 ECS 集群任务 6:测试应用程序总结…...
matlab几种求解器的选择fsolve-sole-vpasolve
文章目录 fsolvesolvevpasovle总结vpasovle的结果fsovle的结果 fsolve 求数值解 result_xfsolve(my_fun,x0,options)参数: my_fun:待求解函数,作为一个.m文件 x0:初始值,向量,可以仅仅指定其中的几项solve 强大的求解器。在方程组中求解析…...
无限访问 GPT-4,OpenAI 强势推出 ChatGPT 企业版!
继 ChatGPT 收费大降价、推出 App 版等系列动作之后,OpenAI 于今日宣布正式发布面向企业的 AI 助手——ChatGPT Enterprise 版。 与 To C 端的 ChatGPT 版本有所不同的是,该版本可以以更快速度无限制地访问 GPT-4,还可以用来处理更长输入的上…...
MySQL的故事——Schema与数据类型优化
Schema与数据类型优化 一、选择优化的数据类型 更小的通常更好 应该尽量使用可以正确存储数据的最小类型,更小的数据类型通常更快,因为他们占用更少的磁盘,内存和CPU缓存,并且处理时需要的CPU周期更少 简单就好 更简单的数据类型…...
C++编译和链接
编译和链接 一、源代码的组织 头文件(.h):#include头文件、函数的声明、结构体的声明、类的声明、模板的声明、内联函数、#define和const定义的常量等。 源文件(.cpp):函数的定义、类的定义、模板具体化的…...
【CSDN技术】Markdown编辑器如何使用-csdn博客编写入门
Markdown编辑器如何使用-csdn博客编写入门 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自…...

【docker】运行redis
拉取redis镜像 有多种选择: redis(基础版)redis/redis-stack(包含redis stack server和RedisInsight)redis/redis-stack-server(仅包含redis stack server) docker pull redis docker pull r…...

Paddle训练COCO-stuff数据集学习记录
COCO-stuff数据集 COCO-Stuff数据集对COCO数据集中全部164K图片做了像素级的标注。 80 thing classes, 91 stuff classes and 1 class ‘unlabeled’ 数据集下载 wget --directory-prefixdownloads http://images.cocodataset.org/zips/train2017.zip wget --directory-prefi…...
SpringBoot 框架学习
java 学习笔记指路 基础知识 Python转java补充知识 Java中常见的名词解释 前端 【黑马程序员pink老师前端】HTML 【黑马程序员pink老师前端】JavaScript基础大总结 【黑马程序员pink老师前端】JavaScript函数与作用域 【黑马程序员pink老师前端】JavaScript对象 数据库 【黑马程…...

java - lua - redis 完成商品库存的删减
java调用lua脚本完成对商品库存的管理 主页链接 微风轻吟挽歌的主页 如若有帮助请帮忙点赞 //lua脚本 获取到内存不够的商品StringBuilder sb new StringBuilder();//定义一个数组存储可能缺少库存的值sb.append(" local table {} ");//获取值sb.append(" …...

dbeaver离线安装clickhouse连接驱动
Clickhouse 数据库连接工具——DBeaver 主要介绍了Clickhouse 数据库连接工具——DBeaver相关的知识,希望对你有一定的参考价值。 Clickhouse 数据库连接工具——DBeaver 1.下载 DBeaver 和 连接驱动 https://dbeaver.io/files/dbeaver-ce-latest-x86_64-setup.…...
2024腾讯校招后端面试真题汇总及其解答(二)
11.如果同时有5个任务在10分钟之后提交,或者更多,那么如果是一个个从队列中拿数据,那么前一个任务会影响后续任务执行时间,说一下解决思路 你的问题是一个典型的并发处理问题。如果你的系统是单线程的,那么的确,前一个任务的执行时间会影响后续任务的执行时间。但是,你…...

IDEA运行Tomcat出现乱码问题解决汇总
最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

工业安全零事故的智能守护者:一体化AI智能安防平台
前言: 通过AI视觉技术,为船厂提供全面的安全监控解决方案,涵盖交通违规检测、起重机轨道安全、非法入侵检测、盗窃防范、安全规范执行监控等多个方面,能够实现对应负责人反馈机制,并最终实现数据的统计报表。提升船厂…...

遍历 Map 类型集合的方法汇总
1 方法一 先用方法 keySet() 获取集合中的所有键。再通过 gey(key) 方法用对应键获取值 import java.util.HashMap; import java.util.Set;public class Test {public static void main(String[] args) {HashMap hashMap new HashMap();hashMap.put("语文",99);has…...
2024年赣州旅游投资集团社会招聘笔试真
2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...
系统设计 --- MongoDB亿级数据查询优化策略
系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log,共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题,不能使用ELK只能使用…...
vue3 字体颜色设置的多种方式
在Vue 3中设置字体颜色可以通过多种方式实现,这取决于你是想在组件内部直接设置,还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法: 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...
linux 错误码总结
1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...

C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。
1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj,再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...
Caliper 配置文件解析:fisco-bcos.json
config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...