其功能可以是在迭代器first、last指定的容器的一个区间[first, last) 中,按顺序查找和val相等的元素。如果找到,就返回该元素的迭代器;如果找不到,就返回last。find模板使用==运算符判断元素是否相等。因此,如果[first, last)区间中存放的是对象,则==运算符应该被适当重载,使得两个对象可以用==运算符比较。find的用法实例如下: #include "iostream"
#include "algorithm"
#include "vector"
using namespace std;
int main(){
int a[10] = {10, 20, 44, 66, 99, 101};
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4); // 此后v里放着4个元素:1,2,3,4
vector<int>::iterator p;
p = find(v.begin(), v.end(), 3); // 在v中查找3
if(p!=v.end()) // 若找不到,find返回 v.end()
cout << *p << endl; // 找到了
p = find(v.begin(), v.end(), 9);
if(p == v.end())
cout << "not found!\n";
p = find(v.begin()+1, v.end()-1, 4); // 在,3 这两个元素中查找4
cout << *p << endl;
int *pp = find(a, a+4, 20);
if(pp == a+4)
cout << "not found" << endl;
else
cout << *pp << endl;
return 0;
}