您的位置:首页 > 其它

find泛型算法的几种应用模式

2010-06-21 23:07 162 查看
(1)find

原型:find(InputIterator beg, InputIeterator end, const T& value)

它是返回区间[beg, end)中第一个“元素值等于value”的元素位置,即在区间中查找某一个具体的值;

(2)find_if

原型:find_if(InputIterator beg, InputIeterator end, UnaryPredicate op)

它是返回区间[beg, end)中令一元判断式op结果为true的第一个元素

(3)adjacent_find

原型:adjacent_find(InputIterator beg, InputIeterator end)

adjacent_find(InputIterator beg, InputIeterator end, BinaryPredicate op)

查找两个连续且相等的元素,op为函数对象

例子如下:

#include <algorithm>
#include <numeric>
#include <list>
#include <cassert>
#include <vector>
#include <functional>
#include <iterator>
using namespace std;

class TwiceOver
{
public:
bool operator()(int val1, int val2)
{
return val1 == val2/2 ? true : false;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
int ia[] = { 1, 4, 4, 8 };
std::vector<int> vec(ia, ia + 4);
int *piter = NULL;
vector<int>::iterator iter;
piter = adjacent_find(ia, ia + 4);
assert(*piter == ia[1]);
iter = adjacent_find(vec.begin(), vec.end(), TwiceOver());
assert(*iter == vec[2]);
cout<<"ok: adjacent-find() succeeded!"<<endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: