15.2 常用查找算法
学习目标:
- 掌握常用的查找算法
算法简介:
- find //查找元素
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找法
- count //统计元素个数
- count_if //按条件统计元素个数
15.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 Person& p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
return false;
}
};
//内置数据类型查找
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(), p2);
if (it != v.end())
{
cout << "找到了,姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << endl;
}
else
{
cout << "没找到" << endl;
}
}
//主函数
int main()
{
test01();
test02();
system("pause");
return 0;
}
15.2.2 find_if 查找算法
功能描述:
- 按条件(如>5的元素)查找元素
函数原型:
-
find_if(iterator beg, iterator end, _Pred);
//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置- beg 开始迭代器
- end 结束迭代器
- _Pred 函数或者谓词(返回bool 类型的仿函数)
测试dome:
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 Person& p)
{
if (p.m_age == this->m_age && p.m_name == this->m_name)
return true;
else
return false;
}
};
class Greatfive01
{
public:
//一元谓词
bool operator()(int value)
{
return value > 5;
}
};
class Greatfive02
{
public:
//一元谓词
bool operator()(const Person& p)
{
return p.m_age > 18;
}
};
//内置数据类型查找
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_if(v.begin(), v.end(), Greatfive01());
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_if(v.begin(), v.end(), Greatfive02());
if (it != v.end()) {
cout << "找到了,姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << endl;
}
else {
cout << "没找到" << endl;
}
}
//主函数
int main() {
test01();
test02();
system("pause");
return 0;
}
15.2.3 adjacent_find 查找算法
功能描述:
- 查找相邻重复元素
函数原型:
- adjacent_find(iterator beg, iterator end);
查找相邻重复元素,返回相邻元素的第一个位置的迭代器- beg 开始迭代器
- end 结束迭代器
示例:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class Person {
public:
string m_name;
int m_age;
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
//重写==操作符,为了底层 adjacent_find 如何对比
//可以写入一个元素查找,或者直接输入自定义类型查找
//看个人需求
bool operator==(const Person& p)
{
if (p.m_age == this->m_age && p.m_name == this->m_name)
return true;
else
return false;
}
};
//内置数据类型查找
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);//相邻重复元素
v.push_back(30);
v.push_back(40);
v.push_back(50);
//利用迭代器接收查找相邻数据的数据
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
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(p3);
v.push_back(p4);
//查找
//若不重写,adjacent_find 不知道比较哪一个元素
//vector<Person>::iterator it = find_if(v.begin(), v.end(), Greatfive02());
vector<Person>::iterator it = adjacent_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;
}
15.2.4 binary_search 二分查找算法
功能描述:
- 查找指定元素是否存在
- 有序序列(升序)找到了返回true,找不到返回false
- 降序不可用,存在但为降序,返回找不到false
- 无序序列不可用,结果未知
函数原型:
- bool binary_search(iterator beg, iterator end, value);
查找指定元素,查到返回true,否则false- beg 开始迭代器
- end 结束迭代器
- value 查找的元素
注意:在无序序列中不可用!,而且只能用于升序序列例如set容器
测试demo:
void Myprint(int value)
{
cout << value << " ";
}
//内置数据类型查找
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);
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), Myprint);
cout << endl;
//返回二分查找的结果
bool ret = binary_search(v.begin(), v.end(), 50);
if (ret)
cout << "1找到了:" << endl;
else
cout << "1没有找到" << endl;
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), Myprint);
cout << endl;
//利用迭代器接收查找到的数据
ret = binary_search(v.begin(), v.end(),20);
if (ret)
cout << "2找到了:" << endl;
else
cout << "2没有找到" << endl;
}
15.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 Person&p) {
return this->m_age == p.m_age;
}
};
//内置数据类型
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(), p2) << endl;
}
//主函数
int main() {
test01();
test02();
system("pause");
return 0;
}
15.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;
//统计元素个数 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 Person&p) {
return this->m_age == p.m_age;
}
};
class Grate20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
class Grate30
{
public:
bool operator()(const Person& p)
{
return p.m_age > 30;
}
};
//内置数据类型
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_if(v.begin(), v.end(),Grate20() );
cout << "容器中大于20的个数为:" << sum << endl;
}
//自定义数据类型
void test02() {
vector<Person> v;
Person p1("张三", 10);
Person p2("李四", 20);
Person p3("王五", 30);
Person p4("赵六", 40);
Person p5("贾七", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
cout << "年龄大于30岁的人的个数是:" << count_if(v.begin(), v.end(),Grate30()) << endl;
}
//主函数
int main() {
test01();
test02();
system("pause");
return 0;
}