STL学习笔记10-查找算法(find,find_if,adjacen_fine,binary_search,binary_search,cout,count_if)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: lower_bound函数是C++ STL中的一个函数,用于在有序的容器中查找第一个大于或等于给定值的元素的迭代器。其函数原型为: ```cpp template<class ForwardIt, class T> ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value); ``` 其中,`first`和`last`是要查找的范围,`value`是要查找的值。返回的是一个迭代器,指向第一个大于或等于`value`的元素。 例如,对于一个有序的vector<int> v,要查找第一个大于等于5的元素,可以这样写: ```cpp auto it = std::lower_bound(v.begin(), v.end(), 5); if (it != v.end()) { std::cout << "First element >= 5 is: " << *it << std::endl; } else { std::cout << "No element >= 5 found" << std::endl; } ``` 如果v中存在大于等于5的元素,那么输出第一个大于等于5的元素的值;否则输出"No element >= 5 found"。 ### 回答2: lower_bound函数是C++标准库中的一种查找算法,用于在有序序列(如数组或容器)中查找第一个大于或等于给定值的元素,并返回其指针或迭代器。 lower_bound函数的使用需要包含<algorithm>头文件,并且传入两个迭代器和一个比较函数对象(可选)作为参数。函数会返回一个指向第一个大于或等于给定值的元素的迭代器,如果没有找到符合条件的元素,则返回指向序列末尾的迭代器。 lower_bound的实现使用的是二分查找binary search)的算法。它通过比较中间元素的值与给定值,不断缩小查找范围,直到找到符合条件的元素或范围缩小到空。在查找过程中,如果中间元素小于给定值,则查找范围会缩小到中间元素的右侧;反之,如果中间元素大于或等于给定值,则查找范围会缩小到中间元素的左侧。 lower_bound函数的时间复杂度为O(logn),其中n为序列的长度。它适用于有序序列中的查找操作,并且通常用于二分查找的实现。 以下是一个使用lower_bound函数的示例: ```cpp #include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> numbers = {1, 2, 4, 4, 6, 8, 10}; // 使用lower_bound查找第一个大于或等于给定值的元素 auto it = std::lower_bound(numbers.begin(), numbers.end(), 5); if (it != numbers.end()) { std::cout << "第一个大于或等于5的元素是:" << *it << std::endl; } else { std::cout << "没有找到符合条件的元素" << std::endl; } return 0; } ``` 输出结果为: ``` 第一个大于或等于5的元素是:6 ``` 以上就是对lower_bound函数的简单介绍和使用示例的回答,希望能对您有所帮助。 ### 回答3: lower_bound函数是C++中的一个算法函数,可以用于查找在一个有序序列中某个值首次出现的位置,或者插入某个值后仍然保持有序的位置。 lower_bound函数接受三个参数:区间的起始迭代器、区间的终止迭代器和要查找的值。 lower_bound函数在查找时使用二分查找算法,在有序序列中找到不小于要查找的值的第一个位置,并返回该位置的迭代器。 如果在序列中找到该值,则lower_bound返回该值在序列中的位置的迭代器。如果在序列中找不到该值,则lower_bound返回大于该值的第一个位置的迭代器。 lower_bound函数的时间复杂度为O(logN),其中N为序列的大小。 lower_bound函数在应用上具有广泛的用途。它可以用于查找某个值在有序数组或容器中的位置,也可以用于判断某个值是否存在于有序序列中。 此外,lower_bound函数还可以用于在有序序列中插入一个元素,使得插入后序列仍然保持有序。可以通过在lower_bound返回的位置插入元素来实现。 总之,lower_bound函数是C++中非常实用的查找和插入算法函数,可以高效地处理有序序列的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值