find_first_of用于在序列A中查找某个元素,该元素至少和序列B中的一个元素存在满足指定条件的情况。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool findComp(int i, int j) { return i == j; }
bool findComp2(int i, int j) { return i > j; }
int main()
{
vector<int> v{ 1,2,2,2,4,5,2,3,4,7,8,9,1,7,2,3,4 };
vector<int> d{ 5,2,3,4};
auto it = find_first_of(v.begin(), v.end(), d.begin(), d.end());
if (it != v.end())
cout << "v[" << it - v.begin() << "]=" << *it << endl;
it = find_first_of(v.begin(), v.end(), d.begin(), d.end(), findComp);
if (it != v.end())
cout << "v[" << it - v.begin() << "]=" << *it << endl;
it = find_first_of(v.begin(), v.end(), d.begin(), d.end(), findComp2);
if (it != v.end())
cout << "v[" << it - v.begin() << "]=" << *it << endl;
return 0;
}
输出结果:
v[1]=2
v[1]=2
v[4]=4