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的更多相关文章

  1. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  2. C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用

    一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...

  3. stl::find,find_if,find_if_not

    //满足特定条件下的实现,回调函数template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, ...

  4. find_if函数与partition函数的转换

    编写程序,求大于等于一个给定长度的单词有多少.我们还会修改输出,使程序只打印大于等于给定长度的单词. 使用find_if实现的代码如下: #include<algorithm> #incl ...

  5. STL中的find_if函数

      上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...

  6. STL 查找vector容器中的指定对象:find()与find_if()算法

    1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...

  7. c++ stl algorithm: std::find, std::find_if

    std::find: 查找容器元素, find仅仅能查找容器元素为<基本数据类型> [cpp] view plaincopy #include <iostream> #incl ...

  8. STL --> find()和find_if()

    find()和find_if() 一.find()函数 find(first, end, value); // 返回区间[first,end)中第一个值等于value的元素的位置.如果没有找到匹配元素 ...

  9. find和find_if,value_type

    find算法:返回 [first,end)中第一个值等于value元素的位置 线性复杂度:最多比较次数:元素的总个数 find函数的最后一个参数,必须是string,float,char,double ...

  10. C++中find_if

    总结:find_if针对查找的对象中包含指针需要进行比较 find则更偏向于普通的数值或者字符比较 两者都可以应用于自定义的类,只需在类中重载==运载符 函数调用符()说白了其实就是代替函数指针,调用 ...

随机推荐

  1. Access to XMLHttpRequest at 'XXX' from origin 'XX' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present o AJAX跨域请求解决方法

    今天出现了一个问题找了好久先看代码: 这可能是个BUG吧插入代码: dataType: 'jsonp', crossDomain: true, 最终:

  2. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  3. CentOS7安装tomcat9

    1.去官网下载tomcat9的tar.gz安装包 2.移到centos7中并解压 解压命令: tar -xzvf tomcat9.tar.gz 3.打开文件 /etc 目录下的 profile 文件: ...

  4. Flex布局的学习经验

    做为css布局的又一种新方式,Flex拥有极强的使用效果,相比原来的float,position对元素样式的操作更加简洁,本文是我的一点学习经验和心得吧,如有错误以及不足之处,请多多指点. 好进入正题 ...

  5. mac 安装 mysql.tar.gz

    解压目录到 MySQL 默认安装路径 /usr/local/mysql 下, /usr/local路径不存在时, 先 sudo mkdir /usr/local 创建. # 移动解压后的二进制包到安装 ...

  6. flutter 异步async、await和Future的使用技巧

    由于前面的HTTP请求用到了异步操作,不少小伙伴都被这个问题折了下腰,今天总结分享下实战成果.Dart是一个单线程的语言,遇到有延迟的运算(比如IO操作.延时执行)时,线程中按顺序执行的运算就会阻塞, ...

  7. JSTL、JSTL核心标签库——流程处理标签

    JSTL环境 JSTL是另一个标准规范,并非在JSP的规范中,所以必须另外下载JSTL实现. 要使用JSTL标签库,必须在JSP网页上使用taglib指示元素定义前置名称与uri参考.例如,引入JST ...

  8. 1.print()与input()

    hello world必备->print函数 print(): 作用: 打印函数,打印数据到屏幕中 参数列表: print(value, ..., sep=' ', end='\n', file ...

  9. 关于kernel-devel、kernel-header和kernel src的区别

    A kernel-header package would contain 'header files' needed by some applications which would be buil ...

  10. LeetCode题解之Merge k Sorted Lists 解法二

    1.题目描述 2.分析 利用 vector 存储指针,同时合并k个链表. 3.代码 ListNode* mergeKLists(vector<ListNode*>& lists) ...