怎样用find函数在vector中遍历查找某元素

怎样用find函数在vector中遍历查找某元素,就用find函数,find if函数不是很会用,有大佬能顺便给讲一下这个感激不尽。

假设 A = {1 0 1 1 0 0 0 1 0},如果想找到所有 1 ,可以这样:

std::vector<int>::iterator iter = A.begin();
while ((iter = std::find_if(iter, A.end(), 1)) != A.end())  //当iter等于A.end()时,意味着处理完毕,退出循环
{
    // 使用找到的iter进行其它处理,在这里加上您的代码

    iter++;  //iter加1,作为下次查找的起点。
}

如果对您有帮助,请采纳答案好吗,谢谢!

如果vector元素类型是结构体怎么办?

 #include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main( )
{
    vector<int> L;
    L.push_back( 1 );
    L.push_back( 2 );
    L.push_back( 3 );
    L.push_back( 4 );
    L.push_back( 5 );
    vector<int>::iterator result = find( L.begin( ), L.end( ), 3 ); //查找3
    if ( result == L.end( ) ) //没找到
        cout << "No" << endl;
    else //找到
        cout << "Yes" << endl;

}

如果要找更多,可以多次调用find,第一个参数为当前找到的+1

vector::iterator result = find( L.begin( ), L.end( ), a ); a就是你要的元素