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. 为tomcat配置项目必须的引擎文件

    1.如下图所示,红框圈出来的四个语音引擎文件是直接放在项目根目录下的,这样的话我们发布web应用的时候,项目并不能自动把这几个文件打包到tomcat中, 除非放到WebRoot文件夹下,但是这样的话项 ...

  2. (转)MySQL 获得当前日期时间 函数

    select *from High_valwhere SerialDate >= curdate() and SerialDate < date_add(curdate(), interv ...

  3. easyui分页,根据网友的一段代码优化了一下

    千言万语尽在代码中,可以自己看,不清楚留言吧! <%@ Page Language="C#" AutoEventWireup="true" CodeBeh ...

  4. Swift 2.0学习笔记(Day 16)——字典集合

    Swift字典表示一种非常复杂的集合,允许按照某个键来访问元素.字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合.键集合是不能有重复元素的,而值集合是可以重复的,键和值是成 ...

  5. Java模板引擎之freemarker简介

  6. Spark之机器学习(Python版)(一)——聚类

    https://www.cnblogs.com/charlotte77/p/5437611.html

  7. 人脸识别 1:1 和1:n

  8. 第一次登录mysql,使用任何命令都报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    问题: 使用临时密码登录成功后,使用任何myql命令,例如show databases;都提示下面的报错 ERROR 1820 (HY000): You must reset your passwor ...

  9. centos7 mysql主从数据库同步

    主:192.168.2.222:从:192.168.2.223 一:安装mysql 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql!这里依旧以mysql为例进行展示 1.先检 ...

  10. docker save 批量导出脚本

    [root@vultr home]# cat docker_sove.sh docker images > images.txtawk '{print $1}' images.txt > ...