find_first_of(beg,end,sbeg,send)和find_first_of(beg,end,sbeg,send,op)的特点
1:无op版本,返回同时在区间[beg,end)和[sbeg,send)出现的第一个元素位置的迭代器,同时我们可以采用逆向迭代器获得最后一个元素,未发现则end
2:有op版本,返回区间[beg,end)中第一个能与[sbeg,send)内其中一个元素使得op(elem,selem)为真的元素位置的迭代器,未发现则end
3:op不应该改动传入的参数以及自身的状态
4:迭代器类型:前向迭代器
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
int main()
{
//寻找第一个同时在c1,c2出现的元素
vector<int>c1 = { 1,2,3,4,5,6,7,8,9 };
vector<int>c2 = {3,4,5,6,7};
auto it = find_first_of(c1.begin(), c1.end(), c2.begin(), c2.end());
if (it != c1.end())
//返回满足条件的元素距离首部的距离
cout << distance(c1.begin(),it)+1<< endl;
else
cout << "未发现" << endl;
}
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
int main()
{
//寻找最后一个同时在c1,c2出现的元素
vector<int>c1 = { 1,2,3,4,5,6,7,8,9 };
vector<int>c2 = {3,4,5,6,7};
auto it = find_first_of(c1.rbegin(), c1.rend(), c2.begin(), c2.end());
if (it != c1.rend())
//返回满足条件的元素距离首部的距离
cout << distance(c1.begin(),it.base())<< endl;
else
cout << "未发现" << endl;
}
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
int main()
{
//寻找第一个c1中出现,并内与c2其中一个元素使得op返回真的元素,即第一个大于5的元素位置
vector<int>c1 = { 1,2,3,4,5,6,7,8,9 };
vector<int>c2 = {7,5,6};
auto it = find_first_of(c1.begin(), c1.end(), c2.begin(), c2.end(),greater<int>());
if (it != c1.end())
//返回满足条件的元素距离首部的距离
cout << distance(c1.begin(),it)+1<< endl;
else
cout << "未发现" << endl;
}
**op推荐采用lambdas表达式:lambdas的使用方法
**