C++STL的非变易算法(Non-mutating algorithms)是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。
find算法用于查找等于某值的元素。它在迭代器区间[first , last)上查找等于value值的元素,如果迭代器iter所指的元素满足 *iter == value ,则返回迭代器iter,未找则返回last。
函数原型:
- template<class InputIterator, class Type>
- InputIterator find(
- InputIterator _First,
- InputIterator _Last,
- const Type& _Val
- );
示例代码:
/********************************************************************
created: 2013/06/05 22:50
Filename: test.cpp
author: Neo
*********************************************************************/
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> nList;
for (size_t i = 0; i < 10; ++i)
{
nList.push_back(i+1);
}
nList.push_back(10);
list<int>::iterator iLocation = find(nList.begin(), nList.end(), 10);
if (iLocation != nList.end())
{
cout << "找到元素 10" << endl;
}
cout << "前一个元素为:" << *(--iLocation) << endl;
return 0;
}