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;
}