C++ STL vector容器元素的查找和搜索 find() find_if()函数的使用
#include <iostream>#include <vector>#include <algorithm>#include <functional>using namespace std;void print(const int& temp){cout<
·
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void print(const int& temp){
cout<<temp<<endl;
}
int main()
{
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<<"数字2的下标是:"<<(location_index-myvt.begin())<<endl;
location_index=find_if(myvt.begin(),myvt.end(),bind2nd(greater<int>(),5));
cout<<"第一个大于5的数字的下标是:"<<(location_index-myvt.begin())<<endl;
return 0;
}

更多推荐
活动日历
查看更多
直播时间 2025-02-26 16:00:00


直播时间 2025-01-08 16:30:00


直播时间 2024-12-11 16:30:00


直播时间 2024-11-27 16:30:00


直播时间 2024-11-21 16:30:00


所有评论(0)