#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<int> ivec;
list<int> searchList;
for (int i = 0; i <= 9; ++i)
ivec.push_back(i);
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
searchList.push_back(3);
searchList.push_back(6);
searchList.push_back(9);
vector<int>::iterator pos;
pos = find_first_of(ivec.begin(), ivec.end(), searchList.begin(), searchList.end());
if (pos != ivec.end())
cout << "找到了,位置:" << distance(ivec.begin(), pos) + 1 << endl;
else
cout << "没有找到。" << endl;
vector<int>::reverse_iterator rpos; // reverse_iterator 是逆向迭代器,
rpos = find_first_of(ivec.rbegin(), ivec.rend(), searchList.begin(), searchList.end());
cout << "找到的位置:" << distance(ivec.begin(),rpos.base()) << endl; // rpos.base()是将逆向迭代器转换为正向迭代器,
string numerics("0123456789");
string name("ra8a7ch");
string::size_type p = name.find_first_of(numerics);
if (p != string::npos)
cout << "找到了,位置:" << p << endl; // 输出是2,
else
cout << "没有找到," << endl;
p = name.find_last_of(numerics);
if (p != string::npos)
cout << "找到了,位置:" << p << endl; // 输出是4,
else
cout << "没有找到," << endl;
return 0;
}
查找算法4
最新推荐文章于 2020-01-07 17:41:34 发布