当前位置: 首页 > 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…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望

文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例&#xff1a;使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例&#xff1a;使用OpenAI GPT-3进…...

在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:

在 HarmonyOS 应用开发中&#xff0c;手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力&#xff0c;既支持点击、长按、拖拽等基础单一手势的精细控制&#xff0c;也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档&#xff0c…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

NLP学习路线图(二十三):长短期记忆网络(LSTM)

在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用

1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

MySQL JOIN 表过多的优化思路

当 MySQL 查询涉及大量表 JOIN 时&#xff0c;性能会显著下降。以下是优化思路和简易实现方法&#xff1a; 一、核心优化思路 减少 JOIN 数量 数据冗余&#xff1a;添加必要的冗余字段&#xff08;如订单表直接存储用户名&#xff09;合并表&#xff1a;将频繁关联的小表合并成…...

免费数学几何作图web平台

光锐软件免费数学工具&#xff0c;maths,数学制图&#xff0c;数学作图&#xff0c;几何作图&#xff0c;几何&#xff0c;AR开发,AR教育,增强现实,软件公司,XR,MR,VR,虚拟仿真,虚拟现实,混合现实,教育科技产品,职业模拟培训,高保真VR场景,结构互动课件,元宇宙http://xaglare.c…...

Python+ZeroMQ实战:智能车辆状态监控与模拟模式自动切换

目录 关键点 技术实现1 技术实现2 摘要&#xff1a; 本文将介绍如何利用Python和ZeroMQ消息队列构建一个智能车辆状态监控系统。系统能够根据时间策略自动切换驾驶模式&#xff08;自动驾驶、人工驾驶、远程驾驶、主动安全&#xff09;&#xff0c;并通过实时消息推送更新车…...

[大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.

ollama官网: 下载 https://ollama.com/ 安装 查看可以使用的模型 https://ollama.com/search 例如 https://ollama.com/library/deepseek-r1/tags # deepseek-r1:7bollama pull deepseek-r1:7b改token数量为409622 16384 ollama命令说明 ollama serve #&#xff1a…...