STL——查找算法

算法简介:

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

1.find

函数原型:

  • 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);
	}
	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;
	}
	//重载 == 让底层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);
	Person p5("eee", 50);
	//导入数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	Person pp("ccc", 30);
	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到了:name = " << (*it).m_name << " age = " << it->m_age << endl;
	}
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

2.find_if

函数原型:

  • find_if(iterator beg, iterator end, _Pred);——// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置;beg 开始迭代器;end 结束迭代器; _Pred 函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//find_if——查找算法
class GreatFive
{
public:
	bool operator ()(int val)
	{
		if (val > 5)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};
//查找内置数据类型
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(), GreatFive());
	if (it == v.end())
	{
		cout << "没有找到大于5的数:" << endl;
	}
	else
	{
		cout << "找到了:" << *it << endl;
	}
}
//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
class Great20
{
public:
	bool operator()(Person p)
	{
		return p.m_age > 20;
	}
};
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);
	//找年龄大于20岁的人
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到了:name = " << it->m_name << " age = " << it->m_age << endl;
	}
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

3.adjacent_find

函数原型:

  • adjacent_find(iterator beg, iterator end);——// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器;beg 开始迭代器;end 结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//adjacent_find——查找相邻重复元素
void test01()
{
	vector<int>v;
	v.push_back(1);
	v.push_back(5);
	v.push_back(3);
	v.push_back(9);
	v.push_back(6);
	v.push_back(6);
	v.push_back(8);
	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()
{
	test01();
	system("pause");
	return 0;
}

4.binary_search

函数原型:

  • bool binary_search(iterator beg, iterator end, value);——// 查找指定的元素,查到 返回true 否则false;注意: 在无序序列中不可用; beg 开始迭代器; end 结束迭代器;value 查找的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//binary_search——查找容器中指定元素是否存在(有序序列)
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//v.push_back(2);    如果是无序序列,结果未知!
	//查找容器中是否有9元素
	//注意:容器必须是有序序列
	bool ret = binary_search(v.begin(), v.end(), 9);
	if (ret)
	{
		cout << "找到了!" << endl;
	}
	else
	{
		cout << "没有找到!" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

注:二分查找法效率很高,但查找容器中元素必须为有序序列。

5.count

函数原型:

  • count(iterator beg, iterator end, value)——// 统计元素出现次数;beg 开始迭代器;end 结束迭代器; value 统计的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//count——查找算法
//查找内置数据类型
void test01()
{
	vector<int>v;
	v.push_back(20);
	v.push_back(50);
	v.push_back(90);
	v.push_back(40);
	v.push_back(50);
	v.push_back(80);
	v.push_back(50);
	int num = count(v.begin(), v.end(), 50);
	cout << "容器中5的个数为:" << num << endl;
}
//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	bool operator ==(const Person &p)
	{
		if (p.m_age == this->m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_name;
	int m_age;
};
void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("aaa", 25);
	Person p2("bbb", 45);
	Person p3("ccc", 25);
	Person p4("ddd", 60);
	Person p5("eee", 25);
	Person p6("fff", 16);
	Person p7("ggg", 25);
	//导入数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	v.push_back(p6);
	v.push_back(p7);
	Person c("compare", 25);
	int num = count(v.begin(), v.end(), c);
	cout << "与compare年龄相同的人个数:" << num << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

注:统计自定义数据类型时需要配合重载operator==

6.count_if

函数原型:

  • count_if(iterator beg, iterator end, _Pred);——// 按条件统计元素出现次数;beg 开始迭代器;end 结束迭代器; _Pred 谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//count_if——条件查找
//查找内置数据类型
class Greater30
{
public:
	bool operator()(int val)
	{
		return val > 30;
	}
};
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(50);
	v.push_back(80);
	v.push_back(60);
	v.push_back(30);
	v.push_back(40);
	//查找大于30的数的个数
	int num = count_if(v.begin(), v.end(), Greater30());
	cout << "大于30的数的个数:" << num << endl;
}
//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
class ageGreater18
{
public:
	bool operator()(Person& p)
	{
		return p.m_age > 18;
	}
};
void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("aaa", 15);
	Person p2("bbb", 45);
	Person p3("ddd", 18);
	Person p4("fff", 60);
	Person p5("jjj", 85);
	//导入数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	//查找年龄大于18的人的个数
	int num = count_if(v.begin(), v.end(), ageGreater18());
	cout << "年龄大于18的人数:" << num << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

 

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值