c++中STL常用查找算法

常用查找算法

1.find()

功能:

查找指定val值,如果找到则返回val值的迭代器,否则返回末尾迭代器。

核心语句:

find(iterator beg,iterator end, val)

查找val值,返回val值的迭代器。如果找不到返回末尾迭代器end()

头文件:

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int> v;
    v.push_back(3);
    v.push_back(1);
    v.push_back(7);
    v.push_back(5);
    v.push_back(2);
    vector<int>::iterator it=find(v.begin(),v.end(),7);
    auto pop=distance(v.begin(), it );
  	//查看7的下标
    cout<<pop<<endl;
    //pop=7
  	vector<int>::iterator 	   index=find(v.begin(),v.end(),4);
    auto x=distance(v.begin(), index );
    cout<<x<<endl;
  	//未找到,x的值为5
    return 0;
}

2.find_if

功能:

根据条件查找元素

核心语句:

find_if(iterator begin(),iterator end(),_pred

查找元素,返回元素的迭代器。如果找不到返回末尾迭代器end()

—pred是函数或者谓词。

头文件:

代码:

//内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class greaterfive
{
public:
    bool operator()(int val)
    {
        return val>5;
    }
};
int main()
{
    vector<int>v;
    for(int i=0;i<10;i++)
    {
        v.push_back(i);
    }
    vector<int>::iterator it=find_if(v.begin(), v.end(), greaterfive());
    if (it == v.end())
    {
        cout << "没有找到!" << endl;
        
    }
    else
    {
        cout << "找到大于5的数字:" << *it << endl;
        
    }
    return 0;
}



/*
找到大于5的数字:6
Program ended with exit code: 0
*/
//自定义数据类型
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

class Person
{
public:
    Person(string name,int age)
    {
        this->m_Name=name;
        this->m_Age=age;
    }
    string m_Name;
    int m_Age;
};

class Greater20
{
public:
    bool operator()(Person &p)
    {
        return p.m_Age>20;
    }
};
int main()
{
    vector<Person>v;
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);
    
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
    if (it == v.end())
    {
        cout << "没有找到!" << endl;
        
    }
    else
    {
        cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
        
    }
    return 0;
}

/*
找到姓名:ccc 年龄: 30
Program ended with exit code: 0
*/

3.Adjacent_find

功能:

查找相邻重复元素

核心代码:

adjacent_find(iterator beg,iterator end);

查找相邻重复元素,返回相邻元素的第一个位置的迭代器

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(3);
    v.push_back(1);
    v.push_back(3);
    v.push_back(3);
    v.push_back(4);
    v.push_back(3);
    vector<int>::iterator it=adjacent_find(v.begin(), v.end());
    cout<<*it<<endl;
    return 0;
}

/*
3
Program ended with exit code: 0
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(3);
    v.push_back(1);
    v.push_back(3);
    v.push_back(0);
    v.push_back(4);
    v.push_back(3);
    vector<int>::iterator it=adjacent_find(v.begin(), v.end());
    if(it==v.end())
        cout<<"未找到相邻元素"<<endl;
    return 0;
}

/*
未找到相邻元素
Program ended with exit code: 0
*/

4.binary_search

功能:

查找指定元素是否存在,返回一个bool值

核心语句:

bool binary_search(iterator beg, iterator end, val);

查找指定val值,查到 返回true 否则false

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(6);
    bool pop=binary_search(v.begin(), v.end(),1);
    if(pop)
        cout<<"找到该元素"<<endl;
    else
        cout<<"未找到该元素"<<endl;
    return 0;
}
/*
找到该元素
Program ended with exit code: 0
*/

注意:容器必须是有序的,如:1 ,2,3,4……,若是无序,如:3,3,1,4,6,2……,就会出错

出错的代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(4);
    v.push_back(1);
    v.push_back(3);
    v.push_back(8);
    v.push_back(7);
    v.push_back(6);
    bool pop=binary_search(v.begin(), v.end(),1);
    if(pop)
        cout<<"找到该元素"<<endl;
    else
        cout<<"未找到该元素"<<endl;
    return 0;
}
/*
未找到该元素
Program ended with exit code: 0
*/

5.count

功能:

统计元素个数

核心语句:

count(iterator beg, iterator end, val);

统计元素val出现次数

代码:

//内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(4);
    v.push_back(1);
    v.push_back(3);
    v.push_back(8);
    v.push_back(3);
    v.push_back(6);
    int pop=count(v.begin(), v.end(),3);
    cout<<pop<<endl;
    return 0;
}
/*
2
Program ended with exit code: 0
*/
//自定义数据类型
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

class Person
{
public:
    Person(string name,int age)
    {
        this->m_name=name;
        this->m_age=age;
    }
  //重载了==
    bool operator==(const Person &P)
    {
        if(P.m_age==this->m_age)
            return true;
        else
            return false;
    }
    
    string m_name;
    int m_age;
};
int main()
{
    Person p1("aaa",20);
    Person p2("bbb",22);
    Person p3("ccc",20);
    Person p4("ddd",21);
    Person p5("eee",20);
    vector<Person>v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    int pop=count(v.begin(), v.end(), p1);
    cout<<pop<<endl;
    return 0;
}

6.count_if

功能:

按条件统计元素个数

核心语句:

count_if(iterator beg, iterator end, _Pred);

// 按条件统计元素出现次数

// _Pred 谓词

代码:

//内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

class pred
{
public:
    bool operator()(int val)
    {
        return val==1;
    }
    
};
int main()
{
    vector<int>v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(1);
    v.push_back(1);
    v.push_back(4);
    v.push_back(1);
    int pop=count_if(v.begin(), v.end(), pred());
    cout<<pop<<endl;
    return 0;
}
/*
4
Program ended with exit code: 0
*/
//自定义数据类型
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

class Person
{
public:
    Person(string name,int age)
    {
        this->m_name=name;
        this->m_age=age;
    }
    string m_name;
    int m_age;
};
class pred
{
public:
    bool operator()(Person &p)
    {
        return p.m_age<=20;
    }
};
int main()
{
    vector<Person>v;
    Person p1("aaa",20);
    Person p2("bbb",22);
    Person p3("ccc",21);
    Person p4("ddd",20);
    Person p5("eee",20);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    int pop=count_if(v.begin(), v.end(),pred());
    cout<<pop<<endl;
    return 0;
}

/*
3
Program ended with exit code: 0
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kaiaaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值