search用于在序列A中查找满足条件的序列B,返回序列B在序列A中第一次出现的位置;
如果序列B的元素都相等,那么可以使用search_n代替search实现查找;
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v{ 1,2,4,5,2,3,4,7,7,7,7,8,9,7,7,7,3,4 };
auto it = search_n(v.begin(), v.end(), 3, 7); //查找7连续出现3次的位置
if (it != v.end())
cout << "v[" << it - v.begin() << "]=" << *it << endl;
return 0;
}
输出结果:
v[7]=7