find和find_if
find函数
是在一个迭代器范围内查找特定元素得函数,可将将他用于任意容器类型得元素。这个函数返回的是所找元素得引用,如果没有找到元素就会返回这个容器得尾迭代器。
#include <iostream>
#include <vector>
#include <time.h> int main()
{
std::vector<int> vec;
int inputTofind = ;
srand(time(NULL)); for (int i = ; i < ; ++i)
{
int temp = rand() % ;
vec.push_back(temp);
std::cout << temp << " ";
} std::cout << std::endl;
std::cout << "you want to look for num : ";
std::cin >> inputTofind; auto endIt = std::end(vec);
auto result = find(std::begin(vec), endIt, inputTofind); if (result != endIt)
{
std::cout << "find it " << *result << std::endl;
}
else
{
std::cout << "No find!" << std::endl;
} return ;
}
结果:
21 7 55 81 67 44 18 10 82 87
you want to look for num : 81
find it 81
请按任意键继续. . .
find_if函数
与find()函数一样,区别就是它可以接受接收一个谓词函数,而不是简单的匹配元素,谓词函数返回的是true或则false;如果是返回了true则find_if函数返回了查找元素的迭代器;如果一直找不到制定的元素就会返回尾迭代器;
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h> bool perfectScord(int scord)
{
return scord >= ;
} int main()
{
std::vector<int> vec;
int inputTofind = ;
srand(time(NULL)); for (int i = ; i < ; ++i)
{
int temp = rand() % ;
vec.push_back(temp);
std::cout << temp << " ";
} std::cout << std::endl;
// std::cout << "you want to look for num : ";
// std::cin >> inputTofind; auto endIt = std::cend(vec); auto result = find_if(std::cbegin(vec), endIt, perfectScord); if (result != endIt)
{
std::cout << "find it " << *result << std::endl;
}
else
{
std::cout << "No find!" << std::endl;
} return ;
}
结果是:
86 82 59 13 84 91 42 18 92 67
find it 86
请按任意键继续. . .
注意的是find_if函数在头文件#include <algorithm>
其实这里的谓词函数也能换成lambda函数:
auto result = find_if(std::cbegin(vec), endIt, [](int i) {return i > ; });
find和find_if的更多相关文章
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...
- stl::find,find_if,find_if_not
//满足特定条件下的实现,回调函数template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, ...
- find_if函数与partition函数的转换
编写程序,求大于等于一个给定长度的单词有多少.我们还会修改输出,使程序只打印大于等于给定长度的单词. 使用find_if实现的代码如下: #include<algorithm> #incl ...
- STL中的find_if函数
上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...
- STL 查找vector容器中的指定对象:find()与find_if()算法
1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...
- c++ stl algorithm: std::find, std::find_if
std::find: 查找容器元素, find仅仅能查找容器元素为<基本数据类型> [cpp] view plaincopy #include <iostream> #incl ...
- STL --> find()和find_if()
find()和find_if() 一.find()函数 find(first, end, value); // 返回区间[first,end)中第一个值等于value的元素的位置.如果没有找到匹配元素 ...
- find和find_if,value_type
find算法:返回 [first,end)中第一个值等于value元素的位置 线性复杂度:最多比较次数:元素的总个数 find函数的最后一个参数,必须是string,float,char,double ...
- C++中find_if
总结:find_if针对查找的对象中包含指针需要进行比较 find则更偏向于普通的数值或者字符比较 两者都可以应用于自定义的类,只需在类中重载==运载符 函数调用符()说白了其实就是代替函数指针,调用 ...
随机推荐
- Oracle总结之plsql编程(基础七)
紧接基础六,对oracle角色和权限的管理之后,在接下来的几次总结中来就最近工作中用过的plsql编程方面的知识进行总结,和大家分享! 原创作品,转自请注明出处:https://www.cnblogs ...
- How to write a professional email?
Advantages and Disadvantages of Email communication compared with Face-To-Face communication: Emai ...
- SSL certificate problem: unable to get local issuer certificate 的解决方法
今天在进行微信开发获取微信Access_Token时,使用到了php的curl库, 在敲完代码后获取token失败,经过各种排查错误,到了下面这一步 SSL certificate problem: ...
- jQuery同步Ajax带来的UI线程阻塞问题
一.需求 在调ajax接口的时候因为有时间延迟,想要做一个loading加载的效果,等数据返回时再把loading的效果去掉. 所以我在调ajax的代码块前面加了显示loading效果的代码,ajax ...
- DevExpress.XtraCharts曲线上的点所对应的坐标值
private void chartControl_ObjectSelected(object sender, HotTrackEventArgs e) { e.Cancel = false; XYD ...
- ArcGIS 中取出面上最大的Z值的坐标点
def MaxZ(shape): line = shape.getPart(0) pnt = line.next() maxValue = float("-in ...
- 帆软发布大数据直连引擎FineDirect,对焦大数据BI
摘要:近日,帆软官方正式发布大数据直连引擎FineDirect模块.通过该模块,企业在应用FineBI原有功能的基础上,可直接对接现有数据源,无论是传统的关系型数据库,还是Hadoop生态圈.Mpp构 ...
- 在JavaScript文件中用jQuery方法实现日期时间选择功能
JavaScript Document $(document).ready(function(e) { 在文本框里面显示当前日期 var date = new Date(); var nian = d ...
- JSTL核心标签库——错误处理标签
<c:catch>标签 Demo: 错误发生时,不转发至其他网页来显示错误信息,而在当前网页捕捉异常,并显示相关信息. <%@page contentType="text/ ...
- url override and HttpSession implements session for form
url 重写结合HttpSession实现会话管理之 form 提交 package com.test; import javax.servlet.ServletException; import j ...