Find and search for elements of C++ STL vector container find() use of find_if() function

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void print(const int& temp){
    cout<<temp<<endl;
}

intmain()
{
    const int ARRAY_SIZE=8;
    int IntArray[ARRAY_SIZE]={1,2,3,4,5,6,7};
    vector<int> myvt;
    vector<int>::iterator location_index;
    for(int i=0;i<8;++i){
        myvt.push_back(IntArray[i]);
    }
    for_each(myvt.begin(),myvt.end(),print);
    location_index=find(myvt.begin(),myvt.end(),2);  //find函数
    cout<<"The subscript of number 2 is: "<<(location_index-myvt.begin())<<endl;
    location_index=find_if(myvt.begin(),myvt.end(),bind2nd(greater<int>(),5));
    cout<<"The subscript of the first number greater than 5 is:"<<(location_index-myvt.begin())<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325554952&siteId=291194637