专栏首页流媒体STL算法(查找)简介adjacent_findbinary_searchcount/count_iffind/find_if/equal_range

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

简介

adjacent_find binary_search cout/cout_if find/find_if/equal_range

adjacent_find

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

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<>()));
    }

示例

#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

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

示例

#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沿用前面的定义。

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。返回被找到的元素的迭代器。
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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • STL(一)vector、set/multiset、listVectorSetmultisetlist

    vector封装数组,list封装了链表,map和set封装了二叉树等。set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据...

    用户2929716
  • STL算法(算数/生成)简介accumulatefill

    用户2929716
  • STL算法(排序)简介mergesortrandom_shufflereverse

    对指定范围内的元素随机调整次序 srand(time(0)); //设置随机种子

    用户2929716
  • 耶鲁大学等多个机构研究新成果,用机器人教聋哑儿童如何交流

    在上面的视频中,那个孩子坐在他母亲的腿上,看着这个大眼睛机器人的头部,这个机器人会周期性的向左旋转,用它那个蓝色的大眼睛盯着一个电脑屏幕。孩子仿佛接收到了指令,...

    企鹅号小编
  • 重新回味·Block·

    说起Block,我想大家再熟悉不过了。每天的开发过程中都在与他打着交道,简洁高效的使用给我们的工作带来了便利。

    Jacklin999
  • 深入理解Block

    我用了将近一周的时间将与Block相关的知识点进行了整理,将相关内容整理成了脑图,分享给大家:

    Jacklin
  • SAP登录窗口设置默认client

    SAP Library:http://help.sap.com/saphelp_47x200/helpdata/en/22/41c43ac23cef2fe100...

    matinal
  • 10条让web app更快的HTML5最佳实践

    简介 本文重点关注如何充分利用HTML5和CSS让web app运行更加流畅. Tip 1: 使用web storage代替cookie cookie最大的缺陷...

    企鹅号小编
  • 谈谈map中的count方法

    map和set两种容器的底层结构都是红黑树,所以容器中不会出现相同的元素,因此count()的结果只能为0和1,可以以此来判断键值元素是否存在(当然也可以使用f...

    用户1215536
  • 最快捷的Linux命令查询工具来了:「我该怎么做XX」,一句话自动返回操作指南

    遇到问题要么去论坛询问、谷歌搜索,要么查自带命令手册(RTFM),过程耗时,结果也不直观。

    用户6543014

扫码关注云+社区

领取腾讯云代金券