【C++】学习笔记[二十]

这篇博客介绍了C++ STL中的常见算法,包括for_each用于遍历容器,transform进行元素转换,find和find_if进行查找操作,adjacent_find查找相邻重复项,binary_search检查元素存在,以及count和count_if进行元素计数。文章通过实例代码展示了每个算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL常用算法【遍历、查找和统计】

1、for_each

  • 实现遍历容器

练习代码:

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

void print01(int val)
{
	cout << val <<" ";
}

class print02
{
public:
	void operator()(int val)
	{
		cout << val <<" ";
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}

	//普通函数
	for_each(v.begin(), v.end(), print01);

	cout << endl;

	//仿函数
	for_each(v.begin(), v.end(), print02());
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

2、transform

  • 搬运容器到另一个容器中

练习代码:

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

class Transform
{
public:
	int operator()(int val)
	{
		return val*10;
	}
};

class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}

	vector<int> vtarget;//目标容器
	vtarget.resize(v.size());//目标容器需要提前开辟空间

	transform(v.begin(), v.end(), vtarget.begin(), Transform());
	for_each(vtarget.begin(), vtarget.end(), MyPrint());
	cout << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

3、find

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

练习代码:

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

//查找内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	//查找容器中是否有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_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;
		}
	}

	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);
	Person pp("bbb", 20);

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

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了!" << endl;
		cout << "naem:" << it->m_name << " " << "age:" << it->m_age << " ";
	}
	cout << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}

在这里插入图片描述

4、find_if

  • 按条件查找元素

练习代码:

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

//内置数据类型练习
class GreaterSix
{
public:
	bool operator()(int val)
	{
		return val > 6;
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}

	//查找有没有大于6的数
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterSix());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了大于6的数字为:" << *it << endl;
	}
}

//自定义数据类型练习
class Person
{
public:
	Person(string name, int age)
	{
		this->m_age = age;
		this->m_name = name;
	}

	//重载 == 让底层知道如何比较自定义数据类型
	

	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 << "找到了" << endl;
		cout << "name:" << it->m_name << " " << "age:" << it->m_age << endl;
	}
}

int main()
{
	test01();
	test02();
	return 0;
}

在这里插入图片描述

5、adjacent_find

  • 查找相邻重复元素

练习代码:

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

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	v.push_back(10);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了:" << *it << endl;
	}
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

6、binary_search

  • 查找指定元素是否存在
  • 在无序序列中不可用

练习代码:

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

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}

	//看看5这个元素是不是存在
	//需要注意的是该容器必须是有序序列
	//如果是无序序列,结果未知
	bool ret = binary_search(v.begin(), v.end(), 5);
	if (!ret)
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" <<endl;
	}
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

7、count

  • 统计元素个数

练习代码:

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

//内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	v.push_back(10);

	int cnt = count(v.begin(), v.end(), 10);
	cout << "10的元素个数为:" << cnt << endl;
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}

	//operator==
	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("aaa", 12);
	Person p2("bbb", 32);
	Person p3("ccc", 12);
	Person p4("ddd", 52);

	Person p("aaa", 12);

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

	//和p同岁数的人有多少个
	int cnt = count(v.begin(), v.end(), p);
	cout <<"和p同岁的人有:"<< cnt << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}

在这里插入图片描述

8、count_if

  • 按条件统计元素个数
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;

//内置数据类型
class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	v.push_back(30);
	v.push_back(50);

	int cnt = count_if(v.begin(), v.end(), Greater20());
	cout << "大于20的元素个数为:" << cnt << 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("aaa", 12);
	Person p2("bbb", 32);
	Person p3("ccc", 12);
	Person p4("ddd", 52);

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

	//大于20岁的人有多少个
	int cnt = count_if(v.begin(), v.end(), AgeGreater20());
	cout <<"大于20岁的人有:"<< cnt << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}

在这里插入图片描述
🤣🤣🤣
排序、拷贝和替换算法见下篇笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值