常用查找算法(一)

算法简介: 

find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找法
count //统计元素个数
count_if //按条件统计元素个数

一、find:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()   

函数原型:

find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素

代码示例:  

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

//常用查找算法 
//find

//查找内置数据类型
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//查找容器是否有5这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
	}
}

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	//重载== 让底层find知道如何对比person数据类型
	bool operator==(const Person& p) 
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	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);

	Person pp("bbb", 20);

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到元素 姓名:"<<it->m_Name<<" 年龄:"<<it->m_Age << endl;
	}
}

int main()
{
	//test01();
	test02();
	return 0;
}

总结: 利用find可以在容器中找指定的元素,返回值是迭代器

二、find_if:按条件查找元素

函数原型:

find_if(iterator beg, iterator end, _Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 函数或者谓词(返回bool类型的仿函数)

代码示例:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

//常用查找算法 find_if
//1.查找内置数据类型
class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());

	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到大于5的数字为:" << *it << endl;
	}
}

//2.查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age; 
};

class Greater30
{
public:
	bool operator()(Person &p)
	{
		return p.m_Age  > 30;
	}
};

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

	//找一个年龄大于30的人
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater30());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到元素 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

int main()
{
	test01();
    test02();
	return 0;
}

三、adjacent_find:查找相邻重复元素

函数原型:

adjacent_find(iterator beg, iterator end);
// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
// beg 开始迭代器
// end 结束迭代器

代码示例:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

//常用查找算法 adjacent_find
void test()
{
	vector<int>v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(3);
	v.push_back(1);
	v.push_back(4);
	v.push_back(3);
	v.push_back(3);

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

	if (pos == v.end())
	{
		cout << "未找到相邻重复元素" << endl;
	}
	else
	{
		cout << "找到重复元素:" << *pos << endl;
	}	
}

int main()
{
	test();
	return 0;
}

相关推荐

  1. 查找算法()

    2024-04-08 20:58:01       17 阅读
  2. 入门算法

    2024-04-08 20:58:01       42 阅读
  3. STL——算法

    2024-04-08 20:58:01       13 阅读
  4. 算法

    2024-04-08 20:58:01       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-08 20:58:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-08 20:58:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-08 20:58:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-08 20:58:01       20 阅读

热门阅读

  1. day31 算法 贪心算法1

    2024-04-08 20:58:01       16 阅读
  2. k8s删除namespace失败一直处于Terminating状态

    2024-04-08 20:58:01       19 阅读
  3. RIPv1和RIPv2的区别

    2024-04-08 20:58:01       14 阅读
  4. Mysql底层原理一:事务

    2024-04-08 20:58:01       14 阅读
  5. 秒验:让APP验证和登录远不只是便捷

    2024-04-08 20:58:01       15 阅读
  6. 【C++ STL】简述

    2024-04-08 20:58:01       14 阅读
  7. 树莓派的简单应用场景

    2024-04-08 20:58:01       13 阅读
  8. 在c语言中,输出100以内的奇数

    2024-04-08 20:58:01       12 阅读
  9. zookeeper介绍

    2024-04-08 20:58:01       11 阅读
  10. C++ //练习 11.24 下面的程序完成什么功能?

    2024-04-08 20:58:01       12 阅读
  11. 2024-4-7

    2024-04-08 20:58:01       16 阅读