c++常用查找算法

常用查找算法

算法简介

在这里插入图片描述

find

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

/**
 * 查找内置数据类型
 */
void test() {
    vector<int> v;

    //插入数据
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }

    //查找 容器中是否有 5 这个元素
    vector<int>::iterator it = find(v.begin(), v.end(), 5);

    if (it == v.end()) {//找到了
        cout << "没有找到!" << endl;
    } else {
        cout << "找到了!" << endl;
    }
}


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

    //需要重载== 底层find知道如何对比Person数据类型
    bool operator==(const Person &p) {
        if (this->name == p.name && this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

/**
 * 查找自定义数据类型
 */
void test02() {
    vector<Person> v;

    //创建数据
    Person p1("aaa", 19);
    Person p2("bbb", 18);
    Person p3("ccc", 20);
    Person p4("ddd", 16);
    Person p5("eee", 23);


    //插入数据
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    vector<Person>::iterator it = find(v.begin(), v.end(), p3);

    if (it == v.end()) {//找到了
        cout << "没有找到!" << endl;
    } else {
        cout << "找到了!" << endl;
    }
}

int main() {
    test();
    test02();
    return 0;
}

find_if

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

class GreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};

/**
 * 查找内置数据类型
 */
void test() {
    vector<int> v;

    //插入数据
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }

    //查找大于5的数,找到了返回数的位置,没有则返回容器后一位
    vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());

    if (it == v.end()) {//找到了
        cout << "没有找到!" << endl;
    } else {
        cout << "找到了!" << endl;
    }
}


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

    //需要重载== 底层find知道如何对比Person数据类型
    bool operator==(const Person &p) {
        if (this->name == p.name && this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

class Greater20{
public:
    bool operator()(Person &p){
        return p.age > 20;
    }
};

/**
 * 查找自定义数据类型
 */
void test02() {
    vector<Person> v;

    //创建数据
    Person p1("aaa", 19);
    Person p2("bbb", 18);
    Person p3("ccc", 20);
    Person p4("ddd", 16);
    Person p5("eee", 23);


    //插入数据
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    //查找年龄大于20的人,有则返回迭代器,没有返回end()
   vector<Person>::iterator it = find_if(v.begin(),v.end(),Greater20());

    if (it == v.end()) {//找到了
        cout << "没有找到!" << endl;
    } else {
        cout << "找到了!" << endl;
    }
}

int main() {
    test();
    test02();
    return 0;
}

adjacent_find

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

void test() {
    vector<int> v;

    //插入数据
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    v.push_back(0);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);

    //查找相邻重复的元素,返回重复元素的第一个元素迭代器
    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());

    if (pos == v.end()) {
        cout << "没有找到相邻重复元素" << endl;
    } else {
        cout << "相邻重复元素是:" << *pos << endl;
    }

}

int main() {
    test();
    return 0;
}

binary_search

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

void test() {
    vector<int> v;

    //插入数据
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }

    //查询容器中有没有9,容器必须是一个有序的序列
    bool result =  binary_search(v.begin(),v.end(),9);

    if (result) {
        cout << "有9" << endl;
    } else {
        cout << "没有9" << endl;
    }
}

int main() {
    test();
    return 0;
}

cout

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

/**
 * 统计内置数据类型
 */
void test() {
    vector<int> v;

    //插入数据
    v.push_back(10);
    v.push_back(10);
    v.push_back(4);
    v.push_back(6);
    v.push_back(2);
    v.push_back(3);
    v.push_back(3);

    int result = count(v.begin(), v.end(), 10);
    cout << "10的元素个数为:" << result << endl;
}

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

    bool operator==(const Person &p) {
        if (this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

/**
 * 统计自定义数据类型
 */
void test2() {
    vector<Person> v;

    //插入数据
    Person p1("刘备", 18);
    Person p2("关羽", 19);
    Person p3("刘禅", 10);
    Person p4("赵云", 18);
    Person p5("张飞", 85);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    Person pTest("诸葛亮", 18);

    //与诸葛亮年龄一样的有多少个,自定义类型需要重写==
    int result = count(v.begin(), v.end(), pTest);
    cout << "与诸葛亮年龄一样的有" << result << "个" << endl;

}

int main() {
    test();
    return 0;
}

count_if

功能描述

在这里插入图片描述

函数原型

在这里插入图片描述

示例

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

class GreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};

/**
 * 统计内置数据类型
 */
void test() {
    vector<int> v;

    //插入数据
    v.push_back(10);
    v.push_back(10);
    v.push_back(4);
    v.push_back(6);
    v.push_back(2);
    v.push_back(3);
    v.push_back(3);

    //查找大于5的元素个数
    int result = count_if(v.begin(), v.end(), GreaterFive());
    cout << "大于5的元素个数为:" << result << endl;
}

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

    bool operator==(const Person &p) {
        if (this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

class Greater20{
public:
    bool operator()(const Person &p){
        return p.age > 20;
    }
};

/**
 * 统计自定义数据类型
 */
void test2() {
    vector<Person> v;

    //插入数据
    Person p1("刘备", 18);
    Person p2("关羽", 19);
    Person p3("刘禅", 10);
    Person p4("赵云", 18);
    Person p5("张飞", 85);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);


    //年龄大于20的元素个数
    int result = count_if(v.begin(), v.end(), Greater20());
    cout << "年龄大于20的元素个数有" << result << "个" << endl;

}

int main() {
    test();
    test2();
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值