c++ stl 遍历算法和查找算法

概述:

算法主要由头文件<algorithm> <functional> <numeric> 提供
<algorithm> 是所有 STL 头文件中最大的一个,提供了超过 90 个支持各种各样算法的函数,包括排序、合并、搜索、去重、分解、遍历、数值交换、拷贝和替换、插入和删除等
<functional> 定义了一些模板类,用以声明函数对象。函数对象(function object)是一个重载了函数调用操作符(operator())的类。
<numeric> 定义了执行算术运算的一些模板函数。

1. 常用遍历算法

学习目标:

掌握常用的遍历算法

算法简介:

for_each // 遍历容器
transform // 搬运容器到另一个容器中

1.1 for_each

函数原型

for_each(iterator beg, iterator end, _func) 
beg 起始迭代器
end 结束迭代器
_func 函数或者函数对象
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  普通函数
void print01(int val){cout << val << " ";
}// 防函数
class PrintData{
public:void operator()(int val){cout << val << " ";}
};void test01()
{// 逻辑非vector<int> v;for(int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), print01);cout << endl;// 防函数for_each(v.begin(), v.end(), PrintData());cout << endl;// Lambda 表达式for_each(v.begin(), v.end(), [](int val){cout << val << " ";});cout << endl;
}int main(int argc, char const *argv[])
{test01();return 0;
}

1.2 transform

概念:

搬运容器到另一个容器中

函数原型:

transform(iterator beg1, iterator end1, iteartor beg2, _fuc); 
beg1 原容器开始迭代器
end1 原容器结束迭代器
beg2 目标起始迭代器
_fuc 函数或者函数对象
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  stL 常用算法   transform// transform(iterator beg1, iterator end1, iteartor beg2, _fuc); class Transform{
public:int operator()(int val){// 将数字翻倍return val * 2;}};void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int> v2;v2.resize(v.size()); // 目标容器需要提前开辟空间transform(v.begin(), v.end(), v2.begin(),Transform());// 遍历容器for_each(v2.begin(), v2.end(), [](int val){cout << val << " ";});
}int main(int argc, char const *argv[])
{test01();return 0;
}

2. 常用查找算法

算法简介

find  // 查找元素
find_if  // 按条件查找元素
adjacent_find  // 查找相邻重复元素
binary_search  // 二分查找
count  // 统计元素个数
count_if  // 按条件统计元素个数

2.1 find

查找指定元素,找到返回指定元素的迭代器,找不到返回end()

函数原型

find(begin, end, val) 
begin 开始迭代器
end 结束迭代器
val 查找的元素
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  stL 常用查找算法// find // 查找内置的数据类型
void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}// 查找容器中是否有6vector<int>::iterator it = find(v.begin(), v.end(), 6);if(it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素为:" << *it << endl;}}// 查找自定义数据类型
class Person{
public:Person(string name, int age):m_Name(name),m_Age(age){}string m_Name;int m_Age;// 重载==bool operator==(const Person &p){if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}
};void test02(){vector<Person> v;Person p1("西施", 18);Person p2("王昭君", 19);Person p3("杨玉环", 17);Person p4("貂蝉", 16);Person p5("小乔", 15);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("貂蝉", 16);vector<Person>::iterator it =  find(v.begin(), v.end(), p);if(it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素为:" << it->m_Name << " " << it->m_Age << endl;}
}int main(int argc, char const *argv[])
{test02();return 0;
}

查找自定义数据,必须重载==

2.2 find_if

功能描述:

按条件查找元素

函数原型

find_if(iterator beg, iterator end, _Pred) 
功能描述:按条件查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
注意:_Pred为谓词(返回bool类型的防函数) 或 函数
beg 开始迭代器
end 结束迭代器
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  stL 常用查找算法// find_if// 查找内置的数据类型
void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}// 用lambda表达式实现 查找容器中是有大于6 vector<int>::iterator it = find_if(v.begin(), v.end(), [&](int val){return val > 6;});if(it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素为:" << *it << endl;}}// 查找自定义数据类型
class Person{
public:Person(string name, int age):m_Name(name),m_Age(age){}string m_Name;int m_Age;bool operator==(const Person &p){if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}
};class findPerson{
public:bool operator()(const Person &p){// 查找年龄大于17的if (p.m_Age > 17){return true;}else{return false;}}
};void test02(){vector<Person> v;Person p1("西施", 18);Person p2("王昭君", 19);Person p3("杨玉环", 17);Person p4("貂蝉", 16);Person p5("小乔", 15);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("貂蝉", 16);vector<Person>::iterator it =  find_if(v.begin(), v.end(), findPerson());if(it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素为:" << it->m_Name << " " << it->m_Age << endl;}
}int main(int argc, char const *argv[])
{test02();return 0;
}

2.3 adjacent_find

功能描述:

查找相邻重复元素

函数原型:

adjacent_find(iterator first, iterator last, binary_predicate pred);
功能描述:
查找相邻重复元素,返回相邻元素的第一个位置的迭代器
参数说明:
first:开始迭代器
last:结束迭代器
pred:二元谓词
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  stL 常用查找算法// adjacent_find// 查找内置的数据类型
void test01()
{vector<int> v;v.push_back(10);v.push_back(20);v.push_back(20);v.push_back(30);v.push_back(40);v.push_back(30);v.push_back(50);vector<int>::iterator it = adjacent_find(v.begin(), v.end());if (it == v.end()){cout << "找不到" << endl;}else{cout << "找到相邻重复元素为:" << *it << endl;}}// 查找自定义数据类型
class Person{
public:Person(string name, int age):m_Name(name),m_Age(age){}string m_Name;int m_Age;bool operator==(const Person &p){if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}
};class findPerson{
public:bool operator()(const Person &p , const Person &p2){// 查找年龄相同的if (p.m_Age == p2.m_Age){return true;}else{return false;}}
};// 查找自定义数据类型
void test02(){vector<Person> v;Person p1("西施", 18);Person p2("王昭君", 19);Person p3("杨玉环", 19);Person p4("貂蝉", 16);Person p5("小乔", 15);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);vector<Person>::iterator it =  adjacent_find(v.begin(), v.end(), findPerson());if(it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素为:" << it->m_Name << " " << it->m_Age << endl;}
}int main(int argc, char const *argv[])
{test02();return 0;
}

2.4 binary_search

查找指定元素是否存在

函数原型

bool binary_search(InputIterator first, InputIterator last, const T& val);

功能描述:

查找到val在[first, last)区间中,则返回true,否则返回false。
注意:在无序序列中不可用。
beg 开始迭代器
end 结束迭代器
val 查找的元素
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  binary_searchvoid test01()
{vector<int> v;for(int i = 0; i < 10; i++){v.push_back(i);}bool result = binary_search(v.begin(), v.end(), 10);if (result){cout << "find" << endl;}else{cout << "not find" << endl;}}int main(int argc, char const *argv[])
{test01();return 0;
}

2.5 count

功能描述:

统计元素个数

函数原型

count(InputIterator first, InputIterator last, const T& val);

功能描述:

统计出元素次数
beg 开始迭代器
end 结束迭代器
val 查找的元素
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//  count// 统计内置数据类型
void test01()
{vector<int> v;for(int i = 0; i < 10; i++){v.push_back(i);}v.push_back(10);v.push_back(10);int result = count(v.begin(), v.end(), 10);cout << result << endl;}// 统计自定义数据类型
class Person{
public:Person(string name, int age):m_Name(name), m_Age(age){}bool operator==(const Person &p){if(this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test02(){vector<Person> v;v.push_back(Person("西施", 18));v.push_back(Person("小龙女", 18));v.push_back(Person("貂蝉", 20));v.push_back(Person("杨玉环", 18));v.push_back(Person("王昭君", 19));Person p("小乔",18);int result = count(v.begin(), v.end(), p);cout << "和小乔年龄相同的人有" << result << "个";
}int main(int argc, char const *argv[])
{test02();return 0;
}

总结:统计自定义类型的时候,需要重载 operator==

2.6 count_if

功能描述: 按照条件在容器中统计元素个数

函数原型:

count_if(iterator beg, iterator end, _Pred)
beg 开始迭代器
end 结束迭代器
_Pred 谓词
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;#define CEHUA 0
#define MEISHU 1
#define YANFA 2//   count_ifclass Greater{
public:Greater(int val):m_Val(val){}bool operator()(int val){return val > m_Val;}int m_Val; // 可以改变条件
};// 统计内置数据类型
void test01()
{vector<int> v;for(int i = 0; i < 10; i++){v.push_back(i);}v.push_back(10);v.push_back(10);// 统计大于8的数字有多少个int result = count_if(v.begin(), v.end(), Greater(8));cout << result << endl;
}// 统计自定义数据类型
class Person{
public:Person(string name, int age):m_Name(name), m_Age(age){}bool operator==(const Person &p){if(this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};class CountPerson{
public:CountPerson(int age):m_Age(age){}bool operator()(const Person &p){return p.m_Age > m_Age;}int m_Age;
};void test02(){vector<Person> v;v.push_back(Person("西施", 18));v.push_back(Person("小龙女", 18));v.push_back(Person("貂蝉", 20));v.push_back(Person("杨玉环", 18));v.push_back(Person("王昭君", 19));Person p("小乔",18);v.push_back(p);int result = count_if(v.begin(), v.end(), CountPerson(17));cout << "年龄大于17的美女: " << result << "个";
}int main(int argc, char const *argv[])
{test02();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/894505.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2.2 实现双向链表的快速排序

实现一个双向链表的快速排序。 1>程序代码 #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <pthread.h>…

力扣动态规划-19【算法学习day.113】

前言 ###我做这类文章一个重要的目的还是记录自己的学习过程&#xff0c;我的解析也不会做的非常详细&#xff0c;只会提供思路和一些关键点&#xff0c;力扣上的大佬们的题解质量是非常非常高滴&#xff01;&#xff01;&#xff01; 习题 1.矩形中移动的最大次数 题目链接…

Gurobi基础语法之 addConstr, addConstrs, addQConstr, addMQConstr

在新版本的 Gurobi 中&#xff0c;向 addConstr 这个方法中传入一个 TempConstr 对象&#xff0c;在模型中就会根据这个对象生成一个约束。更重要的是&#xff1a;TempConstr 对象可以传给所有addConstr系列方法&#xff0c;所以下面先介绍 TempConstr 对象 TempConstr TempC…

neo4j-community-5.26.0 create new database

1.edit neo4j.conf 把 # The name of the default database initial.dbms.default_databasehonglouneo4j # 写上自己的数据库名称 和 # Name of the service #5.0 server.windows_service_nameneo4j #4.0 dbms.default_databaseneo4j #dbms.default_databaseneo4jwind serve…

unity实现回旋镖函数

最近学习unity2D&#xff0c;想实现一个回旋镖武器&#xff0c;发出后就可以在角色周围回旋。 一、目标 1.不是一次性的&#xff0c;扔出去、返回、没有了&#xff1b;而是扔出去&#xff0c;返回到角色后方相同距离&#xff0c;再次返回&#xff1b;再次返回&#xff0c;永远…

【C++基础】字符串/字符读取函数解析

最近在学C以及STL&#xff0c;打个基础 参考&#xff1a; c中的char[] ,char* ,string三种字符串变量转化的兼容原则 c读取字符串和字符的6种函数 字符串结构 首先明确三种字符串结构的兼容关系&#xff1a;string>char*>char [] string最灵活&#xff0c;内置增删查改…

SpringBoot源码解析(九):Bean定义接口体系

SpringBoot源码系列文章 SpringBoot源码解析(一)&#xff1a;SpringApplication构造方法 SpringBoot源码解析(二)&#xff1a;引导上下文DefaultBootstrapContext SpringBoot源码解析(三)&#xff1a;启动开始阶段 SpringBoot源码解析(四)&#xff1a;解析应用参数args Sp…

C++模板编程——可变参函数模板

目录 1. 可变参函数模板基本介绍 2. 参数包展开——通过递归函数 3. 参数包展开——通过编译期间if语句(constexpr if) 4. 重载 5. 后记 进来看的小伙伴们应该对C中的模板有了一定了解&#xff0c;下面给大家介绍一下可变参函数模板。过于基础的概念将不仔细介绍。 1. 可变…

ChatGPT-4o和ChatGPT-4o mini的差异点

在人工智能领域&#xff0c;OpenAI再次引领创新潮流&#xff0c;近日正式发布了其最新模型——ChatGPT-4o及其经济实惠的小型版本ChatGPT-4o Mini。这两款模型虽同属于ChatGPT系列&#xff0c;但在性能、应用场景及成本上展现出显著的差异。本文将通过图文并茂的方式&#xff0…

2025最新源支付V7全套开源版+Mac云端+五合一云端

2025最新源支付V7全套开源版Mac云端五合一云端 官方1999元&#xff0c; 最新非网上那种功能不全带BUG开源版&#xff0c;可以自己增加授权或二开 拥有卓越的性能和丰富的功能。它采用全新轻量化的界面UI&#xff0c;让您能更方便快捷地解决知识付费和运营赞助的难题 它基于…

数据分析系列--[12] RapidMiner辨别分析(含数据集)

一、数据准备 二、导入数据 三、数据预处理 四、建模辨别分析 五、导入测试集进行辨别分析 一、数据准备 点击下载数据集 二、导入数据 三、数据预处理 四、建模辨别分析 五、导入测试集进行辨别分析 Ending, congratulations, youre done.

当卷积神经网络遇上AI编译器:TVM自动调优深度解析

从铜线到指令&#xff1a;硬件如何"消化"卷积 在深度学习的世界里&#xff0c;卷积层就像人体中的毛细血管——数量庞大且至关重要。但鲜有人知&#xff0c;一个简单的3x3卷积在CPU上的执行路径&#xff0c;堪比北京地铁线路图般复杂。 卷积的数学本质 对于输入张…

51单片机 02 独立按键

一、独立按键控制LED亮灭 轻触按键&#xff1a;相当于是一种电子开关&#xff0c;按下时开关接通&#xff0c;松开时开关断开&#xff0c;实现原理是通过轻触按键内部的金属弹片受力弹动来实现接通和断开。 #include <STC89C5xRC.H> void main() { // P20xFE;while(1){…

wax到底是什么意思

在很久很久以前&#xff0c;人类还没有诞生文字之前&#xff0c;人类就产生了语言&#xff1b;在诞生文字之前&#xff0c;人类就已经使用了语言很久很久。 没有文字之前&#xff0c;人们的语言其实是相对比较简单的&#xff0c;因为人类的生产和生活水平非常低下&#xff0c;…

SSRF 漏洞利用 Redis 实战全解析:原理、攻击与防范

目录 前言 SSRF 漏洞深度剖析 Redis&#xff1a;强大的内存数据库 Redis 产生漏洞的原因 SSRF 漏洞利用 Redis 实战步骤 准备环境 下载安装 Redis 配置漏洞环境 启动 Redis 攻击机远程连接 Redis 利用 Redis 写 Webshell 防范措施 前言 在网络安全领域&#xff0…

【周易哲学】生辰八字入门讲解(八)

&#x1f60a;你好&#xff0c;我是小航&#xff0c;一个正在变秃、变强的文艺倾年。 &#x1f514;本文讲解【周易哲学】生辰八字入门讲解&#xff0c;期待与你一同探索、学习、进步&#xff0c;一起卷起来叭&#xff01; 目录 一、六亲女命六亲星六亲宫位相互关系 男命六亲星…

大模型训练(5):Zero Redundancy Optimizer(ZeRO零冗余优化器)

0 英文缩写 Large Language Model&#xff08;LLM&#xff09;大型语言模型Data Parallelism&#xff08;DP&#xff09;数据并行Distributed Data Parallelism&#xff08;DDP&#xff09;分布式数据并行Zero Redundancy Optimizer&#xff08;ZeRO&#xff09;零冗余优化器 …

玉米苗和杂草识别分割数据集labelme格式1997张3类别

数据集格式&#xff1a;labelme格式(不包含mask文件&#xff0c;仅仅包含jpg图片和对应的json文件) 图片数量(jpg文件个数)&#xff1a;1997 标注数量(json文件个数)&#xff1a;1997 标注类别数&#xff1a;3 标注类别名称:["corn","weed","Bean…

Streamlit入门

1、Streamlit是什么 Streamlit 是一个用于快速构建数据应用的开源 Python 库&#xff0c;由 Streamlit 公司开发并维护。它极大地简化了从数据脚本到交互式 Web 应用的转化过程&#xff0c;让开发者无需具备前端开发的专业知识&#xff0c;就能轻松创建出美观、实用的交互式应…

机器学习算法在网络安全中的实践

机器学习算法在网络安全中的实践 本文将深入探讨机器学习算法在网络安全领域的应用实践&#xff0c;包括基本概念、常见算法及其应用案例&#xff0c;从而帮助程序员更好地理解和应用这一领域的技术。"> 序言 网络安全一直是信息技术领域的重要议题&#xff0c;随着互联…