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::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.mfbz.cn/a/781443.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Vue88-Vuex中的mapActions、mapMutations

一、mapMutations的调用 此时结果不对&#xff0c;因为&#xff1a;若是点击事件不传值&#xff0c;默认传的是event&#xff01;&#xff0c;所以&#xff0c;修改如下&#xff1a; 解决方式1&#xff1a; 解决方式2&#xff1a; 不推荐&#xff0c;写法麻烦&#xff01; 1-…

排序算法简述(第八jiang)

目录 排序 选择排序 O(n2) 不稳定&#xff1a;48429 归并排序 O(n log n) 稳定 插入排序 O(n2) 堆排序 O(n log n) 希尔排序 O(n log2 n) 图书馆排序 O(n log n) 冒泡排序 O(n2) 优化&#xff1a; 基数排序 O(n k) 快速排序 O(n log n)【分治】 不稳定 桶排序 O(n…

【图解大数据技术】Flume、Kafka、Sqoop

【图解大数据技术】Flume、Kafka、Sqoop FlumeFlume简介Flume的应用场景 KafkaKafka简介Kafka架构Flume与Kafka集成 SqoopSqoop简介Sqoop原理sqoop搭配任务调度器实现定时数据同步 Flume Flume简介 Flume是一个数据采集工具&#xff0c;多用于大数据技术架构下的日志采集。 …

设计模式之模版方法

模版方法介绍 模版方法&#xff08;Template Method&#xff09;模式是一种行为型设计模式&#xff0c;它定义了一个操作&#xff08;模板方法&#xff09;的基本组合与控制流程&#xff0c;将一些步骤&#xff08;抽象方法&#xff09;推迟到子类中&#xff0c;使得子类可以在…

C语言下的文件详解

主要内容 文件概述文件指针文件的打开与关闭文件的读写 文件 把输入和输出的数据以文件的形式保存在计算机的外存储器上&#xff0c;可以确保数据能随时使用&#xff0c;避免反复输入和读取数据 文件概述 文件是指一组相关数据的有序集合 文件是存储数据的基本单位&#…

# mysql 中文乱码问题分析

mysql 中文乱码问题分析 一、问题分析&#xff1a; MySQL 中文乱码通常是因为字符集设置不正确导致的。MySQL 有多种字符集&#xff0c;如 latin1、utf8、utf8mb4 等&#xff0c;如果在创建数据库、数据表或者字段时没有指定正确的字符集&#xff0c;或者在插入数据时使用了与…

关于Java异常机制及finally关键字的详解

异常机制(Exception) 软件程序在运行过程中&#xff0c;非常可能遇到异常问题。常见的异常&#xff1a; 1、用户输入错误 2、设备错误 3、硬件问题&#xff0c;例如打印机关掉、服务器问题 4、物理限制&#xff1a;磁盘满了 Java是采用面向对象的方式来处理异常的。 处理过程…

哈希表——C语言

哈希表&#xff08;Hash Table&#xff09;是一种高效的数据结构&#xff0c;能够在平均情况下实现常数时间的查找、插入和删除操作。 哈希表的核心是哈希函数&#xff0c;哈希函数是一个将输入数据&#xff08;通常称为“键”或“key”&#xff09;转换为固定长度的整数的函数…

使用vue3-treeselect问题

1.当vue3-treeselect是单选时&#xff0c;使用watch监听绑定value&#xff0c;无法监听到值清空 对照后将:value改为v-model&#xff0c;如图 2.使用vue3-treeselect全部清空按钮如何置空select的值&#xff0c;使用watch监听 多选&#xff1a;pageInfo.officeName(val) {// …

【Linux进阶】文件系统6——理解文件操作

目录 1.文件的读取 1.1.目录 1.2.文件 1.3.目录树读取 1.4.文件系统大小与磁盘读取性能 2.增添文件 2.1.数据的不一致&#xff08;Inconsistent&#xff09;状态 2.2.日志式文件系统&#xff08;Journaling filesystem&#xff09; 3.Linux文件系统的运行 4、文件的删…

Java--方法重写

1.方法的重写首先需要有继承关系&#xff0c;且为子类重写父类的方法 2.方法名必须相同 3.参数列表必须相同 4.修饰符的范围可以扩大但不能缩小&#xff0c;public>protected>default>private,即父类的属性可以从private改为public&#xff0c;但不能反过来 5.抛出…

python爬虫入门(四)之Beautiful Soup库

一、什么是Beautiful Soup库 1、Beautiful Soup库是用来做HTML解析的库 Beautiful Soup把看起来复杂的HTML内容&#xff0c;解析成树状结构&#xff0c;让搜索和修改HTML结构变得更容易 2、第三方库&#xff0c;先安装 终端输入pip install bs4 from bs4 import Beautiful…

Cyber Weekly #14:WAIC 2024

赛博新闻 1、WAIC2024开幕&#xff1a;一半机器人&#xff0c;一半大模型 7月4日&#xff0c;AI界春晚——2024世界人工智能大会&#xff08;WAIC 2024&#xff09;在上海开幕&#xff0c;大会展示了500家企业的1500项展品&#xff0c;突出了机器人和大模型技术。国产机器人和…

【排序算法】—— 快速排序

快速排序的原理是交换排序&#xff0c;其中qsort函数用的排序原理就是快速排序&#xff0c;它是一种效率较高的不稳定函数&#xff0c;时间复杂度为O(N*longN)&#xff0c;接下来就来学习一下快速排序。 一、快速排序思路 1.整体思路 以升序排序为例&#xff1a; (1)、首先随…

学生管理系统(通过顺序表,获取连续堆区空间实现)

将学生的信息&#xff0c;以顺序表的方式存储&#xff08;堆区&#xff09;&#xff0c;并且实现封装函数 &#xff1a; 1】顺序表的创建&#xff0c; 2】判满、 3】判空、 4】往顺序表里增加学生信息、 5】遍历学生信息 6】任意位置插入学生信息 7】任意位置删除学生信…

【大模型LLM面试合集】大语言模型基础_llm概念

1.llm概念 1.目前 主流的开源模型体系 有哪些&#xff1f; 目前主流的开源LLM&#xff08;语言模型&#xff09;模型体系包括以下几个&#xff1a; GPT&#xff08;Generative Pre-trained Transformer&#xff09;系列&#xff1a;由OpenAI发布的一系列基于Transformer架构…

对话大模型Prompt是否需要礼貌点?

大模型相关目录 大模型&#xff0c;包括部署微调prompt/Agent应用开发、知识库增强、数据库增强、知识图谱增强、自然语言处理、多模态等大模型应用开发内容 从0起步&#xff0c;扬帆起航。 基于Dify的QA数据集构建&#xff08;附代码&#xff09;Qwen-2-7B和GLM-4-9B&#x…

Android OpenGL ES 离屏幕渲染1——EGL环境的创建,以及基础概念的理解

创建EGL上下文、配置EGL环境、创建EGL DISPLAY 什么是EGL&#xff1a; 由于OpenGL ES并不负责窗口管理以及上下文管理&#xff0c;该职责由各个平台自行完成&#xff1b;在Android平台下OpenGL ES的上下文环境是依赖EGL的API进行搭建的。 对于EGL这个框架&#xff0c;谷歌已经提…

WAWA鱼曲折的大学四年回忆录

声明&#xff1a;本文内容纯属个人主观臆断&#xff0c;如与事实不符&#xff0c;请参考事实 前言&#xff1a; 早想写一下大学四年的总结了&#xff0c;但总是感觉无从下手&#xff0c;不知道从哪里开始写&#xff0c;通过这篇文章主要想做一个记录&#xff0c;并从现在的认…

那些年背过的面试题——MySQL篇

本文是技术人面试系列 MySQL 篇&#xff0c;面试中关于 MySQL 都需要了解哪些基础&#xff1f;一文带你详细了解&#xff0c;欢迎收藏&#xff01; WhyMysql&#xff1f; NoSQL 数据库四大家族 列存储 Hbase K-V 存储 Redis 图像存储 Neo4j 文档存储 MongoDB 云存储 OSS …