第九章(12):STL之常用查找算法

文章目录

  • 前情回顾
  • 常用查找算法
    • find
    • find_if
    • adjacent_find
    • binary_search
    • count
    • count_if
  • 下一座石碑

welcome
✒️博主介绍:一名大一的智能制造专业学生,在学习C/C++的路上会越走越远,后面不定期更新有关C/C++语法,数据结构,算法,Linux,ue5使用,制作游戏的心得,和大家一起共同成长。
✈️C++专栏:C++爬塔日记
博客制作不易,点赞+⭐收藏+➕关注

前情回顾

在上一块石碑中,我知道了如何去常用的遍历算法,同时下一块石碑也显露出来…

  • 上章地址:第九章(11):STL之常用遍历算法

常用查找算法

  • 常用的查找算法有六个:
find//查找元素
find_if//按照条件查找元素
adjacent_find//查找相邻重复元素
binary_search//二分查找法
count//统计元素个数
count_if//按照条件统计元素

find

  • find可以查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
find(beg,end,T value);
  • find按照值查找元素,beg是查找区间的起始迭代器,end为结束迭代器,value是要查找的元素,当这个元素是自定义的数据类型的时候,注意重载==号,才能进行查找

使用:

#include
using namespace std;
#include
#include

void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	vector<int>::iterator b = find(v.begin(), v.end(), 9);
	if (b == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout <<"找到了:" << *b << endl;
	}

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

第九章(12):STL之常用查找算法_第1张图片

find_if

  • find_if是按照条件去找,这个时候就需要使用者去提供谓词或者函数作为条件
find_if(beg,end,_pred);
  • beg是查找区间的起始迭代器,end是结束迭代器,_pred就是条件,使用谓词,这个时候对于自定义类型不用重载==号

使用:

#include
using namespace std;
#include
#include

bool gre(int a)
{
	return a > 8;
}
void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	vector<int>::iterator b = find_if(v.begin(), v.end(), gre);
	if (b == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout <<"找到了:" << *b << endl;
	}

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

第九章(12):STL之常用查找算法_第2张图片

adjacent_find

  • adjacent_find是用来查找相邻重复元素的,查找到会返回相邻元素中第一个元素的迭代器,找不到返回结束迭代器
adjacent_find(beg,end);
  • beg代表查找区间的起始迭代器,end为结束迭代器

使用:

#include
using namespace std;
#include
#include

void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	vector<int>::iterator b = adjacent_find(v.begin(), v.end());
	if (b == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了:" << *b << endl;
	}

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

第九章(12):STL之常用查找算法_第3张图片

binary_search

  • binary_search,二分查找法,查找速度很快,但是又缺陷,只能查找有序序列,与二分查找的底层有关,二分查找在找到元素会返回真,找不到返回假,返回类型为bool
bool binary_search(beg,end,value);
  • beg代表查找区间的起始迭代器,end为结束迭代器,value为查找元素

使用:

#include
using namespace std;
#include
#include

void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	if (binary_search(v.begin(),v.end(),3))
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没有找到"<< endl;
	}

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

第九章(12):STL之常用查找算法_第4张图片

count

  • 统计元素出现的次数
count(beg,end,value);
  • beg代表查找区间的起始迭代器,end为结束迭代器,value为统计元素,同时,对于自定义类型要重载==号

使用:

#include
using namespace std;
#include
#include

void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
		v.push_back(i);
	}
	cout << count(v.begin(), v.end(), 5) << endl;
}
int main()
{
	test1();
	return 0;
}

第九章(12):STL之常用查找算法_第5张图片

count_if

  • 按照条件统计出现的元素,同样条件使用谓词或者函数
count_if(beg,end,_pred);
  • beg代表查找区间的起始迭代器,end为结束迭代器,_pred为谓词或者函数

使用:

#include
using namespace std;
#include
#include

bool gre(int a)
{
	return a > 1;
}
void test1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
		v.push_back(i);
	}
	cout << count_if(v.begin(), v.end(), gre) << endl;
}
int main()
{
	test1();
	return 0;
}

第九章(12):STL之常用查找算法_第6张图片

下一座石碑

  • 这座石碑倒下了,露出了下一座石碑…

预知后事如何,关注新专栏,和我一起征服C++这座巨塔
专栏:C++爬塔日记
都看到这里了,留下你们的点赞+⭐收藏+评论吧

你可能感兴趣的:(C++爬塔日记,c++,算法,开发语言)