• C++STL常用算法合集(上)


    • 算法主要是由<algorithm><functional><numeric>组成
    • <algorithm>是STL头文件中体积最大的一个,涉及比较、交换、查找、遍历、复制、修改等
    • <numeric>体积很小,只包括几个序列上进行简单数学运算的模板函数
    • <functional>定义了一些模板类,用以声明模板函数

    一、常用遍历算法

    一、for_each

    #include<algorithm>
    #include<iostream>
    #include<vector>
    using namespace std;
    void print1(int val)
    {
    	cout << val << " ";
    }
    class print2
    {
    public:
    	void operator()(int val)
    	{
    		cout << val << " ";
    	}
    };
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	for_each(v.begin(), v.end(), print1);
    	cout << endl;
    	for_each(v.begin(), v.end(), print2());
    	cout << endl;
    }
    
    • 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

    使用普通函数和仿函数作为操作函数,for_each遍历容器

    二、transform

    class Transform
    {
    public:
    	int operator()(int n)
    	{
    		return n+100;
    	}
    };
    void test02()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	vector<int>vTarget;
    	vTarget.resize(v.size());
    	transform(v.begin(), v.end(), vTarget.begin(), Transform());
    	for_each(vTarget.begin(), vTarget.end(), print2());
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    定义仿函数提供transform的操作函数,将v的值+100转移到vTarget上,一定要事先开辟空间
    在这里插入图片描述

    二、常用查找算法

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

    一、find

    • 查找指定元素,找到返回元素的迭代器,找不到返回结束迭代器位置
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<string>
    using namespace std;
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	auto pos=find(v.begin(), v.end(),5);
    	if (pos != v.end())
    		cout << "找到了:" << *pos << endl;
    	else
    		cout << "没找到" << endl;
    }
    class Person
    {
    public:
    	string m_name;
    	int m_age;
    	Person(string name, int age)
    	{
    		this->m_age = age;
    		this->m_name = name;
    	}
    	bool operator==(const Person&p)
    	{
    		if (this->m_age == p.m_age&&this->m_name == p.m_name)
    			return true;
    		else
    			return false;
    	}
    };
    void test02()
    {
    	vector<Person>v;
    	Person p[4] = { {"a",11},{"b",22},{"c",33},{"d",44} };
    	for (int i = 0; i < 4; i++)
    		v.push_back(p[i]);
    	auto pos = find(v.begin(), v.end(), p[1]);
    	if (pos != v.end())
    		cout << "找到了,姓名:" << pos->m_name << " 年龄:"<<pos->m_age<<endl;
    	else
    		cout << "没找到" << endl;
    }
    int main()
    {
    	test01();
    	test02();
    }
    
    • 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

    在这里插入图片描述
    通过源码可以看到查找时会有值的比较,对于自定义数据类型需要重载==,否则无法比较
    在这里插入图片描述

    二、find_if

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    using namespace std;
    class Five
    {
    public:
    	bool operator()(int n)
    	{
    		return n > 5;
    	}
    };
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	auto pos = find_if(v.begin(), v.end(), Five());
    	if (pos != v.end())
    		cout << "找到了:" << *pos << endl;
    	else
    		cout << "没找到" << endl;
    }
    class Person
    {
    public:
    	string m_name;
    	int m_age;
    	Person(string name, int age)
    	{
    		this->m_age = age;
    		this->m_name = name;
    	}
    };
    class Twenty
    {
    public:
    	bool operator()(const Person&p)
    	{
    		return p.m_age > 20;
    	}
    };
    void test02()
    {
    	vector<Person>v;
    	Person p[4] = { {"aaa",10},{"bbb",20},{"ccc",30},{"ddd",40} };
    	for (int i = 0; i < 4; i++)
    		v.push_back(p[i]);
    	auto pos=find_if(v.begin(), v.end(), Twenty());
    	if(pos != v.end())
    		cout << "找到了,姓名:" << pos->m_name << " 年龄:"<<pos->m_age<<endl;
    	else
    	cout << "没找到" << endl;
    }
    int main()
    {
    	test01();
    	test02();
    	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

    在这里插入图片描述
    使用与find一样,只不过是查找方法变成了仿函数设置条件,或者函数。

    三、adjacent_find

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    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); 
    	auto pos = adjacent_find(v.begin(), v.end());
    	if (pos == v.end())
    		cout << "为找到相邻元素" << endl;
    	else
    		cout << "找到相邻重复元素:" << *pos << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    返回值是第一个找到的有相邻重复元素的迭代器

    四、binary_search

    二分查找,效率很高,但要注意的的是,查找的数据必须是有序存储的
    其返回值是一个bool类型的值,找到返回true,反之false

    void test02()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	bool ret=binary_search(v.begin(), v.end(), 7);
    	if (ret)
    		cout << "找到了" << endl;
    	else
    		cout << "没找到" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    五、count

    统计元素个数

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<string>
    using namespace std;
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i <= 573; i++)
    		v.push_back(i);
    	v.push_back(33);
    	v.push_back(33);
    	v.push_back(7);
    	int n1 = count(v.begin(), v.end(), 1),
    		n2 = count(v.begin(), v.end(), 7),
    		n3 = count(v.begin(), v.end(), 33);
    	cout << "1的个数:" << n1 << endl
    		<< "7的个数:" << n2 << endl
    		<< "33的个数:" << n3 << endl;
    }
    class Person
    {
    public:
    	int m_age;
    	string m_name;
    	Person(int age, string name)
    	{
    		this->m_age = age;
    		this->m_name = name;
    	}
    	bool operator==(const Person&p)
    	{
    		return this->m_age == p.m_age;
    	}
    };
    void test02()
    {
    	vector<Person>v;
    	Person p[5]=
    	{
    	{10,"猪猪侠"},
    	{10,"Tom and Jerry"},
    	{10,"光头强"},
    	{20,"星游记"},
    	{30,"快乐星猫"}
    	};
    	for (int i = 0; i < 5; i++)
    		v.push_back(p[i]);
    	Person s(10, "喜羊羊");
    	int num=count(v.begin(), v.end(), s);
    	cout << "和喜羊羊同岁数的人员个数为:" << num << endl;
    }
    int main()
    {
    	test01();
    	test02();
    	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

    在这里插入图片描述
    同样对于自定义数据类型的计数需要重载==设置判断条件

    六、count_if

    就是把查找的标准从相等匹配换为一个条件判断

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<string>
    using namespace std;
    class Person
    {
    public:
    	int m_age;
    	string m_name;
    	Person(int age, string name)
    	{
    		this->m_age = age;
    		this->m_name = name;
    	}
    };
    class cmp1
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 5;
    	}
    };
    class cmp2
    {
    public:
    	bool operator()(const Person&p)
    	{
    		return p.m_age > 10;
    	}
    };
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	int n1 = count_if(v.begin(), v.end(), cmp1());
    	cout << "大于5的值的个数:" << n1;
    	cout << endl;
    	vector<Person>vp;
    	Person p[5] =
    	{
    	{10,"猪猪侠"},
    	{10,"Tom and Jerry"},
    	{10,"光头强"},
    	{20,"星游记"},
    	{30,"快乐星猫"}
    	};
    	for (int i = 0; i < 5; i++)
    		vp.push_back(p[i]);
    	int n2 = count_if(vp.begin(), vp.end(), cmp2());
    	cout << "年龄大于10的人员个数为:" << n2 << endl;
    }
    int main()
    {
    	test01();
    	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

    在这里插入图片描述

    三、常用排序算法

    sort:对容器内元素排序
    random_shuffle:洗牌,指定范围内的元素随机调整次序
    merge:容器元素合并,并存储到另一容器中
    reverse:反转指定范围内的元素

    一、sort

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    void Print(int val)
    {
    	cout << val << " ";
    }
    class cmp
    {
    public:
    	bool operator()(int v1, int v2)
    	{
    		return v1 > v2;
    	}
    };
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 5; i++)
    		v.push_back(i);
    	sort(v.begin(), v.end());
    	for_each(v.begin(), v.end(), Print);
    	cout << endl;
    	sort(v.begin(), v.end(), cmp());
    	for_each(v.begin(), v.end(), Print);
    	cout << endl;
    }
    
    • 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

    可以使用仿函数指定排序方式
    在这里插入图片描述

    二、random_shuffle

    随机调整设定范围内数据的顺序
    需要设置时间戳srand((unsigned int)time(NULL))保证随机数的有效性

    #include<time.h>
    void test02()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	random_shuffle(v.begin(), v.end());
    	for_each(v.begin(), v.end(),Print);
    	cout << endl;
    }
    int main()
    {
    	srand((unsigned int)time(NULL));
    	//test01();
    	test02();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    三、merge

    合并的两个容器必须有序
    同时注意,将存储合并值的容器要有足够的空间去容纳。

    void test03()
    {
    	vector<int>v1;
    	vector<int>v2;
    	for (int i = 0; i < 10; i++)
    	{
    		v1.push_back(i);
    		v2.push_back(i*i);
    	}
    	vector<int>vTarget;
    	vTarget.resize(v1.size() + v2.size());
    	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    	for_each(vTarget.begin(), vTarget.end(), Print);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    这里的合并不是相加合并而是将两块数据有序合并到一块空间上

    四、reverse

    void test04()
    {
    	vector<int>v;
    	for (int i = 10; i < 100; i += 10)
    		v.push_back(i);
    	cout << "反转前:" << endl;
    	for_each(v.begin(), v.end(), Print);
    	cout << endl;
    	cout << "反转后:" << endl;
    	reverse(v.begin(),v.end());
    	for_each(v.begin(), v.end(), Print);
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    对指定区间内的数据进行逆序
    在这里插入图片描述

  • 相关阅读:
    企微SCRM营销平台MarketGo-ChatGPT助力私域运营
    【毕业设计】基于单片机的智能鱼缸系统设计与实现 - 嵌入式 物联网 stm32 c51
    深度学习 | MATLAB实现GRU门控循环单元gruLayer参数设定
    CTFHub | Cookie注入,UA注入,Refer注入,过滤空格(利用hackbar插件)
    JAVA宠物医院后台管理系统设计与实现计算机毕业设计Mybatis+系统+数据库+调试部署
    [C++][算法基础]Nim游戏(博弈论)
    windbg查看GDT表的基址和长度 段描述符查分实验 段选择子拆分实验
    4.3每日一题(知全微分求函数本身)
    CNP实现应用CD部署
    朴素贝叶斯算法
  • 原文地址:https://blog.csdn.net/qwer1234mnbv_/article/details/125612412