美文网首页
3.常用查找算法

3.常用查找算法

作者: lxr_ | 来源:发表于2021-05-05 18:16 被阅读0次
#include<iostream>
using namespace std;

#include<vector>
#include<algorithm>

//find(interator begin,iterator end,value)            查找元素,返回找到元素的迭代器,找不到,返回结束迭代器end()
//find_if         按条件查找元素
//adjacent_find   查找相邻重复元素
//binary_search   二分查找法
//count           统计元素个数
//count_if        按条件统计元素个数

//查找内置数据类型
void test0301()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int>::iterator it=find(v.begin(), v.end(), 5);
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到" << (*it) << endl;
    }
}
//查找自定义数据类型
class Person
{
public:
    Person(string name,int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

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

    string m_Name;
    int m_Age;
};
void test0302()
{
    vector<Person> v;

    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);
    Person p5("eee", 50);

    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(), p2);
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到" << it->m_Name << "  " << it->m_Age << endl;
    }
}
int main()
{
    test0301();
    test0302();

    system("pause");
    return 0;
}

相关文章

  • 3.常用查找算法

  • 子字符串查找(1)

    一、定义 本文主要介绍子字符串查找的各类常用算法,如朴素匹配算法(暴力查找)、KMP算法、BM算法等。各类匹配算法...

  • 常用查找算法

    find(iterator beg, iterator end, value) find算法 查找元素 @para...

  • 常用查找算法

    顺序查找 适合于存储结构为顺序存储或链接存储的线性表。顺序查找也称为线形查找,属于无序查找算法public sta...

  • 常用的 STL 查找算法

    常用的 STL 查找算法 《effective STL》中有句忠告,尽量用算法替代手写循环;查找少不了循环遍历,在...

  • 2019-01-28

    Local Search 常用的local search 算法有 爬山算法, 模拟退火算法, 遗传算法和禁忌查找等...

  • 常见的查找算法

    在我们开发中经常需要查找内容,那么我们如何利用更好的算法去实现查找的内容。下面就介绍几种常用的查找算法。 第一种,...

  • 二分查找--基于python

    简介 二分查找是查找中常用的算法,时间复杂度为O(logn)(时间复杂度用来衡量一个算法查找的时间效率,是最糟糕的...

  • 数据结构与算法——基础篇(六)

    常用10种算法 1、二分查找算法(非递归)——要求有序 二分查找法只适用于从有序的数列中进行查找(比如数字和字母等...

  • 数据结构之kmp算法

    Knuth-Morris-Pratt 字符串查找算法,简称为 “KMP算法”,常用于在一个文本串S内查找一个模式串...

网友评论

      本文标题:3.常用查找算法

      本文链接:https://www.haomeiwen.com/subject/fkbirltx.html