• C++之常用算法


    C++之常用算法

    在这里插入图片描述

    for_each

    transform

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    
    class Tranfor 
    {
    public:
    	int operator()(int var)
    	{
    		return var;
    	}
    };
    
    class MyPrint
    {
    public:
    	void operator()(int var)
    	{
    		cout << var<<" " ;
    	}
    };
    void test()
    {
    	vectorv;
    	for (int i = 0;i < 10;i++)
    	{
    		v.push_back(i);
    	}
    
    	vectorvTarget;//目标容器
    	vTarget.resize(v.size());//目标容器需要提前开辟空间
    
    	transform(v.begin(),v.end(),vTarget.begin(),Tranfor());
    
    	//遍历目标容器
    	for_each(vTarget.begin(), vTarget.end(),MyPrint());
    	cout << endl;
    }
    
    int main()
    {
    	test();
    
    	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

    在这里插入图片描述

    常用查找算法

    find

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    //查找内置数据类型
    
    void test()
    {
    	vectorv;
    	for (int i = 0;i < 10;i++)
    	{
    		v.push_back(i);
    	}
    	
    	vector::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 test2()
    {
    	//定义容器
    	vectorv1;
    	//创建数据(对象)
    	Person p1("aaa", 20);
    	Person p2("bbb", 30);
    	Person p3("ccc", 40);
    	Person p4("ddd", 50);
    
    	//将数据放入容器
    	v1.push_back(p1);
    	v1.push_back(p2);
    	v1.push_back(p3);
    	v1.push_back(p4);
    
    	//查到对象数据
    	vector::iterator it = find(v1.begin(), v1.end(), p2);
    	if (it == v1.end())
    	{
    		cout << "没有找到" << endl;
    	}
    	else
    	{
    		cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;
    	}
    }
    
    int main()
    {
    	test2();
    
    	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

    在这里插入图片描述

    find_if

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    //查找内置数据类型
    class Greater
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 5;
    	}
    };
    
    void test()
    {
    	vectorv;
    	for (int i = 0;i < 10;i++)
    	{
    		v.push_back(i);
    	}
    	
    	vector::iterator it = find_if(v.begin(),v.end(), Greater());
    	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;
    	}
    	string m_Name;
    	int m_Age;
    };
    class GreatAge
    {
    public:
    	bool operator()(Person&p)
    	{
    		return p.m_Age > 20;
    	}
    };
    void test2()
    {
    	//定义容器
    	vectorv1;
    	//创建数据(对象)
    	Person p1("aaa", 20);
    	Person p2("bbb", 30);
    	Person p3("ccc", 40);
    	Person p4("ddd", 50);
    
    	//将数据放入容器
    	v1.push_back(p1);
    	v1.push_back(p2);
    	v1.push_back(p3);
    	v1.push_back(p4);
    
    	//查到对象数据
    	vector::iterator it = find_if(v1.begin(), v1.end(), GreatAge());
    	if (it == v1.end())
    	{
    		cout << "没有找到" << endl;
    	}
    	else
    	{
    		cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;
    	}
    }
    
    int main()
    {
    	test2();
    
    	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

    在这里插入图片描述

    adjacent_find

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    
    void test()
    {
    	vectorv;
    
    	v.push_back(9);
    	v.push_back(3);
    	v.push_back(4);
    	v.push_back(5);
    	v.push_back(5);
    
    	vector::iterator it = adjacent_find(v.begin(),v.end());
    	if (it == v.end())
    	{
    		cout << "没有找到" << endl;
    	}
    	else
    	{
    		cout << "找到了" <<  *it << endl;
    	}
    }
    
    
    int main()
    {
    	test();
    
    	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

    在这里插入图片描述

    binary_search

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    
    void test()
    {
    	vectorv;
    	for (int i = 0;i < 10;i++)
    	{
    		v.push_back(i);
    	}
    	//注意:容器必须是有序的序列
    	bool rat =  binary_search(v.begin(), v.end(), 9);
    	if (rat == true)
    	{
    		cout << "找到了" << endl;
    	}
    	else
    	{
    		cout << "没有找到" << endl;
    	}
    }
    
    
    int main()
    {
    	test();
    
    	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

    在这里插入图片描述

    count

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    
    void test()
    {
    	vectorv;
    	v.push_back(30);
    	v.push_back(50);
    	v.push_back(30);
    	v.push_back(20);
    	v.push_back(50);
    
    	int num = count(v.begin(), v.end(), 50);
    	
    	cout << "找到元素50" << num << 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 test2()
    {
    	//定义容器
    	vectorv1;
    	//创建数据(对象)
    	Person p1("aaa", 20);
    	Person p2("bbb", 30);
    	Person p3("ccc", 40);
    	Person p4("ddd", 50);
    
    	//将数据放入容器
    	v1.push_back(p1);
    	v1.push_back(p2);
    	v1.push_back(p3);
    	v1.push_back(p4);
    
    	//查到对象数据
    	Person p5("ddd", 50);
    	int num = count(v1.begin(), v1.end(), p5);
    	cout << "和ddd同岁的人" << num << endl;
    }
    int main()
    {
    	test2();
    
    	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

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

    count_if

    在这里插入图片描述

    #include
    using namespace std;
    #include
    #include
    #include
    class Greater
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 40;
    	}
    };
    void test()
    {
    	vectorv;
    	v.push_back(30);
    	v.push_back(50);
    	v.push_back(30);
    	v.push_back(20);
    	v.push_back(50);
    
    	int num = count_if(v.begin(), v.end(), Greater());
    	
    	cout << "找到大于40的个数" << 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 GreatAge
    {
    public:
    	bool operator()(Person& p)
    	{
    		return p.m_Age > 20;
    	}
    };
    void test2()
    {
    	//定义容器
    	vectorv1;
    	//创建数据(对象)
    	Person p1("aaa", 20);
    	Person p2("bbb", 30);
    	Person p3("ccc", 40);
    	Person p4("ddd", 50);
    
    	//将数据放入容器
    	v1.push_back(p1);
    	v1.push_back(p2);
    	v1.push_back(p3);
    	v1.push_back(p4);
    
    	int num = count_if(v1.begin(), v1.end(), GreatAge());
    	cout << "岁数大于20的个数" << num << endl;
    }
    int main()
    {
    	test2();
    
    	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

    在这里插入图片描述

  • 相关阅读:
    MQTT-主题基础
    2022年服装进销存软件排行榜重磅出炉!
    grid弹性布局 设置宽高一致
    Python pyintsaller打包异常 type object ‘Callable‘ has no attribute ‘_abc_registry‘
    考研:研究生考试(五天学完)之《线性代数与空间解析几何》研究生学霸重点知识点总结之第二课矩阵及其运算
    【mysql】ssl_choose_client_version:unsupported protocol
    工具类实现导出复杂excel、word
    Java项目硅谷课堂学习笔记-P4前端基础知识2
    简述两种爆破ssh弱口令的方法
    ssm手机销售网站的设计与实现毕业设计-附源码
  • 原文地址:https://blog.csdn.net/qq_45159887/article/details/134495541