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

news2024/10/6 9:22:57

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::tolower


void 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  检查谓词是否对范围中所有、任一或无元素为 true
	std::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 example
	std::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 example
	std::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 elements
	int 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 example
	std::vector<int> myvector3;
	for (int i = 1; i < 10; i++) myvector3.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9

	int 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 50

	int myints4[] = { 10,20,80,320,1024 };                //   myints4: 10 20 80 320 1024

	std::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';
	else
		std::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';
	else
		std::cout << "Element not found in myvector5\n";

	// find_if example
	std::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 example
	std::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 example
	int 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 example
	int 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 example
	int 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 example
	std::vector<int> haystack11;
	// set some values:        haystack11: 10 20 30 40 50 60 70 80 90
	for (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';
	else
		std::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';
	else
		std::cout << "needle21 not found\n";

	// search_n example
	int 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';
	else
		std::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';
	else
		std::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

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

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

相关文章

上位机图像处理和嵌入式模块部署(mcu项目1:假设用51单片机实现)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 到目前位置&#xff0c;我们借助于qt和apm32 mcu芯片&#xff0c;实现了大多数功能。大家如果回过头来看&#xff0c;其实这些功能并不复杂。从固件…

Java(七)——多态

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 ⚡开源项目&#xff1a; rich-vue3 &#xff08;基于 Vue3 TS Pinia Element Plus Spring全家桶 MySQL&#xff09; &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1…

Arcgis Api 三维聚合支持最新版API

Arcgis Api 三维聚合支持最新版API 最近有同学问我Arcgis api 三维聚合&#xff0c;官方还不支持三维聚合API&#xff0c;二维可以。所以依旧是通过GraphicLayers 类来实现&#xff0c;可支持最新Arcgis Api版本 效果图&#xff1a;

【APK】SDKManager运行后闪退

本地JDK已安装&#xff0c;且配置了环境变量&#xff0c;未安装 android studiio 问题描述&#xff1a;右键以管理员身份运行 SDKManager&#xff0c;终端窗口闪退 问题原因&#xff1a;未找到正确的Java路径 解决办法&#xff1a; 1.修改tools目录下的 android.bat 文件&am…

0301STM32GPIO外设输出

STM32GPIO外设输出 STM32内部的GPIO外设GPIO简介基本结构GPIO位结构输入部分&#xff1a;输出部分&#xff1a; GPIO八种工作模式浮空/上拉/下拉输入模拟输入开漏/推挽输出复用开漏/推挽输出 手册寄存器描述GPIO功能描述外设的GPIO配置GPIO寄存器描述端口输入数据寄存器端口输出…

Kafka第四篇——生产数据总体概括,源码解析分区策略,数据收集器,Sender发送线程,key值

目录 流程图以及总体概述 拦截器 分区器以及分区计算策略 为啥进行分区计算&#xff1f; producer生产者怎么知道有哪些分区&#xff1f; 分区计算 如何自定义实现分区器&#xff1f; 想说的在图里啦&#xff01;宝宝&#xff01;&#x1f4a1; ​编辑 如果key值忘记传递了呢&a…

Vue移动端地图App:van-uploader导致的卡顿问题

问题描述 基于Vue3+Vant IU 4开发的移动端地图App,在进行地图点位上报、上报记录查看过程中,出现App卡顿、甚至闪退的问题,进行问题定位之后,发现是van-uploader组件导致的问题。 van-uploader文件上传组件 van-uploader组件用于将本地的图片或文件上传至服务器,并在上传…

园区、社区、乡村的智能管理

智慧园区、社区、乡村管理系统是现代信息技术在城市化进程中的重要应用,它们通过集成多种技术手段,实现对园区、社区、乡村的全面、高效、智能化管理。以下是对这三种管理系统的详细阐述: 一、智慧园区管理系统 1. 定义与目的 智慧园区管理系统是运用物联网、云计算、大数…

Canal架构以及使用规范

Canal架构以及使用规范 一、Canal的作用 相关文档&#xff1a;GitHub - alibaba/canal: 阿里巴巴 MySQL binlog 增量订阅&消费组件 MySQL主备复制原理 MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事件binary log events&#xff0c;可…

【做一道算一道】和为 K 的子数组

给你一个整数数组 nums 和一个整数 k &#xff0c;请你统计并返回 该数组中和为 k 的子数组的个数 。 子数组是数组中元素的连续非空序列。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,1], k 2 输出&#xff1a;2 示例 2&#xff1a; 输入&#xff1a;nums [1,2,3],…

分布式整合

一、分布式架构介绍 什么是分布式系统 分布式系统指一个硬件或软件组件分布在不同的网络计算机上&#xff0c;彼此之间仅仅通过消息传递进行通信和协调的系统。 通俗的理解&#xff0c;分布式系统就是一个业务拆分成多个子业务&#xff0c;分布在不同的服务器节点&#xff0…

【数据结构与算法】快速排序霍尔版

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a;《数据结构与算法》 期待您的关注 ​

找不到x3daudio1_7.dll怎么修复?一招搞定x3daudio1_7.dll丢失问题

当你的电脑突然弹出提示&#xff0c;“找不到x3daudio1_7.dll”&#xff0c;这时候你就需要警惕了。这往往意味着你的电脑中的程序出现了问题&#xff0c;你可能会发现自己无法打开程序&#xff0c;或者即便打开了程序也无法正常使用。因此&#xff0c;接下来我们要一起学习一下…

【简单介绍下Memcached】

&#x1f308;个人主页: 程序员不想敲代码啊 &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共…

LabVIEW从测试曲线中提取特征值

在LabVIEW中开发用于从测试曲线中提取特征值的功能时&#xff0c;可以考虑以下几点&#xff1a; 数据采集与处理&#xff1a; 确保你能够有效地采集和处理测试曲线数据。这可能涉及使用DAQ模块或其他数据采集设备来获取曲线数据&#xff0c;并在LabVIEW中进行处理和分析。 特…

Wormhole Filters: Caching Your Hash on Persistent Memory——泛读笔记

EuroSys 2024 Paper 论文阅读笔记整理 问题 近似成员关系查询&#xff08;AMQ&#xff09;数据结构可以高效地近似确定元素是否在集合中&#xff0c;例如Bloom滤波器[10]、cuckoo滤波器[23]、quotient滤波器[8]及其变体。但AMQ数据结构的内存消耗随着数据规模的增长而快速增长…

Kubernetes集群性能测试之kubemark集群搭建

Kubernetes集群性能测试之kubemark集群搭建 Kubemark是K8s官方提供的一个对K8s集群进行性能测试的工具。它可以模拟出一个K8s cluster&#xff08;Kubemark cluster&#xff09;&#xff0c;不受资源限制&#xff0c;从而能够测试的集群规模比真实集群大的多。这个cluster中ma…

针对tcp不出网打——HTTP隧道代理(以CFS演示)

目录 上传工具到攻击机 使用说明 生成后门文件 由于电脑短路无法拖动文件&#xff0c;我就wget发送到目标主机tunnel.php文件​ 成功上传​ 可以访问上传的文件 启动代理监听 成功带出 后台私信获取弹药库工具reGeorg 上传工具到攻击机 使用说明 生成后门文件 pyt…

Android OpenGL ES 离屏幕渲染2——获取渲染结果并显示到ImageView控件中,使用最简模型展示

简介&#xff1a; 紧接上文&#xff0c;本文将用一个不包含顶点shader和片元shader的最小模型讲述如何把通过EGL创建的OpenGL ES环境渲染后的结果进行提取&#xff0c;单纯输出一片铺满视口的红色的像素。 EGL环境创建逻辑&#xff1a; 先看完整代码&#xff1a; package com.c…

异常组成、作用、处理方式(3种)、异常方法、自定义异常

目录 异常的组成&#xff1a;运行异常与编译异常 两者区别&#xff1a;编译异常用来提醒程序员&#xff0c;运行异常大部分是由于参数传递错误导致 异常作用&#xff1a; 作用1&#xff1a;就是平时的报错&#xff0c;方便我们找到报错的来源 作用2&#xff1a;在方法内部…