find算法:返回 [first,end)中第一个值等于value元素的位置

线性复杂度:最多比较次数:元素的总个数

find函数的最后一个参数,必须是string,float,char,double,int等,用自定义类型的数据查找会出错。

要想确切知道在容器中的位置,要用distance(容器.begin(),p)+1,//p是迭代器返回的位置,+1看情况,看下标是从几开始的。数组的下标是从1开始的,

为了更好理解,我们举个例子:

1、数组在内存中申请是,所申请的内存是一段连续的内存地址;

2、例:int[] a=new int[3];申请一段:int 数据类型的数组,a 为变量,数组长度为:[3];

3、这个数组所申请的内存地址是连续的(假设所申请的:第一个内存地址为:1008,第二个为:1009,第三个为:1010);,但我们只知道:一、变量:a,它只拿到第一个内存地址1008;二、它的数组空间为3个;

4、a[0]——把a拿到的内存地址:1008 + 0 = 1008 (指向第一个内存地址);

a[1]——把a拿到的内存地址:1008 + 1 = 1009 (指向第二个内存地址);

a[2]——把a拿到的内存地址:1008 + 2 = 1010 (指向第三个内存地址);

所以:数据下标从 [0] 开始的意义也在于此!(当然,这是理解版的)。

#include "stdafx.h"
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<numeric>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 30, 50, 70, 90, 20, 10 };
int n = sizeof(array) / sizeof(int);
vector<int>b(array, array + n);
vector<int>::iterator p = find(b.begin(), b.end(), 50);
cout << "it has exit,and the possion is " << distance(b.begin(), p) + 1 << endl; if (p != b.end())
{
cout << "it has exit,the place is :" << distance(b.begin(), p)+1 << endl;
}
else
cout << "it not exit" << endl; return 0;
}

find_if:在区间 [first,end)中搜寻使一元判断式pred为true的第一个元素,

重载(),是个仿函数,在运算符()内部定义要查找的条件,仿函数返回类型必须是bool类型,客观反映在find_if函数查找过程中是否有匹配。

value_type:是stl容器中的数据的数据类型,即迭代器所指对象的类别,在使用stl模板时,需要传入迭代器的参数,这个参数的类别就是容器中数据的类别, 那个参数类型的别名就叫value_type。

自定义类:

#include "stdafx.h"
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<numeric>
using namespace std; template<class T>
class intgreat
{
public:
bool operator()(T &num)
{
return num > 50;
} }; int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 30, 50, 70, 90, 20, 10 };
int n = sizeof(array) / sizeof(int);
vector<int>b(array, array + n);
vector<int>::iterator p = find_if(b.begin(), b.end(),intgreat<int>() );
cout << "it has exit,and the possion is " << distance(b.begin(), p) + 1 << endl;
cout <<*p << endl;
if (p != b.end())
{
cout << "it has exit,the place is :" << distance(b.begin(), p)+1 << endl;
}
else
cout << "it not exit" << endl; return 0;

自定义结构体的查找函数

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; struct value_t
{
int a;
int b;
}; class vector_finder
{
public:
vector_finder( const int a, const int b ) :m_v_a(a),m_v_b(b){}
bool operator ()( vector<struct value_t>::value_type &value) //vector<struct value_t>::value_type这部分都是类型说明,,迭代器所指对象的类型,即容器中数据的数据类型
{
return (value.a==m_v_a)&&(value.b = m_v_b);
}
private:
int m_v_a;
int m_v_b;
}; int main()
{
vector<value_t> my_vector;
value_t my_value; my_value.a = 11; my_value.b = 1001;
my_vector.push_back(my_value); my_value.a = 12; my_value.b = 1002;
my_vector.push_back(my_value); my_value.a = 13; my_value.b = 1003;
my_vector.push_back(my_value); my_value.a = 14; my_value.b = 1004;
my_vector.push_back(my_value); vector<value_t>::iterator it = find_if( my_vector.begin(), my_vector.end(), vector_finder(13,1003)); //传入数值,首先执行构造函数初始化,然后将my_vector中的数值依次传入vector_finder中查找
if( it == my_vector.end() )
cout<<"not found!"<<endl;
else
cout<<"found value a:"<<(*it).a <<", b:"<<(*it).b<<endl;
return 0;
}

在map中的应用:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std; class map_finder
{
public:
map_finder( string cmp_string ) : m_string(cmp_string) {}
bool operator () (const map<int,string>::value_type pair)
{
return pair.second == m_string;
}
private:
string m_string;
}; int main()
{
map<int ,string> my_map;
my_map.insert( make_pair(10,"china"));
my_map.insert( make_pair(20,"usa"));
my_map.insert( make_pair(30,"english"));
my_map.insert( make_pair(40,"hongkong")); map<int,string>::iterator it = find_if(my_map.begin(),my_map.end(),map_finder("english"));
if( it == my_map.end() )
cout<<"not found!"<<endl;
else
cout<<"found key:"<<(*it).first<<", value:"<<(*it).second<<endl;
return 0;
}

查找map中的数值,也可以通过数据来查找,不一定非要关键字。

find和find_if,value_type的更多相关文章

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

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

  2. STL中的find_if函数

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

  3. map按value值查找——find_if的使用(转载)

    转载:http://www.cnblogs.com/xufeiyang/archive/2012/05/09/2491871.html CValueFind #ifndef _CVALUEFIND_H ...

  4. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

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

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

  6. stl::find,find_if,find_if_not

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

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

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

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

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

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

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

随机推荐

  1. is 和 as 运算符

    is和as运算符:is是判断是否是某个类型,返回true或falseo as Ren: 如果转换成功了,没问题:as 是用来转换如果没转换成功,不会报出错误,而是返回一个null值 例 实例化一个集合 ...

  2. 输入框状态禁止enter键提交表单

    1:页面中如果存在input输入框和submit提交按钮时,默认按enter键会提交表单,如果我现在在做查询操作,一不小心按了enter键就会有提交表单的操作,这样显然是不合理的,所以我们要禁止按en ...

  3. 过滤access日志前5条数据

    cat /usr/local/nginx/logs/access.log|awk '{print $1}'|sort|uniq -c|sort -n -r|head -5 找不到的话可以find查找a ...

  4. 部署Java的运行环境

    1.先去官网下载最新的jdk 网址:http://www.oracle.com/technetwork/java/javase/downloads/ 2.用tar zxvf解压相应的文档到相应的路径 ...

  5. flexible

    https://www.w3cplus.com/mobile/lib-flexible-for-html5-layout.html

  6. Numpy random arange zeros

    seed( ) 用于指定随机数生成时所用算法开始的整数值. .如果使用相同的seed( )值,则每次生成的随即数都相同: .如果不设置这个值,则系统根据时间来自己选择这个值,此时每次生成的随机数因时间 ...

  7. JDK8中JVM堆内存划分

    一:JVM中内存 JVM中内存通常划分为两个部分,分别为堆内存与栈内存,栈内存主要用运行线程方法 存放本地暂时变量与线程中方法运行时候须要的引用对象地址. JVM全部的对象信息都 存放在堆内存中.相比 ...

  8. Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util

    Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util,导入包的方法为,import java.util.*: 完整的实例代码: /* 导入基础工具包 */ import java.uti ...

  9. 仿造mongodb的存储方式存一些假数据

    //存入数据 $data = json_encode($row); // 过滤 $data = addslashes($data); //读取数据 $falseData = stripslashes( ...

  10. python找包的路径(找不到自定义包的问题解决)

    问题:工程下自定义的包,python在执行时经常找不到包   python找包的路径:python安装路径下的lib包和PYTHONPATH下的包     可以使用[sys.path]打印出pytho ...