查找算法5 查找两个连续的数adjacent_find

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

using namespace std;

bool doubled(int even1, int even2)
{
	return even1 * 2 == even2;
}

int main()
{
	vector<int>  ivec;

	ivec.push_back(1);
	ivec.push_back(3);
	ivec.push_back(2);
	ivec.push_back(4);
	ivec.push_back(5);
	ivec.push_back(5);
	ivec.push_back(0);

	for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;

	vector<int>::iterator pos;
	pos = adjacent_find(ivec.begin(), ivec.end());  // adjacent_find表示查找连续的两个相同的数,
	if (pos != ivec.end())
		cout << "找到了,位置:" << distance(ivec.begin(), pos) + 1 << endl;
	else
		cout << "没有找到," << endl;
 
	pos = adjacent_find(ivec.begin(), ivec.end(), doubled);
	if (pos != ivec.end())
		cout << "找到了,位置:" << distance(ivec.begin(), pos) + 1 << endl;
	else
		cout << "没有找到," << endl;


	return 0;
}

你可能感兴趣的:(查找算法5 查找两个连续的数adjacent_find)