STL常用遍历,查找,算法
目录
1.遍历算法
1.1for_earch
1.2transform
2.常用查找算法
2.1find,返回值是迭代器
2.1.1查找内置数据类型
2.1.2查找自定义数据类型
2.2fin_if 按条件查找元素
2.2.1查找内置的数据类型
2.2.2查找内置数据类型
2.3查找相邻元素adjeacent_find
2.4查找指定元素是否存在binarary_search
2.5统计元素的个数count
2.5.1统计内置数据类型
2.5.2统计自定义数据类型
2.6按条件统计元素个数
2.6.1统计内置数据类型
2.6.2统计自定义的数据类型
3.常用排序算法
3.1sort
1.遍历算法

1.1for_earch

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用遍历算法 for_each//利用普通函数实现
void print01(int val)
{cout << val << " ";
}//仿函数(函数对象)本身是个类。不是一个函数
class print02
{
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(), print02());//第三个位置需要传入函数对象//类名加小括号,创建出匿名对象
}
int main()
{test01();system("pause");return 0;
}

1.2transform

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用遍历算法 transform//仿函数(函数对象)本身是个类。不是一个函数
class Transform
{
public://搬运过程中把每个元素取出来在返回回去,由于操作的是int型,所以返回intint operator()(int val){return val+100;//+100在搬到容器中}
};
class Myprint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget;//目标容器vTarget.resize(v.size());//目标容器 需要提前开辟空间,不然报错transform(v.begin(), v.end(), vTarget.begin(), Transform());//最后一个位置函数对象for_each(vTarget.begin(), vTarget.end(), Myprint());//最后一个位置函数对象cout << endl;
}
int main()
{test01();system("pause");return 0;
}
2.常用查找算法

2.1find,返回值是迭代器

2.1.1查找内置数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find//查找 内置数据类型
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(), 50);if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到:" << *it << endl;}
}int main()
{test01();system("pause");return 0;
}
2.1.2查找自定义数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//findclass Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}//重载== 让底层find知道如何对比person数据类型bool operator ==(const Person& p)//const防止修改p{if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
//查找 自定义数据类型
void test02()
{vector<Person>v;//创建数据Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);//放到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);Person p("bbb", 20);//查找是否有和p一样的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()
{test02();system("pause");return 0;
}
2.2fin_if 按条件查找元素

2.2.1查找内置的数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find_if//1.查找内置数据类型
class GreaterFive
{
public://谓词返回boolbool operator()(int val)//find_if的底层也是取出每个元素并解引用,放到重载小括号里去操纵{return val > 5;//大于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(), GreaterFive());//第三个位置是匿名函数对象if (it == v.end()){cout << "没有找到大于5的元素" << endl;}else{cout << "找到大于5的数字为:" << *it << endl;}
}//2.查找自定义数据类型int main()
{test01();system("pause");return 0;
}
2.2.2查找内置数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find_if//2.查找自定义数据类型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};
class Great20
{
public:bool operator()(Person &p)//每个数据类型都是Perosn的数据类型用引用的方式传进来{return p.m_Age > 20;}
};
bool G2(Person& p)
{return p.m_Age > 20;
}
void test02()
{vector<Person>v;//创建数据Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//找年龄大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到姓名:" << (*it).m_Name << "年龄:" << it->m_Age << endl;}vector<Person>::iterator it1 = find_if(v.begin(), v.end(), Great20());if (it1 == v.end()){cout << "没有找到" << endl;}else{cout << "找到姓名:" << (*it1).m_Name << "年龄:" << it1->m_Age << endl;}
}
int main()
{test02();system("pause");return 0;
}
2.3查找相邻元素adjeacent_find

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//adjacent_find
void test01()
{vector<int>v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);vector<int>::iterator it=adjacent_find(v.begin(), v.end());if (it == v.end()){cout << "未找到相邻重复元素" << endl;}else{cout << "找到相邻重复元素:" << *it << endl;}
}int main()
{test01();system("pause");return 0;
}
2.4查找指定元素是否存在binarary_search

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//binary_search 二分查找法,在无序的序列中不可以用
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有9//注意容器必须是有序的序列//如果无序结果未知bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到了元素" << endl;}else{cout << "没找到" << endl;}
}int main()
{test01();system("pause");return 0;
}
2.5统计元素的个数count

2.5.1统计内置数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count//1.统计内置数据类型
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);int num=count(v.begin(), v.end(), 40);cout << "40的元素个数为:" <<num<< endl;int num1 = count(v.begin(), v.end(), 1);cout << "1的元素个数为:" << num1 << endl;//输出0
}int main()
{test01();system("pause");return 0;
}
2.5.2统计自定义数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count//2.统计自定义数据类型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}bool operator==(const Person& p)//底层要加const,{if (m_Age==p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
void test02()
{vector<Person>v;Person p1("刘备", 35);Person p2("关羽", 35);Person p3("张飞", 35);Person p4("赵云", 30);Person p5("曹操", 40);//将人员插入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("诸葛亮", 35);//统计与诸葛亮年龄相同的有几人int num = count(v.begin(), v.end(), p);cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}int main()
{test02();system("pause");return 0;
}
2.6按条件统计元素个数

2.6.1统计内置数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count_if//1.统计内置数据类型
class Greater20
{
public:bool operator()(int val){return val > 20;}
};
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(20);v.push_back(40);v.push_back(20);int num = count_if(v.begin(), v.end(), Greater20());cout << "大于20的元素个数为:" << num << endl;
}int main()
{test01();system("pause");return 0;
}
2.6.2统计自定义的数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count_if//2.统计自定义的数据类型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};class AgeGreater20
{
public:bool operator()(Person &p){return p.m_Age > 20;}
};void test02()
{vector<Person>v;Person p1("刘备", 35);Person p2("关羽", 35);Person p3("张飞", 35);Person p4("赵云", 40);Person p5("曹操", 20);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);//统计 大于20岁人员个数int num = count_if(v.begin(), v.end(), AgeGreater20());cout << "大于20的元素个数为:" << num << endl;
}int main()
{test02();system("pause");return 0;
}
3.常用排序算法

3.1sort

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用排序算法
//sort
void myPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);//利用sort进行升序,默认情况下升序sort(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;//改变为降序sort(v.begin(), v.end(), greater<int>());//greater<int>()内建函数对象,需要包含functional头文件,编译器高的不包含functional头文件也不会出错for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;
}int main()
{test01();system("pause");return 0;
}
bool compare(int a,int b)
{ return a < b; //升序排列,如果改为return a>b,则为降序
}
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++) cout<< a[i]<< endl;
sort(a,a+20,compare);
#include<iostream>
using namespace std;
#include<stack>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<queue>
#include<set>
#include<map>struct Point
{int x;int y;//Point(int xx, int yy) :x(xx), y(yy) {};bool operator < (Point& p) {if (x != p.x) {return x < p.x;} else {return y < p.y;}}
};int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end());for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*输出:1 21 32 1*/system("pause");return 0;
}--------------------------------------------------------------------------------------struct Point
{int x;int y;
};
bool Cmp(Point& p1, Point& p2) {if (p1.x != p2.x) {return p1.x < p2.x;} else {return p1.y < p2.y;}
}int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end(),Cmp);for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*输出:1 21 32 1*/system("pause");return 0;
}
----------------------------------------------------------------------------------------struct Point
{int x;int y;
};
class cmp
{
public:bool operator()(Point& p1, Point& p2)const {if (p1.x != p2.x) {return p1.x < p2.x;} else {return p1.y < p2.y;}}
};int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end(), cmp());for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*输出:1 21 32 1*/system("pause");return 0;
}
相关文章:
STL常用遍历,查找,算法
目录 1.遍历算法 1.1for_earch 1.2transform 2.常用查找算法 2.1find,返回值是迭代器 2.1.1查找内置数据类型 2.1.2查找自定义数据类型 2.2fin_if 按条件查找元素 2.2.1查找内置的数据类型 2.2.2查找内置数据类型 2.3查找相邻元素adjeacent_find 2.4查找指…...
BCC源码内容概览(1)
接前一篇文章:BCC源码编译和安装 本文参考官网中的Contents部分的介绍。 BCC源码根目录的文件,其中一些是同时包含C和Python的单个文件,另一些是.c和.py的成对文件,还有一些是目录。 跟踪(Tracing) exam…...
mysql限制用户登录失败次数,限制时间
mysql用户登录限制设置 mysql 需要进行用户登录次数限制,当使用密码登录超过 3 次认证链接失败之后,登录锁住一段时间,禁止登录这里使用的 mysql: 8.1.0 这种方式不用重启数据库. 配置: 首先进入到 mysql 命令行:然后需要安装两个插件: 在 mysql 命令行中执行: mysql> INS…...
从利用Arthas排查线上Fastjson问题到Java动态字节码技术(下)
上一篇从Arthas的源码引出了Java动态字节码技术,那么这一篇就从几种Java字节码技术出发,看看Arthas是如何通过动态字节码技术做到无侵入的源码增强; Java大部分情况下都是解释执行的,也就是解释.class文件,所以如果我们…...
Ubuntu中安装Anaconda 如何将 路径导入为全局变量
第一步:将你的anaconda 路径复制下来,在终端输入对应路径。 echo export PATH"/home/你的用户名/anaconda3/bin:$PATH" >> ~/.bashrc 第二步:在终端输入下面命令或者重启系统。 source ~/.bashrc 在对应的anaconda安装目…...
【QT】Qt的随身笔记(持续更新...)
目录 Qt 获取当前电脑桌面的路径Qt 获取当前程序运行路径Qt 创建新的文本文件txt,并写入内容如何向QPlainTextEdit 写入内容QTimerQMessageBox的使用QLatin1StringQLayoutC在c头文件中写#include类的头文件与直接写class加类名有何区别mutable关键字前向声明 QFontQ…...
【LeetCode-简单题】589. N 叉树的前序遍历
文章目录 题目方法一:单循环栈做法方法二:递归 题目 方法一:单循环栈做法 关键在于子节点的入栈顺序,决定了子节点的出栈顺序, 因为是前序遍历 所以压栈顺序先让右边的入栈 依次往左 这样左边的节点会在栈顶 这样下次…...
Linphone3.5.2 ARM RV1109音视频对讲开发记录
Linphone3.5.2 ARM RV1109音视频对讲开发记录 说明 这是一份事后记录,主要记录的几个核心关键点,有可能很多细节没有记上,主要是方便后面自己再找回来! 版本 3.5.2 一些原因选的是这样一个旧的版本! 新的开发最好选新一些的版…...
Unity用相机实现的镜子效果
首先登场 场景中的元素 mirror是镜子,挂着我们的脚本,Quad是一个面片。Camera是用来生成RenderTexture给面片的。里面的test1是我用来调试位置的球。 镜子size是大小,x是-2,为了反转一下贴图 相机直接可以禁用掉,用…...
计算机网络分类
按照覆盖范围分类 (1)个域网:通常覆盖范围在1~10m。 (2)局域网:通常覆盖范围在10m~1km。 (3)城域网:覆盖范围通常在5~50 km 。 &…...
AI AIgents时代 - (三.) AutoGPT和AgentGPT
前两篇讲解了Agent的原理和组件,这节我将给大家介绍两个agent项目,给出它们的工作原理和区别,并教大家亲手尝试使用 Agents🎉 🟢 AutoGPT🤖️ 我们的老朋友,之前文章也专门写过。AutoGPT 是一…...
Jmeter接口自动化和Python接口自动化,如何选择?
选择Jmeter或Python进行接口自动化测试取决于您的具体需求和环境。以下是一些可以考虑的因素: 1. 语言熟悉度:如果您对Java更熟悉,那么Jmeter可能是更好的选择。而如果您的团队或个人对Python更熟悉,那么Python可能是更好的选择。…...
Sqilte3初步教程
文章目录 安装创建数据库创建和删除表插入行数据 安装 Windows下安装,首先到下载页面,下载Windows安装软件,一般是 sqlite-dll-win32-*.zip sqlite-tools-win32-*.zip下载之后将其内容解压到同一个文件夹下,我把它们都放在了D:\…...
详解Python中的json库
目录 1. json简介2. dumps/loads3. dump/load4. jsonl格式 1. json简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在不同应用程序之间传递数据。它是一种文本格式,易于阅读和编写,同时也易于…...
【Spring Boot】Spring Boot源码解读与原理剖析
这里写目录标题 前言精进Spring Boot首选读物“小册”变“大书”,彻底弄懂Spring Boot全方位配套资源,学不会来找我!技术新赛道,2023领先抢跑 前言 承载着作者的厚望,掘金爆火小册同名读物《Spring Boot源码解读与原理…...
C++学习(1)
一、C概述(了解) C在C语言的基础上添加了面向对象编程和泛型编程的支持 二、helloword程序(掌握) #define _CET_SECURE_NO_WARNINGS//在开发软件visual studio编译 c文件时, visual studio认为strcpy,scanf等函数不安全的导致报…...
机器人如何有效采摘苹果?
摘要:本文利用动捕数据构建拟人运动模型,对比观察两种苹果采摘模式,并对系统性能进行全面评估,为提高机器人采摘效率提供创新方法。 近期,一项关于苹果采摘机器人的有趣研究—— "Design and evaluation of a rob…...
C# OpenCvSharp Yolov8 Detect 目标检测
效果 项目 代码 using OpenCvSharp; using OpenCvSharp.Dnn; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace Open…...
rust数组
一、定义数组 (一)一维数组 1.指定所有元素 语法格式 let variable_name: [dataType; size] [value1,value2,value3];例如 let arr: [i32; 4] [10,20,30,40];2.指定初始值和长度 所有元素具有相同的值 语法格式 let variable_name: [dataType; siz…...
ubuntu | 安装NVIDIA套件:驱动、CUDA、cuDNN
CUDA 查看支持最高的cuda版本 nvidia-smiCUDA Version:12.2 区官网下在12.2.x最新的版本即可CUDA Toolkit Archive | NVIDIA Developer 下载安装 wget https://developer.download.nvidia.com/compute/cuda/12.2.2/local_installers/cuda_12.2.2_535.104.05_linux.run sudo…...
synchronized 学习
学习源: https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖,也要考虑性能问题(场景) 2.常见面试问题: sync出…...
java_网络服务相关_gateway_nacos_feign区别联系
1. spring-cloud-starter-gateway 作用:作为微服务架构的网关,统一入口,处理所有外部请求。 核心能力: 路由转发(基于路径、服务名等)过滤器(鉴权、限流、日志、Header 处理)支持负…...
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...
React Native 导航系统实战(React Navigation)
导航系统实战(React Navigation) React Navigation 是 React Native 应用中最常用的导航库之一,它提供了多种导航模式,如堆栈导航(Stack Navigator)、标签导航(Tab Navigator)和抽屉…...
循环冗余码校验CRC码 算法步骤+详细实例计算
通信过程:(白话解释) 我们将原始待发送的消息称为 M M M,依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)(意思就是 G ( x ) G(x) G(x) 是已知的)࿰…...
测试markdown--肇兴
day1: 1、去程:7:04 --11:32高铁 高铁右转上售票大厅2楼,穿过候车厅下一楼,上大巴车 ¥10/人 **2、到达:**12点多到达寨子,买门票,美团/抖音:¥78人 3、中饭&a…...
MODBUS TCP转CANopen 技术赋能高效协同作业
在现代工业自动化领域,MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步,这两种通讯协议也正在被逐步融合,形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...
解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错
出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上,所以报错,到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本,cu、torch、cp 的版本一定要对…...
如何理解 IP 数据报中的 TTL?
目录 前言理解 前言 面试灵魂一问:说说对 IP 数据报中 TTL 的理解?我们都知道,IP 数据报由首部和数据两部分组成,首部又分为两部分:固定部分和可变部分,共占 20 字节,而即将讨论的 TTL 就位于首…...
JVM虚拟机:内存结构、垃圾回收、性能优化
1、JVM虚拟机的简介 Java 虚拟机(Java Virtual Machine 简称:JVM)是运行所有 Java 程序的抽象计算机,是 Java 语言的运行环境,实现了 Java 程序的跨平台特性。JVM 屏蔽了与具体操作系统平台相关的信息,使得 Java 程序只需生成在 JVM 上运行的目标代码(字节码),就可以…...
