STL之search()算法

从零开始学习CANoe(十六)—— Graphics Graphics是一种以时间轴为横坐标,符号值为纵坐标的显示分析方法,相对于Trace来说,更直观的观察出信号的跳变 阅读详情

我们之前介绍的find()算法以及find_if()算法都帮助我们查找判断某一个value是否被包含在序列中,并返回它第一次出现时所处的位置,假如我想找到某一段满足条件的子区间,应该怎么做呢?C++标准库又为我们提供了哪些算法呢?

(下面程序中要用到的algostuff.h)

search_n()        搜寻具有某特性的第一段“n个连续元素”

InputIterator 
search_n(InputItertor beg,InputIterator end,
        Size count,const T& value)

InputIterator
search_n(InputIterator beg,InputIterator end,
        Size count,const T&value,BinaryPredicate op)
  • 第一形式返回区间[beg,end)中第一组”连续count个元素值全等于 value"的元素位置
  • 第二形式返回区间[beg,end)中第一组“连续count个元素造成以下一元判断式结果为true”的元素位置: op(elem,value)
  • 如果没有找到匹配元素,两种形式都返回end
  • op在函数调用过程中不应该改变自身状态
  • op不应变动被传进去的参数

下面这个例子搜寻连续4个“数值大于等于3”的元素:

#include"algostuff.h"
using namespace std;
int main()
{
	deque<int> col;
	INSERT_ELEMENTS(col, 1, 9);
	PRINT_ELEMENTS(col);	//1 2 3 4 5 6 7 8 9

	//find four consecutive elements with value 3
	deque<int>::iterator pos;
	pos = search_n(col.begin(), col.end(), 4, 3);

	//print result
	if (pos != col.end())
	{
		cout << "four consecutive element with value 3 "
			<< "start with " << distance(col.begin(), pos) + 1 << ".element" << endl;
	}
	else
	{
		cout << "no four consecutive elements with value 3 not found" << endl;
	}
	//no four consecutive elements with value 3 not found
	

	//find four consecutive elements with value greater than 3
	pos = search_n(col.begin(), col.end(), 4, 3, greater<int>());
	//print result
	if (pos != col.end())
	{
		cout << "four consecutive elements with value > 3 "
			<< "start with " << distance(col.begin(), pos) + 1
			<< ". element" << endl;
	}
	else
	{
		cout << "no four consecutive elements with value > 3 found " << endl;
	}

}

 运算结果如下:

 

search()        搜寻某个子区间第一次出现位置

ForwardIterator1
search(ForwardIterator1 beg,ForwardIterator1 end,
        ForwardIterator2 searchBeg,ForwardIterator2 searchEnd)

ForwardIterator1
search(ForwardIterator1 beg,ForwardIterator1 end,
        ForwardIterator2 searchBeg,ForwardIterator2 searchEnd,
        BinaryPredicate op)
  • 两种形式都返回区间[beg,end)内“和区间[searchBeg,searchEnd)完全吻合”的第一个子区间的第一个元素位置
  • 第一形式中,子区间的元素必须完全等于[searchBeg,searchEnd)中的元素
  • 第二形式中,子区间的元素和[searchBeg,searchEnd)里面的对应元素必须造成以下二元判断式的结果为true:       op(elem,searchElem)
  • 如果没有找到符合条件的子区间,两种形式都返回end
  • op在函数调用过程中不应改变自身状态
  • op不应变动传入的参数

下面这个例子展示如何在另一个序列中搜寻一个子序列

#include"algostuff.h"
using namespace std;
int main()
{
	deque<int> col;
	list<int> subcol;

	INSERT_ELEMENTS(col, 1, 7);
	INSERT_ELEMENTS(col, 1, 7);
	PRINT_ELEMENTS(col, "col: ");	//col: 1 2 3 4 5 6 7 1 2 3 4 5 6 7

	INSERT_ELEMENTS(subcol, 3, 6);
	PRINT_ELEMENTS(subcol, "subcol: ");	//subcol: 3 4 5 6

	//search first occurrence of subcol in col
	deque<int>::iterator pos;
	pos = search(col.begin(), col.end(), subcol.begin(), subcol.end());
	//loop while subcol found as subrange of col
	while (pos != col.end())
	{
		//print position of first element
		cout << "subcol found starting with element "
			<< distance(col.begin(), pos) + 1
			<< endl;
		//search next occurrence of subcol
		++pos;
		pos = search(pos, col.end(), subcol.begin(), subcol.end());
	}
}

 

下面这个程序展示如何利用search()算法的第二形式 ,以更复杂的准则来搜寻某个子序列,这里寻找的是“偶数、奇数、偶数”排列而成的子序列区间

#include"algostuff.h"
using namespace std;

//checks whether an element is even or odd
bool checkEven(int elem, bool even)
{
	if (even)
	{
		return elem % 2 == 0;
	}
	else
	{
		return elem % 2 == 1;
	}
}

int main()
{
	vector<int> col;
	INSERT_ELEMENTS(col, 1, 9);
	PRINT_ELEMENTS(col, "col:");	//col:1 2 3 4 5 6 7 8 9

	bool checkEvenArgs[3] = { true,false,true };

	//search first subrange in col
	vector<int>::iterator pos;
	pos = search(col.begin(), col.end(),    //range
		checkEvenArgs, checkEvenArgs + 3,   //subrange values
		checkEven);							//subrange criterion

	//loop while surange found
	while (pos != col.end())
	{
		//print position of first element
		cout << "subrange found starting with element "
			<< distance(col.begin(), pos) + 1 << endl;
		//search next subrange in col
		pos = search(++pos, col.end(),			//range
			checkEvenArgs, checkEvenArgs + 3,	//subrange values
			checkEven);							//criterion
	}


}

运行结果如下: 

(以上表示共找出三个满足“偶、奇、偶”条件的子序列,分别从元素2,4,6开始) 

find_end()        搜寻某个子区间最后一次出现位置

ForwardIterator 
find_end(ForwardIterator beg,ForwardIterator end,
        ForwardIterator searchBeg,ForwardIterator searchEnd)

ForwardIterator
find_end(ForwardIterator beg,ForwardIterator end,
        ForwardIterator searchBeg,ForwardIterator searchEnd,
        BinaryPredicate op)
  • 两种形式都返回区间[beg,end)之中“和区间[searchBeg,searchEnd)完全吻合”的最后一个子区间内的第一个元素位置
  • 第一形式中,子区间的元素必须完全等于[searchBeg,searchEnd)的元素
  • 第二形式中,子区间的元素和[searchBeg,searchEnd)的对应元素必须造成以下二元判断式的结果为true:           op(elem,searchElem)
  • 如果没有找到符合条件的子区间,两种形式都返回end
  • op在函数调用过程中不应该改变自身状态
  • op不应改动传入的参数

下面这个例子展示如何在一个序列中搜寻“与某序列相等”的最后一个子序列

#include"algostuff.h"
using namespace std;
int main()
{
	deque<int> col;
	list<int> subcol;
	INSERT_ELEMENTS(col, 1, 7);
	INSERT_ELEMENTS(col, 1, 7);

	INSERT_ELEMENTS(subcol, 3, 6);

	PRINT_ELEMENTS(col, "col: ");	//col: 1 2 3 4 5 6 7 1 2 3 4 5 6 7
	PRINT_ELEMENTS(subcol, "subcol: ");	//subcol: 3 4 5 6

	//search last occurrence of subcol in col
	deque<int>::iterator pos;
	pos = find_end(col.begin(), col.end(), //range
		subcol.begin(), subcol.end());	//subrange

	//loop while subcol found as subrange of col
	deque<int>::iterator end(col.end());
	while (pos != end)
	{
		//print position of first element
		cout << "subcol found starting with element "
			<< distance(col.begin(), pos) + 1 << endl;

		//search nect occurrence of subcol
		end = pos;
		pos = find_end(col.begin(), end, subcol.begin(), subcol.end());
	}
}

 

find_first_of()        搜寻某些元素的第一次出现地点

ForwardIterator 
find_first_of(ForwardIterator1 Beg,ForwardIterator1 end,
                ForwardIterator2 searchBeg,ForwardIterator2 searchEnd)

ForwardIterator 
find_first_of(ForwardIterator1 Beg,ForwardIterator1 end,
                ForwardIterator2 searchBeg,ForwardIterator2 searchEnd,
                BinaryPredicate op)
  • 第一形式返回第一个“既在[beg,end)中出现,也在区间[searchBeg,searchEnd)中出现”的元素的位置
  • 第二形式返回区间[beg,end)中第一个这样的元素:它和区间[searchBeg,searchEnd)内每一个元素进行以下动作的结果都是true:        op(elem,searchElem)
  • 如果没有找到吻合元素,两种形式都返回end
  • op在调用过程中不应改变自身状态
  • op不应改动传入的参数

下面展示find_first_of()的用法

#include"algostuff.h"
using namespace std;
int main()
{
	vector<int> col;
	list<int> searchcol;

	INSERT_ELEMENTS(col, 1, 11);
	INSERT_ELEMENTS(searchcol, 3, 5);

	PRINT_ELEMENTS(col, "col: ");	//col: 1 2 3 4 5 6 7 8 9 10 11
	PRINT_ELEMENTS(searchcol, "searchcol: ");	//searchcol: 3 4 5

	//search first occurrence of an element of searchcol in col
	vector<int>::iterator pos;
	pos = find_first_of(col.begin(), col.end(),	//range
		searchcol.begin(), searchcol.end());	//search range

	cout << "first element of searchcol in col is element "
		<< distance(col.begin(), pos) + 1 << endl;

	//search last occurrence of an element of searchcol in col
	vector<int>::reverse_iterator rpos;
	rpos = find_first_of(col.rbegin(), col.rend(), searchcol.begin(), searchcol.end());
	cout << "last element of searchcol in col is element "
		<< distance(col.begin(), rpos.base()) << endl;

}

 

adjacent _find()        搜寻两个连续相等的元素

InputIterator
adjacent_find(InputIterator beg,InputIterator end)

InputIterator
adjacent_find(InputIterator beg,InputIterator end, BinaryPredicate op)
  • 第一形式返回区间[beg,end)中第一对“连续两个相等元素”之中的第一元素位置
  • 第二形式返回区间[beg,end)中第一对“连续两个元素均使以下二元判断式的结果为true”的其中第一元素位置:       op(elem,nectElem)
  • 如果没有找到吻合元素,两者都返回end
  • op在函数调用过程中不应该改变自身状态
  • op不应改动传入的参数

下面展示adjacent_find()算法的用法

#include"algostuff.h"
using namespace std;

//return whether the second object has double the value of the first
bool doubled(int elem1, int elem2)
{
	return elem1 * 2 == elem2;
}
int main()
{
	vector<int> col;
	col.push_back(1);
	col.push_back(3);
	col.push_back(2);
	col.push_back(4);
	col.push_back(5);
	col.push_back(5);
	col.push_back(0);

	PRINT_ELEMENTS(col, "col: ");
	// search first two elements with equal value
	vector<int>::iterator pos;
	pos = adjacent_find(col.begin(), col.end());
	if (pos != col.end())
	{
		cout << "first two elements with equal value have position "
			<< distance(col.begin(), pos) + 1 << endl;
	}
	//search first two elements for which the second has double the value of the first
	pos = adjacent_find(col.begin(), col.end(), doubled);
	if (pos != col.end())
	{
		cout << "first two elements with second value twice the first have pos. "
			<< distance(col.begin(), pos) + 1 << endl;
	}
}

search_n()搜寻具有某特性的第一段“n个连续元素”
search()搜寻某个子区间第一次出现位置
find_end()搜寻某个子区间最后一次出现的位置
find_first_of()搜寻某些元素第一次出现地点
adjacent_find()搜寻两个连续且相等的元素

Parasoft C++Test软件单元测试_操作指南 本文主要为使用C++TEST进行单元测试的流程性操作讲解,后续文章将以专题形式进行详细讲解,以及用一些被测函数作为示例进行实例讲解。 阅读详情

相关推荐

VL53L0X测距不准?手把手教你做RefSPAD校准与环境补偿(附Arduino/STM32代码)

本文详细解析了VL53L0X激光测距传感器精度不足的原因,并提供了RefSPAD校准与环境补偿的完整解决方案。通过Arduino和STM32代码示例,手把手教你实现毫米级测距稳定性,适用于机器人避障、工业检测等场景。

weixin_28744423的博客 257

STL技术中search()算法

STL通用算法search()用来搜索一个容器,但它用来搜索的是一个元素串,不像find()和find_if()只搜索单个元素。 #include #include #include #include using namespace std; void main ()  {  vector TargetCharacters;               //定义两个

huaxiazhichunshi的专栏 745

SpringBoot Redis入门(一)——redis、Lettuce、Redisson使用

Lettuce是SpringBoot自带的Redis客户端,性能比Redis要好, 是线程安全的,支持同步编程,异步编程,响应式编程,自动重新连接,主从模式,集群模块,哨兵模式Redisson 是一个在 Redis 的功能基础上实现的 Java 驻内存数据网格客户端。实现了分布式和可扩展的 Java 数据结构,提供很多分布式相关操作服务,例如分布式锁,分布式集合,可通过 Redis 支持延迟队列。

朗朗的博客 3833

C++:STL的search用法

// STL_search.cpp : 定义控制台应用程序的入口点。 //STL的search用法 #include "stdafx.h" #include <string> #include<list> #include <iostream> #include<algorithm> using namespace std; void printLine(char& StringLine); int _tmain(int argc, _TCHA.

qq_41708281的博客 548

Goldwasser-Micali 公钥加密系统

1、二次剩余问题 对于整数n ,定义Zn∗={a∈Zn,gcd(a,n)=1}Z^*_n=\{ a∈Z_n,gcd(a,n) =1\}Zn∗​={a∈Zn​,gcd(a,n)=1} 。当存在d∈Znd \in Z_nd∈Zn​ ,使得 d2≡a mod nd^2 \equiv a \, mod \,nd2≡amodn ,称 a 为模 n 的二次剩余;否则称 a 为模 n的二次非剩余。判断 a 是否为模 n 的二次剩余的问题称为模n 的二次剩余问题。 \rule[-10pt]{14.3cm}{0.05em}

不染心的博客 4314

在 Windows 系统下使用 VMware 安装 Ubuntu 24.04 LTS 系统

Ubuntu24.04桌面端官方下载:https://cn.ubuntu.com/download/desktop。

u014695938的博客 1万+

C++ search_n()函数用法详解(深入了解,一文学会)

和 search() 一样,search_n() 函数也定义在头文件中,用于在指定区域内查找第一个符合要求的子序列。不同之处在于,前者查找的子序列中可包含多个不同的元素,而后者查找的只能是包含多个相同元素的子序列。本文作者原创,转载请附上文章出处与本文链接。

断点 910

傻白入门芯片设计,SystemVerilog Assertion(SVA)学习(二十一)

对SVA(SystemVerilogt Assertion)有了更深刻的认识,对经典的AveMC形式验证工具进行了介绍

qq_46675545的博客 8472

C++的STL之search()与find_end()

1、search()search(ideq.begin(),ideq.end(), ilist.begin(), ilist.end());2、find_end()find_end(ideq.begin(), ideq.end(),ilist.begin(),ilist.end());也可以用谓词:bool checkEven(int elem, bool even){if(even)return...

jisuanji198509的博客 1088

2019年电赛D题《简易电路特性测试仪》全过程

2019年全国大学生电子设计大赛D题《简易电路特性测试仪》硬件部分及故障分析

zzhao2580的博客 5万+

从零到一:高通Memory QMVS测试环境搭建的避坑指南

本文详细介绍了高通Memory QMVS测试环境搭建的全流程避坑指南,涵盖硬件选择、软件配置、QMVS核心组件安装及测试执行技巧。重点解析了adb工具配置、QPST日志抓取等关键步骤,帮助工程师高效搭建稳定测试环境,避免常见兼容性和稳定性问题。

weixin_29251567的博客 557

STL search搜索算法

在查找序列的子序列方面,search() 算法和 find_end() 算法相似,但它所查找的是第一个匹配项而不是最后一个。 和 find_end() 算法一样,它也有两个版本,第二个版本接受用来比较元素的谓词作为第 5 个参数。可以用 search() 来验证前面使用 find_end() 搜索的结果。 如何改变每次遍历搜索的具体范围是它们的主要不同之处。下面是一个示例: string t...

JIWilliams 1058

LangChain与LangGraph核心区别解析

LangChain与LangGraph是当前大语言模型(LLM)应用开发中两个紧密相关且极具影响力的开源框架。它们源于LangChain AI公司,设计哲学互补,旨在解决不同维度的LLM工程化问题,共同构成了构建复杂智能应用的强大工具集。

weixin_35774598的博客 1926

如何在C++中使用STL中的查找算法binary_search()

【代码】如何在C++中使用STL中的查找算法binary_search()

一个与计算机相关的博客 241

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

C++ STL标准库:算法<algorithm> 查找算法 search()自定义规则

#include <functional> #include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> void FillValue(T& vect, int first, int last) { if (last >= first) { for (int i = first; i &l

超级大洋葱的博客 1071

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

C++ STL标准库:算法<algorithm> 查找算法 search_n()

#include <functional> #include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> void FillValue(T& vect, int first, int last) { if (last >= first) { for (int i = first; i &l

超级大洋葱的博客 1295

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

c++之STL(13) STL 算法 - 查找算法(3)search() find_end()

search() 是从前面找 find_end() 是从后门找 注意:两个算法是一对,第二个算法叫search_end(),但是被命名为find_end(); 两个都是用来在容器中查找,连续的数据在容器中是否存在

taotaoahui 1385

C++ STL标准库:算法<algorithm> 查找位置 binary_search() lower_bound() upper_bound()

#include <iostream> #include <algorithm> #include <list> #include <vector> #include <functional> using namespace std; void main() { int dim[] = { 1,2,3,4,5,6,7,8,9 }; list<int> l1; list<int>::iterator posL, po

超级大洋葱的博客 571

STL常用算法——查找算法

find() 函数会返回一个输入迭代器,当 find() 函数查找到目标元素时,其指向查找到的第一个目标元素位置迭代器;adjacent_find()函数会返回一个迭代器,当查找成功时,该迭代器指向的是连续相等元素的第 1 个元素;和find() 函数不同的是,find() 函数需要明确指定要查找的元素的值,而find_if() 允许自定义条件来查找元素。find_if_not() 函数和 find_if() 函数的功能恰好相反,用于查找第一个不符合查找规则的目标元素。函数原型:该函数有以下两种语法格式。

万里顾一程的博客 975

10道C++ STL高频面试题[11-20](附带完整参考答案)

迭代器是 C++ 标准模板库(STL)中的一个重要概念。简单来说,迭代器就像是一个指针,用于访问和遍历容器中的元素(比如数组、链表、集合等)。迭代器提供了一种统一的方法来访问容器中的元素,而不需要关心容器的具体类型。遍历容器: 通过迭代器可以遍历容器中的所有元素。例如,使用begin()和end()方法获取容器的起始和结束迭代器,然后通过循环来访问每个元素。访问元素: 可以通过迭代器读取或修改容器中的元素。连接算法与容器: STL 中的很多算法(如排序、查找等)都是通过迭代器来操作容器的。

qq_45863980的博客 1451

STL泛型算法--binary_search()

#include #include #include #include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ int ia[] = {29, 23, 20, 22, 17, 15, 26, 51, 19, 12, 35, 40}; sort(&ia[0], &ia

龙宇网的专栏 1164

c++之STL(13) STL 算法 - 查找算法(2)search_n() search_n(b, e, c, v) search_n(b, e, c, v, p)

search_n() 用来查找连续的n个匹配的数值 或者 加谓词 search_n(b, e, c, v) search_n(b, e, c, v, p) 注意:该方法的第二种形式应该是search_n_if(b, e, c, p) #include #include #include // #include // using namespace std; int mai

taotaoahui 730

C++_STL_查找算法

算法概述 算法部分主要由头文件,和组成。 是所有STL头文件中最大的一个,其中常用到的功能范围涉及到比较、交换、查找、遍历操作、复制、修改、反转、排序、合并等等。 体积很小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作。 中则定义了一些模板类,用以声明函数对象。 STL提供了大量实现算法的模版函数,只要我们熟悉了STL之后,许多代码可以被大大的化简,只...

weixin_43916755的博客 439

学习C++:STL算法

STL算法就是像查找、搜索、删除等操作的通用函数,其应用范围很广。要使用STL算法,应用程序必须包含头文件。

不积跬步,无以至千里! 899

C++算法adjacent_find()和binary_search()和sotr()

adjacent_find() 搜索相邻的重复元素 //创建容器 vector<int> CreateVector() { vector<int> VeInt; VeInt.push_back(1); VeInt.push_back(2); VeInt.push_back(2); VeInt.push_back(3)...

焕小谢的博客 391

STL中的find函数

来源链接:https://blog.youkuaiyun.com/scarletty/article/details/7058398 接触STL不多,但每当写程序的时候,会先想到用它,还算个好习惯吧,毕竟自己写的链表之类的没那么好。 如何使用STL进行查找?通用算法find()和find_if()可以做这些。就象for_each(), count(), count_if()一样,这些算法也使用iterato...

李昊的博客 566
上一篇: C++怎样查找序列中是否包含某一个元素?STL之find()算法
下一篇: C++两个区间的比较?——STL之equal()算法和mismatch()算法
yangSHU21
博客等级 码龄4年 303粉丝 96原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值