常见的查找算法
学习目标:
- 掌握常用的查找算法
算法简介:
find
//查找元素find_if
//按条件查找元素adjacent_find
//查找相邻重复元素binary_search
//二分查找法count
//统计元素个数count_if
//按条件统计元素个数
find
功能描述:
- 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器 end()
函数原型:
find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
示例:
#include <algorithm>
#include <vector>
#include <string>
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_Name = name;
this->m_Age = age;
}
//重载==
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
public:
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);
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(), p2);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
总结: 利用 find 可以在容器中找指定的元素,返回值是迭代器
find_if
功能描述:
- 按条件查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器 end()
函数原型:
find_if(iterator beg, iterator end, function);
// 按条件查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// function 查找的条件,是一个函数对象,返回值为 bool 类型,函数原型为 bool function(value)
示例:
#include <algorithm>
#include <vector>
#include <string>
class Person {
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
//重载==
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
public:
string m_Name;
int m_Age;
};
bool isPerson(const Person& p)
{
if (p.m_Age > 20)
{
return true;
}
return false;
}
void test03() {
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);
vector<Person>::iterator it = find_if(v.begin(), v.end(), isPerson);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
总结: 利用 find_if 可以在容器中按条件查找元素,返回值是迭代器
adjacent_find
功能描述:
- 查找相邻重复元素,找到返回指定元素的迭代器,找不到返回结束迭代器 end()
函数原型:
adjacent_find(iterator beg, iterator end);
// 查找相邻重复元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
示例:
#include <algorithm>
#include <vector>
#include <string>
void test04() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
v.push_back(5);
v.push_back(5);
v.push_back(5);
v.push_back(5);
v.push_back(5);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到重复元素:" << *it << endl;
}
}
总结: 利用 adjacent_find 可以在容器中查找相邻重复元素,返回值是迭代器
binary_search
功能描述:
- 二分查找法,查找指定元素,找到返回 true,找不到返回 false
函数原型:
binary_search(iterator beg, iterator end, value);
// 二分查找法,查找指定元素,找到返回 true,找不到返回 false
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
示例:
#include <algorithm>
#include <vector>
#include <string>
void test05() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
//查找容器中是否有 5 这个元素
bool b = binary_search(v.begin(), v.end(), 5);
if (b)
{
cout << "找到!" << endl;
}
else
{
cout << "没有找到!" << endl;
}
}
总结: 利用 binary_search 可以在容器中二分查找指定元素,返回值是bool类型,true 表示找到,false 表示没找到。
count
功能描述:
- 统计元素个数,返回元素个数
函数原型:
count(iterator beg, iterator end, value);
// 统计元素个数,返回元素个数
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
示例:
#include <algorithm>
#include <vector>
#include <string>
void test06() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
//统计容器中有几个 5
int n = count(v.begin(), v.end(), 5);
cout << "有" << n << "个5" << endl;
}
总结: 利用 count 可以在容器中统计指定元素的个数,返回值是int类型。
count_if
功能描述:
- 按条件统计元素个数,返回元素个数
函数原型:
count_if(iterator beg, iterator end, function);
// 按条件统计元素个数,返回元素个数
// beg 开始迭代器
// end 结束迭代器
// function 查找的条件,是一个函数对象,返回值为 bool 类型,函数原型为 bool function(value)
示例:
#include <algorithm>
#include <vector>
#include <string>
class Person {
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
//重载==
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
public:
string m_Name;
int m_Age;
};
bool isPerson(const Person& p)
{
if (p.m_Age > 20)
{
return true;
}
return false;
}
void test07() {
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);
int n = count_if(v.begin(), v.end(), isPerson);
cout << "有" << n << "个年龄大于20岁的" << endl;
}
总结: 利用 count_if 可以在容器中按条件统计元素的个数,返回值是int类型。