查找算法2

#include <iostream>
#include <algorithm>
#include <deque>
#include <functional>

using namespace std;

int main()
{
	deque<int> ideq;
	for (int i = 0; i <= 9; ++i)
	if (i == 3)
	{
		ideq.push_back(i);
		ideq.push_back(i);
		ideq.push_back(i);
		ideq.push_back(i);
	}
	else
	{
		ideq.push_back(i);
	}
	for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;

	deque<int>::iterator pos;
	pos = search_n(ideq.begin(), ideq.end(), 4, 3);
	if (pos != ideq.end())
		cout << "找到了连续的4个3," << *pos << endl;
	else
		cout << "没有找到," << endl;

	pos = search_n(ideq.begin(), ideq.end(), 3, 6, greater<int>());  // 输出的是连续三个大于6的数,greater<>()是取大数的谓词,
	//pos = search_n_if(ideq.begin(), ideq.end(), 3, bind2nd(greater<int>(),6));
	if (pos != ideq.end())
		cout << "找到了连续的3个大于6的数,下标是:" << distance(ideq.begin(), pos) + 1 << endl;
	else
		cout << "没有找到," << endl;

	return 0;
}

你可能感兴趣的:(查找算法2)