STL常用查找算法介绍

adjacent_find()

在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_adjacent_find()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(5);

	vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
	if (it == v1.end())
	{
		cout << "没有找到 重复的元素" << endl;
	}
	else
	{
		cout << *it << endl;
	}
	// 2
	int index = distance(v1.begin(), it);
	cout << index << endl;
	// 1

}

int main()
{
	play_adjacent_find();

	return 0;
}

binary_search()

在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

// binary_search是对排序好的进行查找
void play_binary_search()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	v1.push_back(7);
	v1.push_back(9);

	bool b = binary_search(v1.begin(), v1.end(), 7);
	if (b) {
		cout << "find success\n";
	}
	else {
		cout << "find fail\n";
	}
	// find success

}

int main()
{
	play_binary_search();

	return 0;
}

count()

利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_count()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(3);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	int cnt = count(v1.begin(), v1.end(), 3);
	cout << "count of 3: " << cnt << endl;
	// count of 3: 3

}

int main()
{
	play_count();

	return 0;
}

count_if()

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
	return a > 3;
}

void play_count_if()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	int cnt = count_if(v1.begin(), v1.end(), GreaterThree);
	cout << "count of greater 3: " << cnt << endl;
	// count of greater 3: 3

}

int main()
{
	play_count_if();

	return 0;
}

find()

find:  利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_find()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	vector<int>::iterator it = find(v1.begin(), v1.end(), 4);
	if (it == v1.end()) {
		cout << "find fail\n";
	}
	else {
		cout << "find success\n";
	}
	// find success

}

int main()
{
	play_find();

	return 0;
}

find_if()

find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
	return a > 3;
}

void play_find_if()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterThree);
	if (it == v1.end()) {
		cout << "find fail\n";
	}
	else {
		cout << "find success\n";
		cout << "value: " << *it << endl;
	}
	// find success
	// value: 4

}

int main()
{
	play_find_if();

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-30 03:15:54

STL常用查找算法介绍的相关文章

STL常用排序算法介绍

merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void printV(vector<int> &v) { for (vector<int>::iterator it = v

c++STL之常用查找算法

引入#include<algorithm> 算法简介: find:查找元素 find_if:按条件查找 adjacent_find:查找相邻房重复的元素 binary_search:二分查找 count:统计元素个数 count_if:按条件统计元素个数 1.find #include<iostream> using namespace std; #include <vector> #include <algorithm> #include <stri

C++ STL 常用遍历算法

C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了统一性奠定了基 础 3) STL的算法,通过函数对象实现了自定义数据类型的算法运算:所以说:STL的算法也提 供了统一性.                核心思想:其实函数对象本质就是回调函数,回调函数的思想:就是任务的编写者和任务的调用者有效解耦合.函数指针做函数参数.4) 具体例子:transf

C++ STL之查找算法

C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个迭代器(查找成功返回目标所在迭代器的位置,否则返回最后一个元素的后一个位置或者说是容器的end()) 2.查找算法经常会用到迭代器区间,注意区间是前闭后开的 3.所有查找函数中如果存在两个区间,第一个区间是被查找对象的区间,第二个是目标对象的区间,如果只有一个区间则是被查找对象的区间. 4.对于有序

常用查找算法

原文出处:http://www.cnblogs.com/yw09041432/p/5908444.html 1.顺序查找 2.二分查找 3.差值查找 4.肥婆那楔查找 5.树表查找 6.分块查找 7.哈希查找 查找:根据给定的某个值,在查找表中确定一个其关键字等于给定值得数据元素(或记录). 查找算法分类: 1.静态查找和动态查找 动态查找指查找表中有删除和插入操作的表. 2.无序查找和有序查找 无序查找:被查找数列有序无序均可 有序查找:被查找数列必须有序 1.顺序查找 基本思想:顺序查找也称

STL常用遍历算法for_each和transform的比较

for_each()和transform()算法比较 1)STL 算法 – 修改性算法 for_each() copy() copy_backward() transform() merge() swap_ranges() fill() fill_n() generate() generate_n() replace replace_if() replace_copy() replace_copy_if() 2) for_each() 速度快 不灵活 transform() 速度慢 非常灵活 一

常用查找算法的总结

数据结构中查找的知识点主要有以下三点 1.静态查找 1.1 顺序查找 1.2 有序表 1.2.1 二分查找 1.2.2 插值查找 2.动态查找 2.1 二叉排序树 2.2 平衡二叉树 2.3 B-和B+树 3.哈希查找 3.1 常用哈希函数 3.2 解决冲突的办法 1.2.1.1  非递归实现 实现思路: 1.low=0,high=len-1; 2.mid=(low+high)/2; 3.中间值小于目标值,high=mid-1 4.中间值大于目标值,low=mid+1 5.重复 public s

【Java_Base】常用查找算法:顺序查找、二分查找

顺序查找 从第一个元素开始顺序比较查找. 二分查找 二分查找前提条件: 已排序的数组中查找 二分查找的基本思想是: 首先确定该查找区间的中间点位置: int mid = (low+upper) / 2; 然后将待查找的值与中间点位置的值比较: 若相等,则查找成功并返回此位置. 若中间点位置值大于待查值,则新的查找区间是中间点位置的左边区域. 若中间点位置值小于待查值,则新的查找区间是中间点位置的右边区域. 下一次查找是针对新的查找区间进行的. 1 public class Search{ 2 p

常用查找算法总结

1. 二分查找 //递归版 int binarySearch(const int arr[], int low, int high, int val) { if (low <= high) { int mid = low + (high - low) / 2; // Do not use (low + high) / 2 which might encounter overflow issue if (val < arr[mid]) return binarySearch(arr, low,