【44C++STL-常用算法----2、常用查找算法】

14.2 常用查找算法

学习目标: 掌握常用的查找算法

算法简介:

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

功能描述:

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

函数原型:

  • find(iterator beg,iterator end, value);

​ //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置end()

​ //beg 开始迭代器

​ //end 结束迭代器

​ //value 查找的元素

示例代码:

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

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

	//查找 容器中是否有5 这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 50);
	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 test32()
{
	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);

	Person pp("ccc", 30);


	//创建迭代器遍历容器中数据,查找p3
	vector<Person>::iterator it = find(v.begin(), v.end(),pp);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << " 找到元素  姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}
}

int main()
{
	//test31();
	test32();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

总结:

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

14.2.2 find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _pred);

​ //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

​ //beg 开始迭代器

​ //end 结束迭代器

​ //_Pred 函数或者谓词(返回bool类型的仿函数)

示例代码:

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>
//常用查找算法 find_if

//1、查找内置数据类型
class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val>5;
	}
};
void test41()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

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

	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到大于5的数据为:" << *it << endl;
	}
}

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

	string m_name;
	int m_age;
};

class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_age > 20;
	}
};

void test42()
{
	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 << "找到姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}
}

int main()
{
	//test41();
	test42();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

14.2.3 adjacent_find

功能描述:

  • 操作相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);

​ //查找相邻重复元素,返回相邻元素的第一个位置的迭代器

​ //beg 开始迭代器

​ //end 结束迭代器

示例代码:

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

//常用查找算法 adjacent_find
void test51()
{
	vector<int>v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(3);
	v.push_back(2);
	v.push_back(1);
	v.push_back(2);
	v.push_back(2);

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

	if (pos == v.end())
	{
		cout << "未找到相邻重复元素" << endl;
	}
	else
	{
		cout << "找到相邻重复元素" << *pos << endl;
	}
}

int main()
{
	test51();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

总结: 该算法在实际应用开发中用到不多,但会在面试题中出现,面试官会考察你对STL的掌握程度。

14.2.4 binary_search

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, value);

​ //查找指定的元素,查到 返回true,否则false

​ //注意:在无序序列中不可用

​ //beg 开始迭代器

​ //end 结束迭代器

​ //value 查找的元素

示例代码:

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

//常用查找算法  binary_search
void test61()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//v.push_back(5);

	//二分查找容器中是否有8
	//注意:容器必须是有序的序列,否则结果的正确性未知

	bool ret = binary_search(v.begin(), v.end(), 8);
	if (ret)
	{
		cout << "查找到该元素" << endl;
	}
	else
	{
		cout << "没有找到" << endl;
	}
}

int main()
{
	test61();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

总结: 二分查找法查找效率很高,值得注意的是查找的容器中元素必须是有序序列的,否则结果正确性难以保证。

14.5.5 count

功能描述:

  • 统计元素个数

函数原型:

  • int count(iterator beg, iterator end, value);

​ //统计元素出现次数

​ //beg 开始迭代器

​ //end 结束迭代器

​ //value 统计的元素

**示例代码: **

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

//常用查找算法 count
//1、统计内置数据类型

void test71()
{
	vector<int>v;
	v.push_back(20);
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);

	int num = count(v.begin(), v.end(), 20);

	cout << "20的个数为:" << num<<endl;
}

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

	bool operator==(const Person& p)
	{
		if (this->m_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_name;
	int m_age;
};
void test72()
{
	vector<Person>v;

	Person p1("刘备", 25);
	Person p2("张飞", 35);
	Person p3("赵云", 25);
	Person p4("曹操", 22);
	Person p5("关羽", 25);
	Person pp("诸葛亮", 35);

	//将数据添加到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count(v.begin(), v.end(), pp);

}

int main()
{
	test71();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

14.2.6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg,iterator end, _pred);

​ //按条件统计元素出现次数

​ //beg 开始迭代器

​ //end 结束迭代器

​ //_pred 谓词

示例代码:

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

//常用查找算法  count_if
//1、统计内置数据类型
class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};
void test81()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(10);
	v.push_back(30);
	v.push_back(20);
	v.push_back(10);

	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "大于20元素个数为:" << num << endl;
}

//2、统计自定义数据类型个数
class Person
{
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	string m_name;
	int m_age;
};

class AgeGreater20
{
public:
	bool operator()(const Person& p)
	{
		return p.m_age > 20;
	}
};
void test82()
{
	vector<Person>v;

	Person p1("张飞", 35);
	Person p2("刘备", 20);
	Person p3("曹操", 35);
	Person p4("关羽", 40);
	Person p5("诸葛亮", 35);

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

	int num = count_if(v.begin(), v.end(), AgeGreater20());
	cout << "大于20岁的人员个数为:" << num << endl;
}
int main()
{
	//test81();
	test82();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

总结: 按值统计用count,按条件统计用count_if

如果对你有帮助的话,请不要忘了给我一点点点…支持 ( ^ o ^)/~

上一篇:【43C++STL-常用算法----1、常用遍历算法】

下一篇:【45C++STL-常用算法----3、常用排序算法】

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值