STL常用遍历、查找算法

目录

算法概述

常用遍历算法for_each

常用遍历算法transform

常用查找算法find

常用查找算法find_if

常用查找算法adjacent_find

常用查找算法binary_search

常用查找算法count

常用查找算法count_if


算法概述

        算法主要是由头文件<algorithm><functional> <numeric>组成
        <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等。<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
<functiona>定义了一些模板类用以声明函数对象。

常用遍历算法for_each

        for_each //遍历容器

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;void print01(int v)
{cout << v << " ";
}
//仿函数
class print02
{
public:void operator()(int i){cout << i<< " ";}
};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());cout << endl;}int main()
{test01();return 0;
}

编译运行

常用遍历算法transform

 功能描述:
        搬运容器到另一个容器中
函数原型:
        transform(iterator beg1, iterator end1, iterator beg2, func);
        //beg1源容器开始迭代器
        //end1源容器结束选代器
        //beg2目标容器开始迭代器
        //_func函数或者函数对象

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;class mytransform
{
public:int operator()(int i){return i + 100;}
};class myprint
{
public:void operator()(int i){cout << i << " ";}
};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(),mytransform());//搬运for_each(v2.begin(),v2.end(),myprint());//输出cout << endl;
}int main()
{test01();return 0;
}

编译运行

常用查找算法find

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

find//查找元素
find if//按条件查找元素
adjacent find//查找相邻重复元素
binary_search//二分查找法
count//统计元素个数
count if//按条件统计元素个数

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;class person
{
public:person(string name,int age){this->m_age = age;this->m_name = name;}bool operator==(const person& p){if(this->m_name == p.m_name && this->m_age == p.m_age){return true;}else{return false;}	}
public:int m_age;string m_name;
};void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);}vector<int>::iterator it = find(v.begin(),v.end(),5);if(it == v.end()){cout << "没有找到"<< endl;}else{cout << "找到元素:"<<*it << endl;}
}void test02()
{vector<person>v;person p1("aaa",10);person p2("aaa",20);person p3("aaa",30);person p4("aaa",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<person>::iterator it = find(v.begin(),v.end(),p2);if(it == v.end()){cout << "没有找到" << endl;}else{cout << "年龄:" << it->m_age<<"姓名:" <<it->m_name << endl;}
}int main()
{test01();test02();return 0;
}

常用查找算法find_if

find if(iterator beg, iterator end,Pred);// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg开始选代器
//end 结束选代器
//_Pred函数或者谓词(返回bool类型的仿函数)

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//内置数据类型
class GreaterFive
{
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(),GreaterFive());if(it == v.end()){cout << "没有找到"<< endl;}else{cout << "找到元素:"<<*it << endl;}
}
//自定义数据类型
class person
{
public:person(string name,int age){this->m_age = age;this->m_name = name;}bool operator==(const person& p){if(this->m_name == p.m_name && this->m_age == p.m_age){return true;}else{return false;}	}
public:int m_age;string m_name;
};class Greater20
{
public:bool operator()(person &p){return p.m_age > 20;}
};void test02()
{vector<person>v;person p1("aaa",10);person p2("aaa",20);person p3("aaa",30);person p4("aaa",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(),Greater20());if(it == v.end()){cout << "没有找到" << endl;}else{cout << "年龄:" << it->m_age<<"姓名:" <<it->m_name << endl;}
}int main()
{test01();test02();return 0;
}

编译运行

常用查找算法adjacent_find

adjacent find(iterator beg, iterator end);
//查找相邻重复元素,返回相邻元素的第一个位置的选代器
// beg开始选代器
//end结束选代器

#include <set>
#include <map>
#include<functional>using namespace std;void test01()
{vector<int>v;v.push_back(0);v.push_back(1);v.push_back(0);v.push_back(2);v.push_back(2);v.push_back(3);v.push_back(4);vector<int>::iterator it = adjacent_find(v.begin(),v.end());if(it == v.end()){cout << "没有找到"<< endl;}else{cout << "找到元素:"<<*it << endl;}
}int main()
{test01();return 0;
}

编译运行

找到元素: 6

bool binary_search(iterator beg, iterator end, value);
//查找指定的元素,查到返true 否则false
//注意在无序序列中不可用
// beg开始选代器//end结束选代器
// value 查找的元素

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);}	bool ret = binary_search(v.begin(),v.end(),2);if(!ret){cout << "没有找到"<< endl;}else{cout << "找到元素2"<< endl;}
}int main()
{test01();return 0;
}

常用查找算法count

count(iterator beg, iterator end, value);
// 统计元素出现次数
// beg开始选代器//end结束选代器
// value统计的元素

​
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//统计内置数据类型
void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);}	int num = count(v.begin(),v.end(),2);cout << "元素2出现的次数:"<< num << endl;
}
//统计自定义数据类型
class person
{
public:person(string name,int age){this->m_age = age;this->m_name = name;}bool operator==(const person &p){if(this->m_age == p.m_age){return true;}else{return false;}}
public:int m_age;string m_name;
};void test02()
{vector<person>v;person p1("aaa",10);person p2("bbb",20);person p3("ccc",30);person p4("ddd",40);person p5("eee",50);person p6("fff",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);int num = count(v.begin(),v.end(),p6);cout << "和fff同龄的有:"<< num<<endl;}
int main()
{test01();test02();return 0;
}​

编译运行

常用查找算法count_if

count if(iterator beg, iterator end, Pred):
// 按条件统计元素出现次数
// beg开始迭代器
//end结束选代器
//_Pred谓词

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//统计内置数据类型
class greater2
{
public:bool operator()(int val){return val > 2;}
};void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);}	int num = count_if(v.begin(),v.end(),greater2());cout << "元素大于2出现的次数:"<< num << endl;
}
//统计自定义数据类型
class person
{
public:person(string name,int age){this->m_age = age;this->m_name = name;}bool operator==(const person &p){if(this->m_age == p.m_age){return true;}else{return false;}}
public:int m_age;string m_name;
};class greater20
{
public:bool operator()(const 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);person p5("eee",50);person p6("fff",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);int num = count_if(v.begin(),v.end(),greater20());cout << "大于20岁的人有:"<< num<<endl;}
int main()
{test01();test02();return 0;
}

编译运行

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

DAMO-YOLO训练KITTI数据集

1.KITTI数据集准备 DAMO-YOLO支持COCO格式的数据集&#xff0c;在训练KITTI之前&#xff0c;需要将KITTI的标注转换为KITTI格式。KITTI的采取逐个文件标注的方式确定的&#xff0c;即一张图片对应一个label文件。下面是KITTI 3D目标检测训练集的第一个标注文件&#xff1a;000…

Kubernetes 学习总结(38)—— Kubernetes 与云原生的联系

一、什么是云原生&#xff1f; 伴随着云计算的浪潮&#xff0c;云原生概念也应运而生&#xff0c;而且火得一塌糊涂&#xff0c;大家经常说云原生&#xff0c;却很少有人告诉你到底什么是云原生&#xff0c;云原生可以理解为“云”“原生”&#xff0c;Cloud 可以理解为应用程…

c语言练习67:写一个宏,可以将一个整数的二进制位的奇数位和偶数位交换。

写一个宏&#xff0c;可以将一个整数的二进制位的奇数位和偶数位交换。 #define SwapIntBit(n) (((n) & 0x55555555) << 1 | ((n) & 0xaaaaaaaa) >> 1) 交换奇偶位&#xff0c;需要先分别拿出奇偶位。既然是宏&#xff0c;分别拿出用循环不是很现实&…

Python 编程基础 | 第一章-预备知识 | 1.5、开发工具

一、开发工具 - VSCode VSCode是一个相当优秀的IDE&#xff0c;具备开源、跨平台、模块化、插件丰富、轻量化、启动时间快、颜值高的特质。 1、下载VSCode VSCode下载地址&#xff1a;https://code.visualstudio.com/ 2、安装VSCode 载软件包&#xff0c;一步步安装即可&#x…

nodejs+vue 大学生就业管理系统

随着信息化时代的到来&#xff0c;管理系统都趋向于智能化、系统化&#xff0c;学生就业管理系统也不例外&#xff0c;但目前国内仍都使用人工管理&#xff0c;市场规模越来越大&#xff0c;同时信息量也越来越庞大&#xff0c;人工管理显然已无法应对时代的变化&#xff0c;而…

DataX - 在有总bps限速条件下,单个channel的bps值不能为空,也不能为非正数

更新服务器上的datax版本后&#xff0c;发现执行以前的任务全都失败&#xff0c;查看日志都有报 com.alibaba.datax.common.exception.DataXException: Code:[Framework-03], Description:[DataX引擎配置错误&#xff0c;该问题通常是由于DataX安装错误引起&#xff0c;请联系…

LeetCode力扣018:罗马数字转整数

罗马数字转整数 代码实现 class Solution(object):def romanToInt(self, s):""":type s: str:rtype: int"""nlen(s)sum0for i in range(0,n):dic {I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000}if i1!n:if s[i]I:if s[i1] V or s[i1]X…

【文件操作——详细讲解】

1. 为什么使用文件&#xff1f;&#x1f9d0; 如果没有⽂件&#xff0c;我们写的程序的数据是存储在电脑的内存中&#xff0c;如果程序退出&#xff0c;内存回收&#xff0c;数据就丢失了&#xff0c;等再次运⾏程序&#xff0c;是看不到上次程序的数据的&#xff0c;如果要将数…

skywalking源码本地编译运行经验总结

前言 最近工作原因在弄skywalking&#xff0c;为了进一步熟悉拉了代码下来准备debug&#xff0c;但是编译启动项目我就费了老大劲了&#xff0c;所以准备写这篇&#xff0c;帮兄弟们少踩点坑。 正确步骤 既然是用开源的东西&#xff0c;那么最好就是按照人家的方式使用&…

面试打底稿② 专业技能的第二部分

简历原文 抽查部分 比较熟悉Nacos、Feign、SpringCloud Gateway等微服务的使用&#xff0c;有实际上手项目使用的经验&#xff1b;基本掌握Linux常用命令&#xff0c;了解Linux系统管理、网络管理、生产环境等必用服务&#xff0c;了解Docker的使用&#xff0c;在博客中多有关…

AI智能语音机器人的优势

1.高效自动拨号功能。 导入客户数据&#xff0c;外呼机器人自动拨号&#xff0c;无需看守&#xff0c;真人录音话术&#xff0c;定制场景问答和1秒内的问答响应&#xff0c;为客户带来真实准确的咨询体验。同时&#xff0c;每次通话结束后&#xff0c;外呼系统根据通话时间和关…

【深度学习实验】卷积神经网络(二):自定义简单的二维卷积神经网络

目录 一、实验介绍 二、实验环境 1. 配置虚拟环境 2. 库版本介绍 三、实验内容 0. 导入必要的工具包 1. 二维互相关运算&#xff08;corr2d&#xff09; 2. 二维卷积层类&#xff08;Conv2D&#xff09; a. __init__&#xff08;初始化&#xff09; b. forward(前向传…

Databend 开源周报第112期

Databend 是一款现代云数仓。专为弹性和高效设计&#xff0c;为您的大规模分析需求保驾护航。自由且开源。即刻体验云服务&#xff1a;https://app.databend.cn 。 Whats On In Databend 探索 Databend 本周新进展&#xff0c;遇到更贴近你心意的 Databend 。 理解用户自定义…

什么是物联网智慧公厕?

在当今科技快速发展的背景下&#xff0c;具备全感知、可靠传输、智能处理三大特点的物联网技术&#xff0c;正逐渐渗透到各个领域。而智慧公厕作为其中的一个创新应用&#xff0c;正逐渐受到市场的关注和重视。 什么是物联网智慧公厕&#xff1f;物联网智慧公厕是指通过物联网…

C++之互斥锁、读写锁、互斥量、 信号量、原子锁机制总结(二百二十五)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

【知识点随笔分析】我看看谁还不会用CURL命令

目录 前言&#xff1a; CURL介绍&#xff1a; CURL的基本使用&#xff1a; CURL与PING命令的区别&#xff1a; CURL命令的应用&#xff1a; 总结&#xff1a; 前言&#xff1a; 当今互联网时代&#xff0c;与服务器进行数据交互成为了无法回避的需求。无论是获取Web…

C++,对象赋值与对象拷贝的区别、深浅拷贝

在C中&#xff0c;对象赋值和对象拷贝是两个不同的操作&#xff0c;它们有明显的区别&#xff1a; 1. 对象赋值&#xff08;Object Assignment&#xff09;&#xff1a; - 对象赋值是指将一个已经存在的对象的值复制给另一个已经存在的对象。这通常通过赋值操作符&#xff08;…

MySQL索引看这篇就够了

能简单说一下索引的分类吗&#xff1f; 例如从基本使用使用的角度来讲&#xff1a; 主键索引: InnoDB 主键是默认的索引&#xff0c;数据列不允许重复&#xff0c;不允许为 NULL&#xff0c;一个表只能有一个主键。唯一索引: 数据列不允许重复&#xff0c;允许为 NULL 值&…

SpringBoot之视图解析

文章目录 前言一、视图解析1.视图解析原理流程 二、模板引擎——Thymeleaf基本语法表达式字面量文本操作数学运算布尔运算比较运算条件运算特殊操作设置属性值-th:attr迭代条件运算属性优先级 提取公共页面th:insertth:replace区别 总结 前言 SpringBoot默认不支持 JSP&#x…

黎明加水印微信小程序源码 支持流量主接入

黎明加水印微信小程序源码&#xff0c;支持流量主接入。支持从聊天记录选择文件、相机拍摄、直接选择文件 支持白底、黑底的隐形水印&#xff0c;制作后&#xff0c;通过增加蒙版方能看到水印 纯前端&#xff0c;可嵌入任何项目。 部署教程 1、解压后得到项目文件夹 3、把…