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;
}
原型: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;
}
相关文章推荐
- 转】SLF4J 的几种实际应用模式--之二:SLF4J+Logback
- 【Java.ThirdParty】日志——Slf4j的几种应用模式
- SLF4J 的几种实际应用模式--之一:SLF4J+Log4J
- SLF4J 的几种实际应用模式--之二:SLF4J+Logback
- SLF4J 的几种实际应用模式--之一:SLF4J+Log4J(转载)
- SLF4J 的几种实际应用模式--之二:SLF4J+Log4J 与 SLF4J+LogBack
- SLF4J 的几种实际应用模式--之一:SLF4J+Log4J(转载)
- 【转】SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J
- SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J
- 设计模式用过哪些,应用场景是什么;单例模式有几种实现方式,代码怎么写?
- SLF4J 的几种实际应用模式--之二:SLF4J+Logback(转载)
- (转载)单例模式的几种应用
- 转】SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J
- UIViewController的几种跳转模式及其应用
- SLF4J 的几种实际应用模式--之二:SLF4J+Logback
- SLF4J 的几种实际应用模式 SLF4J+Log4J 与 SLF4J+LogBack
- SLF4J 的几种实际应用模式--:SLF4J+Log4J 与 SLF4J+LogBack
- SLF4J 的几种实际应用模式--:SLF4J+Log4J 与 SLF4J+LogBack
- SLF4J 的几种实际应用模式--之一:SLF4J+Log4J
- SLF4J 的几种实际应用模式--之一:SLF4J+Log4J