前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >STL算法(查找)简介adjacent_findbinary_searchcount/count_iffind/find_if/equal_range

STL算法(查找)简介adjacent_findbinary_searchcount/count_iffind/find_if/equal_range

作者头像
用户2929716
发布2018-08-23 13:22:11
3510
发布2018-08-23 13:22:11
举报
文章被收录于专栏:流媒体

简介

adjacent_find binary_search cout/cout_if find/find_if/equal_range

adjacent_find

在一个集合中寻找两个相邻的元素。如果相等,就返回这两个相等元素第一个元素的迭代器,不等的话,就返回v.end(). 函数原型:

代码语言:javascript
复制
template<class _FwdIt,
    class _Pr> inline
    _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
    {   // find first satisfying _Pred with successor
    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER_IF(_First != _Last && _STD next(_First) != _Last, _Pred);
    return (_Rechecked(_First,
        _Adjacent_find_unchecked(_Unchecked(_First), _Unchecked(_Last), _Pred)));
    }

        // TEMPLATE FUNCTION adjacent_find
template<class _FwdIt> inline
    _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
    {   // find first matching successor
    return (_STD adjacent_find(_First, _Last, equal_to<>()));
    }

示例

代码语言:javascript
复制
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
using namespace std;
class Student{
private:
    int number;
    string name;
public:
    Student(int number, string name) {
        cout << "构造 " << number << " " << name.c_str() << endl;
        this->number = number;
        this->name = name;
    }
    Student(const Student & stu) {
        //cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
        this->number = stu.getNumber();
        this->name = stu.getName();
    }
    ~Student() {
        //cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
    }

    void print() {
        cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
    }

    int getNumber() const{
        return this->number;
    }
    string getName()const {
        return this->name;
    }
};

struct StuFunc
{
    bool operator()(const Student & stu1, const Student & stu2) const {
        return stu1.getNumber() == stu2.getNumber();
    }
};

void printStuV(vector<Student> v) {
    cout << "开始遍历============" << endl;
    for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
        it->print();
    }
    cout << "结束遍历============" << endl;
}
int main()
{
    vector<Student> v;
    v.push_back(Student(1, "one"));
    Student stu2(2, "two");
    v.push_back(stu2);
    v.push_back(stu2);
    v.push_back(Student(3, "three"));
    v.push_back(Student(4, "four"));
    v.push_back(Student(5, "five"));
    printStuV(v);
    vector<Student>::iterator res=adjacent_find(v.begin(), v.end(), StuFunc());
    if (res != v.end()) {
        cout << "adjacent_find result:"<<endl;
        res++->print();
        res++->print();
        res->print();
    }
    else {
        cout << "adjacent_find nothing!" << endl;
    }
    return 0;
}

结果:

adjacent_find.png

binary_search

二分查找 集合必须是有序的

示例

代码语言:javascript
复制
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
using namespace std;
class Student{
private:
    int number;
    string name;
public:
    Student(int number, string name) {
        cout << "构造 " << number << " " << name.c_str() << endl;
        this->number = number;
        this->name = name;
    }
    Student(const Student & stu) {
        //cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
        this->number = stu.getNumber();
        this->name = stu.getName();
    }
    ~Student() {
        //cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
    }

    void print()const {
        cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
    }

    int getNumber() const{
        return this->number;
    }
    string getName()const {
        return this->name;
    }
};

struct StuFunc
{
    bool operator()(const Student & stu1, const Student & stu2) const {
        cout << "StuFunc》》"<<endl;
        stu1.print();
        stu2.print();
        return stu1.getNumber() <stu2.getNumber();
    }
};

void printStuV(vector<Student> v) {
    cout << "开始遍历============" << endl;
    for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
        it->print();
    }
    cout << "结束遍历============" << endl;
}
int main()
{
    vector<Student> v;
    v.push_back(Student(1, "one"));
    Student stu2(2, "two");
    v.push_back(stu2);
    v.push_back(stu2);
    v.push_back(Student(3, "three"));
    v.push_back(Student(4, "four"));
    v.push_back(Student(5, "five"));
    printStuV(v);
    bool res=binary_search(v.begin(), v.end(),Student(7,"keyword"), StuFunc());
    cout << "binary_search 7: " << res << endl << endl;;
    res = binary_search(v.begin(), v.end(), Student(2, "keyword"), StuFunc());
    cout << "binary_search 2: " << res << endl;
    return 0;
}

结果:

binary_search.png

count/count_if

前两个参数是iterator(迭代器),表示查找半闭合区间的前后两个位置。

  • count_if 第三个参数是具体元素
  • count_if 更加灵活,用来查找自定义类型数据或复杂的查询条件
    • 带参数的仿函数
    • 通过函数适配器使用系统预定义的仿函数。 bind2nd( greater<int>(), 1)

示例

这里的Student沿用前面的定义。

代码语言:javascript
复制
struct StuCount {
private:
    int num ;
public:
    StuCount(const int num) {
        this->num = num;
    }
    bool operator()(const Student & stu) {
        return stu.getNumber() == num;
    }
};

int main()
{
    vector<Student> v;
    v.push_back(Student(1, "one"));
    Student stu2(2, "two");
    v.push_back(stu2);
    v.push_back(stu2);
    v.push_back(Student(3, "three"));
    v.push_back(Student(4, "four"));
    v.push_back(Student(5, "five"));
    printStuV(v);
    int sum=count_if(v.begin(), v.end(), StuCount(2));
    cout << "vStu count_if 2: " << sum << endl;
    vector<int> vNum;
    vNum.push_back(1);
    vNum.push_back(1);
    vNum.push_back(1);
    vNum.push_back(2);
    cout << "vNum count 1: " << count(vNum.begin(),vNum.end(),1) << endl;
    cout << "vNum count_if 1: " << count_if(vNum.begin(), vNum.end(),bind2nd( greater<int>(), 1)) << endl;
    return 0;
}

结果:

count.png

find/find_if/equal_range

  • find 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
  • equal_range: 返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。
  • find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。
代码语言:javascript
复制
struct StuCount {
private:
    int num ;
public:
    StuCount(const int num) {
        this->num = num;
    }
    bool operator()(const Student & stu) {
        return stu.getNumber() == num;
    }
};

int main()
{
    vector<Student> v;
    v.push_back(Student(1, "one"));
    Student stu2(2, "two");
    v.push_back(stu2);
    v.push_back(stu2);
    v.push_back(Student(3, "three"));
    v.push_back(Student(4, "four"));
    v.push_back(Student(5, "five"));
    printStuV(v);
    vector<Student>::iterator itStuRes=find_if(v.begin(), v.end(), StuCount(2));
    if (itStuRes != v.end()) {
        itStuRes->print();
    }
    vector<int> vNum;
    vNum.push_back(1);
    vNum.push_back(1);
    vNum.push_back(1);
    vNum.push_back(2);
    vector<int>::iterator itNumRes=find(vNum.begin(), vNum.end(), 2);
    cout <<"num find " << *itNumRes << endl;

    pair<vector<int>::iterator,vector<int>::iterator> pRes=equal_range(vNum.begin(), vNum.end(), 1);
    cout << "equal_range left:" << *pRes.first << " right:" << *pRes.second << endl;
    return 0;
}

结果:

find.png

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.09.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • adjacent_find
    • 示例
    • binary_search
      • 示例
      • count/count_if
        • 示例
        • find/find_if/equal_range
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档