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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值