c++STL之常用查找算法

引入#include<algorithm>

算法简介:

  • find:查找元素
  • find_if:按条件查找
  • adjacent_find:查找相邻房重复的元素
  • binary_search:二分查找
  • count:统计元素个数
  • count_if:按条件统计元素个数

1.find

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

//常用查找算法
//find

//查找 内置数据类型
void test01()
{
    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 << "找到: " << *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 test02()
{
    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);

    Person pp("bbb", 20);

    vector<Person>::iterator it = find(v.begin(), v.end(), pp);
    if (it == v.end())
    {
        cout << "没有找到" << endl;
    }
    else
    {
        cout << "找到元素  姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
    }
}

int main() {

    //test01();

    test02();

    system("pause");

    return 0;
}

2.find_if

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
//常用查找算法 find_if

//1、查找内置数据类型
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};

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

}

//2、查找自定义数据类型
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;
    }
};

void test02()
{
    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);

    //找年龄大于20的人
    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;
    }
}

int main() {

    //test01();

    test02();

    system("pause");

    return 0;
}

3.adjacent_find

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

//常用查找算法 adjacent_find
void test01()
{
    vector<int>v;
    v.push_back(0);
    v.push_back(2);
    v.push_back(0);
    v.push_back(3);
    v.push_back(1);
    v.push_back(4);
    v.push_back(3);
    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() {

    test01();

    system("pause");

    return 0;
}

4.binary_search

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

//常用查找算法 binary_search
void test01()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //v.push_back(2);  如果是无序序列,结果未知!
    //查找容器中是否有9 元素
    //注意:容器必须是有序的序列
    bool ret = binary_search(v.begin(), v.end(), 9);

    if (ret)
    {
        cout << "找到了元素" << endl;
    }
    else
    {
        cout << "未找到" << endl;
    }
}

int main() {

    test01();

    system("pause");

    return 0;
}

5.count

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
//常用查找算法_count

//1、统计内置数据类型

void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(40);
    v.push_back(20);
    v.push_back(40);

    int num = count(v.begin(), v.end(), 40);

    cout << "40的元素个数为: " << num << endl;
}

//2、统计自定义数据类型

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

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

    string m_Name;
    int m_Age;
};

void test02()
{
    vector<Person>v;

    Person p1("刘备", 35);
    Person p2("关羽", 35);
    Person p3("张飞", 35);
    Person p4("赵云", 30);
    Person p5("曹操", 40);

    //将人员插入到容器中
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    Person p("诸葛亮", 35);

    int num = count(v.begin(), v.end(), p);

    cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}

int main() {

    //test01();

    test02();

    system("pause");

    return 0;
}

6.count_if

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

//常用查找算法  count_if

//统计内置数据类型
class Greater20
{
public:
    bool operator()(int val)
    {
        return val > 20;
    }
};

void test01()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(20);

    int num = count_if(v.begin(), v.end(), Greater20());

    cout << "大于20的元素个数为: " << num << endl;
}

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

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

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

    Person p1("刘备", 35);
    Person p2("关羽", 35);
    Person p3("张飞", 35);
    Person p4("赵云", 40);
    Person p5("曹操", 20);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    //统计  大于20岁人员个数
    int num = count_if(v.begin(), v.end(), AgeGreater20());
    cout << "大于20岁的人员个数为:" << num << endl;
}

int main() {

    //test01();

    test02();

    system("pause");

    return 0;
}

原文地址:https://www.cnblogs.com/xiximayou/p/12114737.html

时间: 2024-08-04 16:46:53

c++STL之常用查找算法的相关文章

STL中的查找算法

STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中.众多算法中,查找算法是应用最为普遍的一类. 单个元素查找 1. find() 比较条件为元素是否相等的查找: template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val); 2.fi

常用查找算法

原文出处:http://www.cnblogs.com/yw09041432/p/5908444.html 1.顺序查找 2.二分查找 3.差值查找 4.肥婆那楔查找 5.树表查找 6.分块查找 7.哈希查找 查找:根据给定的某个值,在查找表中确定一个其关键字等于给定值得数据元素(或记录). 查找算法分类: 1.静态查找和动态查找 动态查找指查找表中有删除和插入操作的表. 2.无序查找和有序查找 无序查找:被查找数列有序无序均可 有序查找:被查找数列必须有序 1.顺序查找 基本思想:顺序查找也称

c++STL之常用排序算法

sort:对容器元素进行排序 random_shuffle:洗牌,指定范围内的元素随机调整次序 merge:容器元素合并,并存储到另一容器中 reverse:反转指定范围内的元素 1.sort #include<iostream> using namespace std; #include <algorithm> #include <vector> #include <functional> //常用排序算法 sort void myPrint(int va

c++STL之常用集合算法

set_intersection:求两个容器的交集 set_union:求两个集合的并集 set_difference:求两个集合的差集 1.set_intersection #include<iostream> using namespace std; #include <vector> #include <algorithm> //常用集合算法 set_intersection void myPrint(int val) { cout << val &l

STL常用查找算法介绍

adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void play_adjacent_find() { vector<int> v1

常用查找算法的总结

数据结构中查找的知识点主要有以下三点 1.静态查找 1.1 顺序查找 1.2 有序表 1.2.1 二分查找 1.2.2 插值查找 2.动态查找 2.1 二叉排序树 2.2 平衡二叉树 2.3 B-和B+树 3.哈希查找 3.1 常用哈希函数 3.2 解决冲突的办法 1.2.1.1  非递归实现 实现思路: 1.low=0,high=len-1; 2.mid=(low+high)/2; 3.中间值小于目标值,high=mid-1 4.中间值大于目标值,low=mid+1 5.重复 public s

【Java_Base】常用查找算法:顺序查找、二分查找

顺序查找 从第一个元素开始顺序比较查找. 二分查找 二分查找前提条件: 已排序的数组中查找 二分查找的基本思想是: 首先确定该查找区间的中间点位置: int mid = (low+upper) / 2; 然后将待查找的值与中间点位置的值比较: 若相等,则查找成功并返回此位置. 若中间点位置值大于待查值,则新的查找区间是中间点位置的左边区域. 若中间点位置值小于待查值,则新的查找区间是中间点位置的右边区域. 下一次查找是针对新的查找区间进行的. 1 public class Search{ 2 p

常用查找算法总结

1. 二分查找 //递归版 int binarySearch(const int arr[], int low, int high, int val) { if (low <= high) { int mid = low + (high - low) / 2; // Do not use (low + high) / 2 which might encounter overflow issue if (val < arr[mid]) return binarySearch(arr, low,

常用查找算法Python实现

二分法查找(递归): def binarySearch(arr, min, max, key): mid = int((max + min)/2) if key < arr[mid]: return binarySearch(arr, min, mid-1, key) elif key > arr[mid]: return binarySearch(arr, mid+1, max, key) elif key == arr[mid]: print("找到{0}了!是第{1}个数字!&