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;
}

使用普通函数和仿函数作为操作函数,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;
}

定义仿函数提供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();
}

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

二、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;
}

在这里插入图片描述
使用与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;
}

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

四、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;
}

在这里插入图片描述

五、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;
}

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

六、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;
}

在这里插入图片描述

三、常用排序算法

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;
}

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

二、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();
}

在这里插入图片描述

三、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);
}

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

四、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;
}

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

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值