STL算法——常用查找算法(find、find_if、adjacent_find、binary_search、count、count_if)

5.2 常用查找算法

学习目标:

  • 掌握常用的查找算法

算法简介:

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

功能描述:

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

函数原型:

  • find(iterator beg, iterator end, value);
    • 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    • beg 开始迭代器
    • end 结束迭代器
    • value 查找的元素

示例:

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

//查找 find

class Person {
public:
	string m_name;
	int m_age;

	Person(string name, int age) {
		this->m_name = name;
		this->m_age = age;
	}
	
	//重写==操作符,为了底层 find 如何对比
	//可以写入一个元素查找,或者直接输入自定义类型查找
	//看个人需求
	bool operator==(const string name) {
		return this->m_name == name;
	}

};

//内置数据类型查找
void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);
	
	//利用迭代器接收查找到的数据
	vector<int>::iterator it = find(v.begin(), v.end(), 40);

	if (it != v.end()) {
		cout << "找到了:" << *it << endl;
	}
	else {
		cout << "没有找到" << endl;
	}
}

//自定义数据类型查找
void test02() {
	vector<Person> v;

	//创建数据
	Person p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 10);

	//加入容器
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	//查找
	//若不重写,find 不知道比较哪一个元素
	vector<Person>::iterator it = find(v.begin(), v.end(), "李四");

	if (it != v.end()) {
		cout << "找到了,姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << endl;
	}
	else {
		cout << "没找到" << endl;
	}
}


//主函数
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

总结:利用find可以在容器中找到指定的元素,返回值是迭代器

5.2.2 find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred);
    • 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    • beg 开始迭代器
    • end 结束迭代器
    • _Pred 函数或者谓词(返回bool 类型的仿函数)

示例:

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

//按条件查找元素 find_if

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

//写一个谓词(内置数据类型判断)
class GreatThirty {
public:
	bool operator()(int val) {
		return val > 30;
	}
};

//换一种写法,用函数(自定义数据类型判断)
bool DIY_GreatTwenty(const Person& p) {
	return p.m_age > 20;
}


//内置数据类型
void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(50);
	v.push_back(40);
	v.push_back(30);

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreatThirty());

	if (it != v.end()) {
		cout << "找到了 大于30 的元素为:" << (*it) << endl;
	}
	else {
		cout << "没有找到" << endl;
	}
}

//自定义数据类型
void test02() {
	vector<Person> v;

	Person p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 40);

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

	vector<Person>::iterator it = find_if(v.begin(), v.end(), DIY_GreatTwenty);

	if (it != v.end()) {
		cout << "找到了 大于20 的元素,姓名为:" << (*it).m_name << " 年龄为:" << (*it).m_age << endl;
	}
	else {
		cout << "没有找到" << endl;
	}
}

//主函数
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}
5.2.3 adjacent_find

功能描述:

  • 查找相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);
    • 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
    • beg 开始迭代器
    • end 结束迭代器

示例:

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

//查找相邻重复元素 adjacent_find
void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(10);
	v.push_back(30);
	v.push_back(30);
	v.push_back(40);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());

	if (it != v.end()) {
		cout << "找到了相邻重复元素:" << (*it) << endl;
	}
	else {
		cout << "没有找到" << endl;
	}
}


//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
5.2.4 binary_search

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, value);
    • 查找指定元素,查到 返回true,否则false
    • 注意:在无序序列中不可用!,而且只能用于升序序列例如set容器
    • beg 开始迭代器
    • end 结束迭代器
    • value 查找的元素

示例:

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

//二分查找指定元素是否存在 binary_search
void test01() {
	set<int> s;
	s.insert(20);
	s.insert(50);
	s.insert(30);
	s.insert(10);
	s.insert(40);

	if (binary_search(s.begin(), s.end(),50)) {
		cout << "在 set 容器中找到了50" << endl;
	}
	else {
		cout << "在 set 容器中没有找到" << endl;
	}

	//试用 binary_search 查找无序容器
	vector<int> v;
	v.push_back(50);
	v.push_back(40);
	v.push_back(30);
	v.push_back(20);

	if (binary_search(v.begin(), v.end(), 50)) {
		cout << "在 vector 容器中找到了50" << endl;
	}
	else {
		cout << "在 vector 容器中没有找到" << endl;
	}
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}

总结:二分查找法效率很高,值得注意的是查找的容器中元素必须是有序序列

5.2.5 count

功能描述:

  • 统计元素个数

函数原型:

  • count(iterator beg, iterator end, value);
    • 统计元素出现次数
    • beg 开始迭代器
    • end 结束迭代器
    • value 查找的元素

示例:

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

//统计元素个数 count

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

	//重载==符号,方便元素查找
	bool operator==(const int val) {
		return this->m_age == val;
	}
};

//内置数据类型
void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(10);
	v.push_back(40);
	v.push_back(10);

	int sum = count(v.begin(), v.end(), 10);
	cout << "容器中10的个数为:" << sum << endl;
}

//自定义数据类型
void test02() {
	vector<Person> v;

	Person p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 20);
	Person p5("贾七", 20);

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

	cout << "年龄是20岁的人的个数是:" << count(v.begin(), v.end(), 20) << endl;
}

//主函数
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

总结:统计自定义数据类型时候,需要配合operator==

5.2.6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);
    • 按条件统计元素出现次数
    • beg 开始迭代器
    • end 结束迭代器
    • _Pred 谓词

示例:

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

//按条件统计元素个数

//谓词(寻找小于30)
class LessThirty {
public:
	bool operator()(int val) {
		return val < 30;
	}
};

//统计内置数据类型
void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	int sum = count_if(v.begin(), v.end(), LessThirty());

	cout << "小于30的数有 " << sum << " 个" << endl;
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

三月江东

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

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

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

打赏作者

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

抵扣说明:

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

余额充值