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

algorithm算法库学习之——不修改序列的操作

algorithm此头文件是算法库的一部分。本篇介绍不修改序列的操作函数。

不修改序列的操作

all_ofany_ofnone_of

(C++11)(C++11)(C++11)

检查谓词是否对范围中所有、任一或无元素为 true
(函数模板)

for_each

应用函数到范围中的元素
(函数模板)

for_each_n

(C++17)

应用一个函数对象到序列的前 n 个元素
(函数模板)

countcount_if

返回满足指定判别标准的元素数
(函数模板)

mismatch

寻找两个范围出现不同的首个位置
(函数模板)

findfind_iffind_if_not

(C++11)

寻找首个满足特定判别标准的元素
(函数模板)

find_end

在特定范围中寻找最后出现的元素序列
(函数模板)

find_first_of

搜索元素集合中的任意元素
(函数模板)

adjacent_find

查找首对相邻的相同(或满足给定谓词的)元素
(函数模板)

search

搜索一个元素范围
(函数模板)

search_n

在范围中搜索一定量的某个元素的连续副本
(函数模板)

示例代码:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility>      // std::pair
#include <vector>
#include <array>
#include <cctype>       // std::tolowervoid myfunction(int i) {  // function:std::cout << ' ' << i;
}struct myclass {           // function object type:void operator() (int i) { std::cout << ' ' << i; }
} myobject;bool IsOdd(int i) { return ((i % 2) == 1); }bool mypredicate(int i, int j) {return (i == j);
}bool myfunction8(int i, int j) {return (i == j);
}bool comp_case_insensitive9(char c1, char c2) {return (std::tolower(c1) == std::tolower(c2));
}bool myfunction10(int i, int j) {return (i == j);
}bool mypredicate11(int i, int j) {return (i == j);
}bool mypredicate12(int i, int j) {return (i == j);
}int main()
{// all_of example  检查谓词是否对范围中所有、任一或无元素为 truestd::array<int, 8> foo = { 3,5,7,11,13,17,19,23 };if (std::all_of(foo.begin(), foo.end(), [](int i) {return i % 2; }))std::cout << "All the elements are odd numbers.\n";// any_of examplestd::array<int, 7> foo2 = { 0,1,-1,3,-3,5,-5 };if (std::any_of(foo2.begin(), foo2.end(), [](int i) {return i < 0; }))std::cout << "There are negative elements in the range.\n";// none_of examplestd::array<int, 8> foo3 = { 1,2,4,8,16,32,64,128 };if (std::none_of(foo3.begin(), foo3.end(), [](int i) {return i < 0; }))std::cout << "There are no negative elements in the range.\n";// for_each example  应用函数到范围中的元素 std::vector<int> myvector;myvector.push_back(10);myvector.push_back(20);myvector.push_back(30);std::cout << "myvector contains:";for_each(myvector.begin(), myvector.end(), myfunction);std::cout << '\n';// or:std::cout << "myvector contains:";for_each(myvector.begin(), myvector.end(), myobject);std::cout << '\n';// count algorithm example 返回满足指定判别标准的元素数 // counting elements in array:int myints[] = { 10,20,30,30,20,10,10,20,10,20 };   // 8 elementsint mycount = std::count(myints, myints + 8, 10);std::cout << "10 appears " << mycount << " times.\n";// counting elements in container:std::vector<int> myvector2(myints, myints + 10);mycount = std::count(myvector2.begin(), myvector2.end(), 20);std::cout << "20 appears " << mycount << " times.\n";// count_if examplestd::vector<int> myvector3;for (int i = 1; i < 10; i++) myvector3.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9int mycount3 = count_if(myvector3.begin(), myvector3.end(), IsOdd);std::cout << "myvector3 contains " << mycount3 << " odd values.\n";// mismatch algorithm example 寻找两个范围出现不同的首个位置 std::vector<int> myvector4;for (int i = 1; i < 6; i++) myvector4.push_back(i * 10); // myvector4: 10 20 30 40 50int myints4[] = { 10,20,80,320,1024 };                //   myints4: 10 20 80 320 1024std::pair<std::vector<int>::iterator, int*> mypair4;// using default comparison:mypair4 = std::mismatch(myvector4.begin(), myvector4.end(), myints4);std::cout << "First mismatching elements: " << *mypair4.first;std::cout << " and " << *mypair4.second << '\n';++mypair4.first; ++mypair4.second;// using predicate comparison:mypair4 = std::mismatch(mypair4.first, myvector4.end(), mypair4.second, mypredicate);std::cout << "Second mismatching elements: " << *mypair4.first;std::cout << " and " << *mypair4.second << '\n';// find example// using std::find with array and pointer:int myints5[] = { 10, 20, 30, 40 };int *p;p = std::find(myints5, myints5 + 4, 30);if (p != myints5 + 4)std::cout << "Element found in myints5: " << *p << '\n';elsestd::cout << "Element not found in myints5\n";// using std::find with vector and iterator:std::vector<int> myvector5(myints5, myints5 + 4);std::vector<int>::iterator it;it = find(myvector5.begin(), myvector5.end(), 30);if (it != myvector5.end())std::cout << "Element found in myvector5: " << *it << '\n';elsestd::cout << "Element not found in myvector5\n";// find_if examplestd::vector<int> myvector6;myvector6.push_back(10);myvector6.push_back(25);myvector6.push_back(40);myvector6.push_back(55);std::vector<int>::iterator it6 = std::find_if(myvector6.begin(), myvector6.end(), IsOdd);std::cout << "The first odd value is " << *it6 << '\n';// find_if_not examplestd::array<int, 5> foo7 = { 1,2,3,4,5 };std::array<int, 5>::iterator it7 = std::find_if_not(foo7.begin(), foo7.end(), [](int i) {return i % 2; });std::cout << "The first even value is " << *it7 << '\n';// find_end exampleint myints8[] = { 1,2,3,4,5,1,2,3,4,5 };std::vector<int> haystack8(myints8, myints8 + 10);int needle1[] = { 1,2,3 };// using default comparison:std::vector<int>::iterator it8;it8 = std::find_end(haystack8.begin(), haystack8.end(), needle1, needle1 + 3);if (it8 != haystack8.end())std::cout << "needle1 last found at position " << (it8 - haystack8.begin()) << '\n';int needle2[] = { 4,5,1 };// using predicate comparison:it8 = std::find_end(haystack8.begin(), haystack8.end(), needle2, needle2 + 3, myfunction8);if (it8 != haystack8.end())std::cout << "needle2 last found at position " << (it8 - haystack8.begin()) << '\n';// find_first_of exampleint mychars9[] = { 'a','b','c','A','B','C' };std::vector<char> haystack9(mychars9, mychars9 + 6);std::vector<char>::iterator it9;int needle9[] = { 'A','B','C' };// using default comparison:it9 = find_first_of(haystack9.begin(), haystack9.end(), needle9, needle9 + 3);if (it9 != haystack9.end())std::cout << "The first match is: " << *it9 << '\n';// using predicate comparison:it9 = find_first_of(haystack9.begin(), haystack9.end(),needle9, needle9 + 3, comp_case_insensitive9);if (it9 != haystack9.end())std::cout << "The first match is: " << *it9 << '\n';// adjacent_find exampleint myints10[] = { 5,20,5,30,30,20,10,10,20 };std::vector<int> myvector10(myints10, myints10 + 8);std::vector<int>::iterator it10;// using default comparison:it10 = std::adjacent_find(myvector10.begin(), myvector10.end());if (it10 != myvector10.end())std::cout << "the first pair of repeated elements are: " << *it10 << '\n';//using predicate comparison:it10 = std::adjacent_find(++it10, myvector10.end(), myfunction10);if (it10 != myvector10.end())std::cout << "the second pair of repeated elements are: " << *it10 << '\n';// search algorithm examplestd::vector<int> haystack11;// set some values:        haystack11: 10 20 30 40 50 60 70 80 90for (int i = 1; i < 10; i++) haystack11.push_back(i * 10);// using default comparison:int needle11[] = { 40,50,60,70 };std::vector<int>::iterator it11;it11 = std::search(haystack11.begin(), haystack11.end(), needle11, needle11 + 4);if (it11 != haystack11.end())std::cout << "needle11 found at position " << (it11 - haystack11.begin()) << '\n';elsestd::cout << "needle11 not found\n";// using predicate comparison:int needle21[] = { 20,30,50 };it11 = std::search(haystack11.begin(), haystack11.end(), needle21, needle21 + 3, mypredicate11);if (it11 != haystack11.end())std::cout << "needle21 found at position " << (it11 - haystack11.begin()) << '\n';elsestd::cout << "needle21 not found\n";// search_n exampleint myints12[] = { 10,20,30,30,20,10,10,20 };std::vector<int> myvector12(myints12, myints12 + 8);std::vector<int>::iterator it12;// using default comparison:it12 = std::search_n(myvector12.begin(), myvector12.end(), 2, 30);if (it12 != myvector12.end())std::cout << "two 30s found at position " << (it12 - myvector12.begin()) << '\n';elsestd::cout << "match not found\n";// using predicate comparison:it12 = std::search_n(myvector12.begin(), myvector12.end(), 2, 10, mypredicate12);if (it12 != myvector12.end())std::cout << "two 10s found at position " << int(it12 - myvector12.begin()) << '\n';elsestd::cout << "match not found\n";std::cout << "hello world\n";return 0;
}

运行结果:

参考:

https://cplusplus.com/reference/algorithm/

https://zh.cppreference.com/w/cpp/header/algorithm

相关文章:

algorithm算法库学习之——不修改序列的操作

algorithm此头文件是算法库的一部分。本篇介绍不修改序列的操作函数。 不修改序列的操作 all_ofany_ofnone_of (C11)(C11)(C11) 检查谓词是否对范围中所有、任一或无元素为 true (函数模板) for_each 应用函数到范围中的元素 (函数模板) for_each_n (C17) 应用一个函数对象到序…...

idea创建的maven项目pom文件引入的坐标报红原因

如下所示 我们在引入某些依赖坐标的时候&#xff0c;即使点击了右上角的mavne刷新之后还是报红。 其实这是正常现象&#xff0c;实际上是我们的本地仓库当中没有这些依赖坐标&#xff0c;而idea就会通过报红来标记这些依赖来说明在我们的本地仓库是不存在的。 那有的同学就会…...

Python面试题:Python 中的生成器(generator)是什么?有什么优点?

在Python中&#xff0c;生成器&#xff08;generator&#xff09;是一种特殊的迭代器&#xff0c;使用yield关键字生成值&#xff0c;可以逐个生成序列中的值&#xff0c;而不需要一次性将所有值加载到内存中。生成器函数在定义时使用def关键字&#xff0c;并包含一个或多个yie…...

Go语言--复合类型之map、结构体

map Go 语言中的 map(映射、字典)是一种内置的数据结构&#xff0c;它是一个无序的 key-value 对的集合&#xff0c;比如以身份证号作为唯一键来标识一个人的信息。 格式 map [keyType]valueType 在一个 map 里所有的键都是唯一的&#xff0c;而且必须是支持和!操作符的类型…...

Stable Diffusion图像的脸部细节控制——采样器全解析

文章目录 艺术地掌控人物形象好易智算原因分析为什么在使用Stable Diffusion生成全身图像时&#xff0c;脸部细节往往不够精细&#xff1f; 解决策略 局部重绘采样器总结 艺术地掌控人物形象 在运用Stable Diffusion这一功能强大的AI绘图工具时&#xff0c;我们往往会发现自己…...

CurrentHashMap巧妙利用位运算获取数组指定下标元素

先来了解一下数组对象在堆中的存储形式【数组长度&#xff0c;数组元素类型信息等】 【存放元素对象的空间】 Ma 基础信息实例数据内存填充Mark Word,ClassPointer,数组长度第一个元素第二个元素固定的填充内容 所以我们想要获取某个下标的元素首先要获取这个元素的起始位置…...

实现antd designable平台的组件拖拽功能

平台&#xff1a;designable设计器 github&#xff1a;designable 目录 1 背景2 技术栈3 组件拖拽和放置3.1 类型定义3.2 拖拽3.3 放置 1 背景 由于业务需求&#xff0c;我们需要实现designable平台的一个简易版的组件拖拽功能。 #mermaid-svg-QrxSDGe9YyGG3LbQ {font-family:…...

计算机网络-IP组播基础

一、概述 在前面的学习交换机和路由协议&#xff0c;二层通信是数据链路层间通信&#xff0c;在同一个广播域间通过源MAC地址和目的MAC地址进行通信&#xff0c;当两台主机第一次通信由于不清楚目的MAC地址需要进行广播泛洪&#xff0c;目的主机回复自身MAC地址&#xff0c;然后…...

Git删除了文件拉取时失败

本地删除了一些文件&#xff0c;远端的另一个提交修改了被删除的文件&#xff0c;vs里拉取时提示未处理的提交&#xff0c;无法继续操作&#xff0c;git gui里显示很多unstaged change的项 解决办法&#xff1a; 1、用git bash的git rm --cached filename或 git rm -r --cached…...

【面向就业的Linux基础】从入门到熟练,探索Linux的秘密(十二)-管道、环境变量、常用命令

大致介绍了一下管道、环境变量、一些常用的基本命令&#xff0c;可以当作学习笔记收藏学习一下&#xff01;&#xff01;&#xff01; 文章目录 前言 一、管道 二、环境变量 1.概念 2.查看 3.修改 4.常用环境变量 三、系统状况 总结 前言 大致介绍了一下管道、环境变量、一些常…...

Spring Boot与Apache Kafka Streams的集成

Spring Boot与Apache Kafka Streams的集成 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 一、Apache Kafka Streams简介 Apache Kafka Streams是一个用于构…...

Unity中使用VectorGraphics插件时,VectorUtils.RenderSpriteToTexture2D方法返回结果错误的解决方法

Unity中使用VectorGraphics插件时&#xff0c;如果使用VectorUtils.BuildSprite方法创建Sprite&#xff0c;那么得到的Sprite往往是一个三角网格数比较多的Sprite&#xff0c;如果想要得到使用贴图只有两个三角面的方形Sprite&#xff0c;可以使用该插件提供的VectorUtils.Rend…...

用MySQL+node+vue做一个学生信息管理系统(一):配置项目

先用npm init -y生成配置文件 在项目下新建src文件夹&#xff0c;app.js文件。src目录用来放静态资源文件&#xff0c;app.js是服务器文件&#xff0c;index.js是vue的入口文件 使用npm install express下载express框架 在app.js文件夹开启node服务&#xff0c;监听的端口为…...

2024年06月CCF-GESP编程能力等级认证Python编程二级真题解析

本文收录于专栏《Python等级认证CCF-GESP真题解析》&#xff0c;专栏总目录&#xff1a;点这里&#xff0c;订阅后可阅读专栏内所有文章。 一、单选题&#xff08;每题 2 分&#xff0c;共 30 分&#xff09; 第 1 题 小杨父母带他到某培训机构给他报名参加CCF组织的GESP认证…...

Unity动画系统(2)

6.1 动画系统基础2-3_哔哩哔哩_bilibili p316 模型添加Animator组件 动画控制器 AnimatorController AnimatorController 可以通过代码控制动画速度 建立动画间的联系 bool值的设定 trigger p318 trigger点击的时候触发&#xff0c;如喊叫&#xff0c;开枪及换子弹等&#x…...

深度网络现代实践 - 深度前馈网络之反向传播和其他的微分算法篇

序言 反向传播&#xff08;Backpropagation&#xff0c;简称backprop&#xff09;是神经网络训练过程中最关键的技术之一&#xff0c;尤其在多层神经网络中广泛应用。它是一种与优化方法&#xff08;如梯度下降法&#xff09;结合使用的算法&#xff0c;用于计算网络中各参数的…...

自动化设备上位机设计 四

目录 一 设计原型 二 后台代码 一 设计原型 二 后台代码 using SimpleTCP; using SqlSugar; using System.Text;namespace 自动化上位机设计 {public partial class Form1 : Form{SqlHelper sqlHelper new SqlHelper();SqlSugarClient dbContent null;bool IsRun false;i…...

[leetcode hot 150]第二十三题,合并K个升序链表

题目&#xff1a; 给你一个链表数组&#xff0c;每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中&#xff0c;返回合并后的链表。 示例 1&#xff1a; 输入&#xff1a;lists [[1,4,5],[1,3,4],[2,6]] 输出&#xff1a;[1,1,2,3,4,4,5,6] 解释&#xff1a…...

MybatisPlus实现插入/修改数据自动设置时间

引言 插入数据时自动设置当前时间&#xff0c;更新数据时自动修改日期为修改时的日期。 使用MybatisPlus的扩展接口MetaObjectHandler 步骤 实现接口 实体类加注解 实现接口 package com.example.vueelementson.common;import com.baomidou.mybatisplus.core.handlers.M…...

Java语言程序设计篇一

Java语言概述 Java语言起源编程语言最新排名名字起源Java语言发展历程Java语言的特点Java虚拟机垃圾回收Java语言规范Java技术简介Java程序的结构Java程序注意事项&#xff1a;注释编程风格练习 Java语言起源 1990年Sun公司提出一项绿色计划。1992年语言开发成功最初取名为Oak…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

Docker 离线安装指南

参考文章 1、确认操作系统类型及内核版本 Docker依赖于Linux内核的一些特性&#xff0c;不同版本的Docker对内核版本有不同要求。例如&#xff0c;Docker 17.06及之后的版本通常需要Linux内核3.10及以上版本&#xff0c;Docker17.09及更高版本对应Linux内核4.9.x及更高版本。…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

SpringTask-03.入门案例

一.入门案例 启动类&#xff1a; package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...

关键领域软件测试的突围之路:如何破解安全与效率的平衡难题

在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件&#xff0c;这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下&#xff0c;实现高效测试与快速迭代&#xff1f;这一命题正考验着…...

【Java学习笔记】BigInteger 和 BigDecimal 类

BigInteger 和 BigDecimal 类 二者共有的常见方法 方法功能add加subtract减multiply乘divide除 注意点&#xff1a;传参类型必须是类对象 一、BigInteger 1. 作用&#xff1a;适合保存比较大的整型数 2. 使用说明 创建BigInteger对象 传入字符串 3. 代码示例 import j…...

基于Java Swing的电子通讯录设计与实现:附系统托盘功能代码详解

JAVASQL电子通讯录带系统托盘 一、系统概述 本电子通讯录系统采用Java Swing开发桌面应用&#xff0c;结合SQLite数据库实现联系人管理功能&#xff0c;并集成系统托盘功能提升用户体验。系统支持联系人的增删改查、分组管理、搜索过滤等功能&#xff0c;同时可以最小化到系统…...

Java毕业设计:WML信息查询与后端信息发布系统开发

JAVAWML信息查询与后端信息发布系统实现 一、系统概述 本系统基于Java和WML(无线标记语言)技术开发&#xff0c;实现了移动设备上的信息查询与后端信息发布功能。系统采用B/S架构&#xff0c;服务器端使用Java Servlet处理请求&#xff0c;数据库采用MySQL存储信息&#xff0…...