find和find_if,value_type

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中的数值,也可以通过数据来查找,不一定非要关键字。

猜你喜欢

转载自www.cnblogs.com/ymd12103410/p/9613833.html