C++ STL(十三):常用查找算法(find、find_if、adjacent_find、binary_search、count、count_if)


0 常用查找算法简介【find、find_if、adjacent_find、binary_search、count、count_if】

算法简介
find:查找指定元素是否存在。
find_if按条件查找元素是否存在。
adjacent_find:查找相邻且重复的元素
binary_search:二分查找判断元素是否存在。
count :统计元素个数。
count_if按条件统计元素个数。


1 find【查找指定元素是否存在】

作用:查找指定元素是否存在:存在则返回第1次匹配指定元素的迭代器位置;不存在则返回结束迭代器end()

注1:使用find算法时,需包含头文件include <algorithm>
注2:查找自定义数据类型的元素时,需在自定义数据类型的类中,重载运算符operator==,告知find算法应如何比较两个自定义数据类型的对象。

函数原型
find(iterator begin, iterator end, value);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置;
value:待查找的元素。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用find算法

//查找内置数据类型元素
void func1() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

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

	if (pos != v.end()) {
    
    
		cout << "目标元素存在:" << *pos << endl;	//6
	}
	else {
    
    
		cout << "目标元素不存在.." << endl;
	}
}


class Person {
    
    
public:
	string name;
	int age;

	Person(string name, int age) {
    
    
		this->name = name;
		this->age = age;
	}

	//重载operator==运算符
	bool operator==(const Person& p) {
    
    
		return this->name == p.name && this->age == p.age;
	}
};

//查找自定义数据类型元素,需重载operator==运算符
void func2() {
    
    
	vector<Person> v;
	Person p1("Tom", 16);
	Person p2("Jerry", 18);
	Person p3("Jack", 20);
	Person p4("Lucy", 22);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	
	Person person("Lucy", 22);

	vector<Person>::iterator pos = find(v.begin(), v.end(), person);

	if (pos != v.end()) {
    
    
		cout << "目标元素存在:" << endl;
		cout << "姓名:" << (*pos).name << ",年龄:" << pos->age << endl;
	}
	else {
    
    
		cout << "目标元素不存在.." << endl;
	}
}

int main() {
    
    
	func1();
	func2();

	return 0;
}

2 find_if【按条件查找元素是否存在】

作用按条件查找元素是否存在:存在则返回第1次匹配指定条件的迭代器位置;不存在则返回结束迭代器end()

注1:使用find_if算法时,需包含头文件include <algorithm>
注2:查找自定义数据类型的元素时,需指定查询条件:①普通回调函数;②谓词(返回类型为bool的仿函数);③匿名函数(lambda表达式)。
注3:按查询使用find,按条件查询使用find_if

函数原型
find_if(iterator begin, iterator end, _Pred);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置;
_Pred:指定查询的条件。
①普通回调函数;
谓词(返回类型为bool的仿函数);
③匿名函数(lambda表达式)。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用find_if算法

//回调函数
bool lessThanFive(int val) {
    
    
	return val < 5;
}

//谓词(返回类型为bool的仿函数/函数对象)
class LessThanFive {
    
    
public:
	bool operator()(int val) {
    
    
		return val < 5;
	}
};

//查找内置数据类型元素
void func1() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

	//回调函数
	//vector<int>::iterator pos = find_if(v.begin(), v.end(), lessThanFive);
	
	//谓词
	//vector<int>::iterator pos = find_if(v.begin(), v.end(), LessThanFive());
	
	//匿名函数(lambda表达式)
	vector<int>::iterator pos = find_if(v.begin(), v.end(), [](int val) {
    
    return val < 5;});

	if (pos != v.end()) {
    
    
		cout << "目标元素存在:" << *pos << endl;	//1
	}
	else {
    
    
		cout << "目标元素不存在.." << endl;
	}
}


class Person {
    
    
public:
	string name;
	int age;

	Person(string name, int age) {
    
    
		this->name = name;
		this->age = age;
	}
};

//回调函数
bool greaterThan20(const Person &p) {
    
    
	return p.age > 20;
}

//谓词(返回类型为bool的仿函数/函数对象)
class GreaterThan20 {
    
    
public:
	bool operator()(const Person& p) {
    
    
		return p.age > 20;
	}
};

//查找自定义数据类型元素,需重载operator==运算符
void func2() {
    
    
	vector<Person> v;
	Person p1("Tom", 16);
	Person p2("Jerry", 18);
	Person p3("Jack", 20);
	Person p4("Lucy", 22);

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

	//回调函数
	//vector<Person>::iterator pos = find_if(v.begin(), v.end(), greaterThan20);

	//谓词
	//vector<Person>::iterator pos = find_if(v.begin(), v.end(), GreaterThan20());

	//匿名函数(lambda表达式)
	vector<Person>::iterator pos = find_if(v.begin(), v.end(), [](const Person &p) {
    
    return p.age > 20; });

	if (pos != v.end()) {
    
    
		cout << "目标元素存在:" << endl;
		cout << "姓名:" << (*pos).name << ",年龄:" << pos->age << endl;	//姓名:Lucy,年龄:22
	}
	else {
    
    
		cout << "目标元素不存在.." << endl;
	}
}

int main() {
    
    
	//func1();
	func2();

	return 0;
}

3 adjacent_find【查找相邻且重复的元素】

作用:查找相邻且重复的元素是否存在:存在则返回相邻且重复元素中第1个元素的迭代器位置;不存在则返回结束迭代器end()

函数原型
adjacent_find(iterator begin, iterator end);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用adjacent_find算法

//查找相邻且重复元素
void func1() {
    
    
	vector<int> v;

	v.push_back(9);	//重复元素,但不相邻
	v.push_back(1);
	v.push_back(9);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);	//相邻且重复元素
	v.push_back(3);

	//adjacent_find算法
	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos != v.end()) {
    
    
		cout << "存在相邻且重复元素:" << *pos << endl;	//3
	}
	else {
    
    
		cout << "不存在相邻且重复元素.." << endl;
	}
}

int main() {
    
    
	func1();

	return 0;
}

4 binary_search【二分查找法判断指定元素是否存在】

作用二分查找法判断指定元素是否存在:存在则返回true;不存在则返回false

注1:binary_search算法的查找效率高,但必须对有序序列使用,无序序列中不可用。
注2:使用binary_search算法时,需包含头文件include <algorithm>

函数原型
bool binary_search(iterator begin, iterator end, value);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置;
value:待查找的元素。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用binary_search算法

//对有序序列使用二分查找
int main() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

	//只能对有序序列使用二分查找
	sort(v.begin(), v.end());

	bool flag = binary_search(v.begin(), v.end(), 6);
	cout << (flag ? "存在" : "不存在") << endl;	//存在

	return 0;
}

5 count【统计指定元素的个数】

作用:统计指定元素的个数。

注1:使用count算法时,需包含头文件include <algorithm>
注2:统计自定义数据类型元素的个数时,需在自定义数据类型的类中,重载运算符operator==,告知count算法应如何比较两个自定义数据类型的对象。

函数原型
count(iterator begin, iterator end, value);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置;
value:待查找的元素。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用count算法

//统计内置数据类型元素的个数
void func1() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(1);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

	int total = count(v.begin(), v.end(), 1);
	cout << "元素1的个数:" << total << endl;	//3
}

class Person {
    
    
public:
	string name;
	int age;

	Person(string name, int age) {
    
    
		this->name = name;
		this->age = age;
	}

	//重载operator==运算符
	//与目标对象age属性相等
	bool operator==(const Person& p) {
    
    
		return this->age == p.age;
	}
};

//统计自定义数据类型元素的个数
void func2() {
    
    
	vector<Person> v;
	Person p1("Tom", 16);
	Person p2("Jerry", 18);
	Person p3("Jack", 22);
	Person p4("Lucy", 22);

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

	Person person("Michael", 22);

	int total = count(v.begin(), v.end(), person);
	cout << "与Michael同龄的Person对象个数:" << total << endl;	//2
}

int main() {
    
    
	func1();
	func2();

	return 0;
}

6 count_if【按条件统计指定元素的个数】

作用按条件统计指定元素的个数。

注1:使用count_if算法时,需包含头文件include <algorithm>
注2:按统计使用count,按条件统计使用count_if

函数原型
count_if(iterator begin, iterator end, _Pred);

参数解释
begin:迭代器起始位置;
end:迭代器结束位置;
_Pred:指定统计的条件。
①普通回调函数;
谓词(返回类型为bool的仿函数);
③匿名函数(lambda表达式)。

示例

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用count_if算法

//回调函数
bool lessThanFive(int val) {
    
    
	return val < 5;
}

//谓词(返回类型为bool的仿函数/函数对象)
class LessThanFive {
    
    
public:
	bool operator()(int val) {
    
    
		return val < 5;
	}
};

//按条件统计内置数据类型元素的个数
void func1() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

	/* 统计值小于5的元素 */
	//回调函数
	//int total = count_if(v.begin(), v.end(), lessThanFive);

	//谓词
	//int total = count_if(v.begin(), v.end(), LessThanFive());

	//匿名函数(lambda表达式)
	int total = count_if(v.begin(), v.end(), [](int val) {
    
    return val < 5; });

	cout << "值小于5的元素个数:" << total << endl;		//2
}


class Person {
    
    
public:
	string name;
	int age;

	Person(string name, int age) {
    
    
		this->name = name;
		this->age = age;
	}
};

//回调函数
bool ageGreaterThan20(const Person& p) {
    
    
	return p.age > 20;
}

//谓词(返回类型为bool的仿函数/函数对象)
class AgeGreaterThan20 {
    
    
public:
	bool operator()(const Person& p) {
    
    
		return p.age > 20;
	}
};

//按条件统计自定义数据类型元素的个数
void func2() {
    
    
	vector<Person> v;
	Person p1("Tom", 16);
	Person p2("Jerry", 18);
	Person p3("Jack", 20);
	Person p4("Lucy", 22);

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

	//回调函数
	//int total = count_if(v.begin(), v.end(), ageGreaterThan20);

	//谓词
	//int total = count_if(v.begin(), v.end(), AgeGreaterThan20());

	//匿名函数(lambda表达式)
	int total = count_if(v.begin(), v.end(), [](const Person& p) {
    
    return p.age > 20; });

	cout << "年龄大于20的Person对象个数:" << total << endl;	//1
}

int main() {
    
    
	//func1();
	func2();

	return 0;
}

猜你喜欢

转载自blog.csdn.net/newson92/article/details/114242770
今日推荐