• 【C++】STL-常用算法-常用查找算法


    0.前言

    在这里插入图片描述

    1.find

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    // 常用查找算法 find
    #include
    #include
    
    //查找 内置数据类型
    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) //加const 防止修改数据
    	{
    		if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    
    	string m_Name;
    	int m_Age;
    };
    
    void test02()
    {
    	//创建数据
    	Person p1("a", 10);
    	Person p2("b", 20);
    	Person p3("c", 30);
    
    	//放入容器中
    	vector<Person>v;
    	v.push_back(p1);
    	v.push_back(p2);
    	v.push_back(p3);
    
    	Person p5("b", 20);
    
    	vector<Person>::iterator it = find(v.begin(), v.end(), p5);
    
    	if (it == v.end())
    	{
    		cout << "未找到" << endl;
    	}
    	else
    	{
    		cout << "找到: 姓名: " << it->m_Name << "  age:" << (*it).m_Age << endl;
    	}
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    在这里插入图片描述

    2.find_if

    在这里插入图片描述

    #include 
    using namespace std;
    
    // 常用查找算法 find_if
    #include
    
    
    //1.查找内置数据类型
    
    class Greater5
    {
    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(), Greater5());
    
    	if (it == v.end())
    	{
    		cout << "no find" << endl;
    	}
    	else
    	{
    		cout << "find element: " << (*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 greater_Age20
    //{
    //public:
    //	bool operator()(const Person& p)
    //	{
    //		return p.m_Age > 20;
    //	}
    //};
    
    //方式二 利用普通函数
    bool greater_Age20(const Person& p)
    {
    	return p.m_Age > 20;
    }
    
    
    void test02()
    {
    	//创建对象
    	Person p1("a", 10);
    	Person p2("b", 20);
    	Person p3("c", 30);
    	
    	vector<Person>v;
    	v.push_back(p1);
    	v.push_back(p2);
    	v.push_back(p3);
    
    	//查找年龄大于20的人
    	vector<Person>::iterator it = find_if(v.begin(), v.end(), greater_Age20);
    
    	if (it == v.end())
    	{
    		cout << "no find" << endl;
    	}
    	else
    	{
    		cout << "find the element: name:" << it->m_Name << " age:" << it->m_Age << endl;
    	}
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    在这里插入图片描述

    3.adjacent_find

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    // 常用查找算法 adjacent_find
    #include
    #include
    
    void test01()
    {
    	//创建对象
    	vector<int>v;
    	v.push_back(1);
    	v.push_back(2);
    	v.push_back(1);
    	v.push_back(3);
    	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 << "no find adjacent duplcate elements " << endl;
    	}
    	else
    	{
    		cout << "find adjacent duplcate elements: " << *pos << endl;
    	}
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    在这里插入图片描述

    4.binary_search

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    // 常用查找算法 binary_search 二分查找
    #include
    #include
    
    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 << "find point element" << endl;
    	}
    	else
    	{
    		cout << "no find" << endl;
    	}
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

    5.count

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    // 常用查找算法 count
    #include
    #include
    
    //1.统计内置数据类型
    void test01()
    {
    	vector<int>v;
    	v.push_back(1);
    	v.push_back(4);
    	v.push_back(1);
    	v.push_back(3);
    
    	int num = count(v.begin(), v.end(), 1);
    	
    	cout << "the nmber of point element: " << 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 (p.m_Age == this->m_Age)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    
    	string m_Name;
    	int m_Age;
    };
    void test02()
    {
    	//创建对象
    	Person p1("刘备", 35);
    	Person p2("薇恩", 24);
    	Person p3("皮城", 35);
    	Person p4("光辉", 40);
    
    	vector<Person>v;
    	v.push_back(p1);
    	v.push_back(p2);
    	v.push_back(p3);
    	v.push_back(p4);
    
    	Person p5("卡米尔", 35);
    
    	int num = count(v.begin(), v.end(), p5);
    	 
    	cout << "跟卡米尔同岁的人有多少个: " << num << endl;
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    在这里插入图片描述

    6.count_if

    在这里插入图片描述

    #include 
    using namespace std;
    
    
    // 常用查找算法 count_if
    #include
    #include
    
    //1、统计内置数据类型
    
    //利用仿函数
    class Greater3
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 3;
    	}
    };
    
    void test01()
    {
    	vector<int>v;
    	v.push_back(1);
    	v.push_back(6);
    	v.push_back(3);
    	v.push_back(2);
    	v.push_back(4);
    	v.push_back(3);
    
    	int num = count_if(v.begin(), v.end(), Greater3());
    
    	cout << "the number of element greater than 3 :  " << num << endl;
    }
    
    //2、统计自定义数据类型
    
    class Person
    {
    public:
    	Person(string name, int age)
    	{
    		this->m_Name = name;
    		this->m_Age = age;
    	}
    
    	string m_Name;
    	int m_Age;
    };
    
    //利用普通函数
    bool age_Greater35(const Person& p)
    {
    	if (p.m_Age > 35)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    void test02()
    {
    
    	//创建对象
    	Person p1("刘备", 35);
    	Person p2("薇恩", 24);
    	Person p3("皮城", 35);
    	Person p4("光辉", 40);
    	
    	vector<Person>v;
    	v.push_back(p1);
    	v.push_back(p2);
    	v.push_back(p3);
    	v.push_back(p4);
    
    	//统计岁数大于35的人物的个数
    	int num = count_if(v.begin(), v.end(), age_Greater35);
    
    	cout << "the number of character whose age greater than 35 :" << num << endl;
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    在这里插入图片描述

  • 相关阅读:
    基于元模型优化算法的主从博弈多虚拟电厂动态定价和能量管理MATLAB程序
    【学习总结】SpringBoot中使用单例模式+ScheduledExecutorService实现异步多线程任务(若依源码学习)
    Freeswitch学习笔记(三):配置
    云小课|云小课教您如何选择Redis实例类型
    .cn是几级域名?
    C++学习第二十九天----引用变量和c语言之register关键字
    BufferCache与PageCache
    DDD的简单落地实现
    关于rdkit 错误2w08_ligand: warning - O.co2 with non C.2 or S.o2 neighbor.
    读取excel
  • 原文地址:https://blog.csdn.net/m0_48808835/article/details/132724100